Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > about structures memory allocation

Reply
Thread Tools

about structures memory allocation

 
 
Barry Schwarz
Guest
Posts: n/a
 
      06-24-2007
On Sun, 24 Jun 2007 00:10:45 -0000, SM Ryan
<> wrote:

>"" <> wrote:
># how much memory is allocated for following structure
>
>None. Structures only exist in the compiler; in the code
>you have blocks of allocated memory and offsets into them.
>
>A variable of type T needs no more than sizeof(T) char sized
>units allocated on appropriate memory boundary.
>
># struct bharath
># {
># int b;
># char c;
># float d;
># }
>
>A variable of type (struct bharath) will be allocated at
>least sizeof(struct bharath) bytes. How a compiler allocates


Would you care to give an example of when it will be allocated more
than sizeof(srtuct bharath) bytes?

>the space is really up to the compiler. If you need to know
>there are field offset macros to find otu where each field
>begins in the variable's allocated space.



Remove del for email
 
Reply With Quote
 
 
 
 
Gordon Burditt
Guest
Posts: n/a
 
      06-24-2007
>># how much memory is allocated for following structure
>>
>>None. Structures only exist in the compiler; in the code
>>you have blocks of allocated memory and offsets into them.
>>
>>A variable of type T needs no more than sizeof(T) char sized
>>units allocated on appropriate memory boundary.
>>
>># struct bharath
>># {
>># int b;
>># char c;
>># float d;
>># }
>>
>>A variable of type (struct bharath) will be allocated at
>>least sizeof(struct bharath) bytes. How a compiler allocates

>
>Would you care to give an example of when it will be allocated more
>than sizeof(srtuct bharath) bytes?


If struct bharath requires padding so the second element of an array
of struct bharath is properly aligned, the padding is part of
sizeof(struct bharath). In other words, the padding goes *INSIDE*
the struct, not between array elements in an array of this struct.

There might be padding between auto variables if, for example, a
long long requires stricter alignment than a struct bharath and
happens to come after one and would end up misaligned. Which
variable you "blame" or "charge" for the padding is better determined
by a course in cost accounting rather than C.

malloc(sizeof(struct bharath)) may allocate more memory than
requested, among other reasons, so it can satisfy the requirement
that allocations are aligned suitably for any object. For example,
int and float may be aligned on a 4-byte boundary (so sizeof(struct
bharath) might come out 12, including 3 bytes of padding after the
char), but malloc() may round a request up to a multiple of 8,
because there might be other things (say, long long and double)
which are aligned to a multiple of 8. This also doesn't account
for the possibility that malloc() may also allocate space for
internal bookkeeping information used to keep track of allocated
and free blocks.


 
Reply With Quote
 
 
 
 
Keith Thompson
Guest
Posts: n/a
 
      06-24-2007
Barry Schwarz <> writes:
> On Sun, 24 Jun 2007 00:10:45 -0000, SM Ryan
> <> wrote:

[...]
>>> struct bharath
>>> {
>>> int b;
>>> char c;
>>> float d;
>>> }

>>
>>A variable of type (struct bharath) will be allocated at
>>least sizeof(struct bharath) bytes. How a compiler allocates

>
> Would you care to give an example of when it will be allocated more
> than sizeof(srtuct bharath) bytes?


malloc(sizeof(struct bharath)) can easily allocate more than
sizeof(struct bharath) bytes. Even for an object declaration, there
may be a gap between the object and the next object in memory.

You could argue, of course, that the extra bytes aren't allocated *to
the object*, but are just padding outside the object (and I wouldn't
disagree).

--
Keith Thompson (The_Other_Keith) kst- <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
Barry Schwarz
Guest
Posts: n/a
 
      06-24-2007
On Sun, 24 Jun 2007 02:39:04 -0000, (Gordon
Burditt) wrote:

>>># how much memory is allocated for following structure
>>>
>>>None. Structures only exist in the compiler; in the code
>>>you have blocks of allocated memory and offsets into them.
>>>
>>>A variable of type T needs no more than sizeof(T) char sized
>>>units allocated on appropriate memory boundary.
>>>
>>># struct bharath
>>># {
>>># int b;
>>># char c;
>>># float d;
>>># }
>>>
>>>A variable of type (struct bharath) will be allocated at
>>>least sizeof(struct bharath) bytes. How a compiler allocates

>>
>>Would you care to give an example of when it will be allocated more
>>than sizeof(srtuct bharath) bytes?

>
>If struct bharath requires padding so the second element of an array
>of struct bharath is properly aligned, the padding is part of
>sizeof(struct bharath). In other words, the padding goes *INSIDE*
>the struct, not between array elements in an array of this struct.


That was my obviously too subtle point to the OP.

>
>There might be padding between auto variables if, for example, a
>long long requires stricter alignment than a struct bharath and
>happens to come after one and would end up misaligned. Which
>variable you "blame" or "charge" for the padding is better determined
>by a course in cost accounting rather than C.


This has nothing to do with the question asked.

>
>malloc(sizeof(struct bharath)) may allocate more memory than
>requested, among other reasons, so it can satisfy the requirement
>that allocations are aligned suitably for any object. For example,
>int and float may be aligned on a 4-byte boundary (so sizeof(struct
>bharath) might come out 12, including 3 bytes of padding after the
>char), but malloc() may round a request up to a multiple of 8,
>because there might be other things (say, long long and double)
>which are aligned to a multiple of 8. This also doesn't account
>for the possibility that malloc() may also allocate space for
>internal bookkeeping information used to keep track of allocated
>and free blocks.


All true but unrelated to the question. The OP is talking about
defined objects, not about dynamic allocation.

>


And it would really help if you did not delete attributions.


Remove del for email
 
Reply With Quote
 
Barry Schwarz
Guest
Posts: n/a
 
      06-24-2007
On Sat, 23 Jun 2007 19:54:37 -0700, Keith Thompson <kst->
wrote:

>Barry Schwarz <> writes:
>> On Sun, 24 Jun 2007 00:10:45 -0000, SM Ryan
>> <> wrote:

>[...]
>>>> struct bharath
>>>> {
>>>> int b;
>>>> char c;
>>>> float d;
>>>> }
>>>
>>>A variable of type (struct bharath) will be allocated at
>>>least sizeof(struct bharath) bytes. How a compiler allocates

>>
>> Would you care to give an example of when it will be allocated more
>> than sizeof(srtuct bharath) bytes?

>
>malloc(sizeof(struct bharath)) can easily allocate more than
>sizeof(struct bharath) bytes. Even for an object declaration, there
>may be a gap between the object and the next object in memory.


malloc does not allocate objects. It allocates the requested amount
of space suitably aligned for any object.

Why is everyone stripping out the next section of the original post
where he made it clear he was talking about how the compiler allocates
space for defined objects?

>
>You could argue, of course, that the extra bytes aren't allocated *to
>the object*, but are just padding outside the object (and I wouldn't
>disagree).



Remove del for email
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      06-24-2007
Barry Schwarz <> writes:
> On Sat, 23 Jun 2007 19:54:37 -0700, Keith Thompson <kst->
> wrote:
>>Barry Schwarz <> writes:
>>> On Sun, 24 Jun 2007 00:10:45 -0000, SM Ryan
>>> <> wrote:

>>[...]
>>>>> struct bharath
>>>>> {
>>>>> int b;
>>>>> char c;
>>>>> float d;
>>>>> }
>>>>
>>>>A variable of type (struct bharath) will be allocated at
>>>>least sizeof(struct bharath) bytes. How a compiler allocates
>>>
>>> Would you care to give an example of when it will be allocated more
>>> than sizeof(srtuct bharath) bytes?

>>
>>malloc(sizeof(struct bharath)) can easily allocate more than
>>sizeof(struct bharath) bytes. Even for an object declaration, there
>>may be a gap between the object and the next object in memory.

>
> malloc does not allocate objects. It allocates the requested amount
> of space suitably aligned for any object.


That's arguable. An object is a "region of data storage in the
execution environment, the contents of which can represent values".
That's pretty much what malloc allocates.

> Why is everyone stripping out the next section of the original post
> where he made it clear he was talking about how the compiler allocates
> space for defined objects?


Um, what section was that? Here's the original post in its entirety:

| how much memory is allocated for following structure
| struct bharath
| {
| int b;
| char c;
| float d;
| }
|
| and how?

Just a type declaration.

Now I'm really just nitpicking here, not arguing any significant
point. It's true that anything that allocates an object of type
struct bharath, either an object declaration or a malloc call, can
easily allocate more that sizeof(struct bharath) bytes. It's also
true that the size of any struct bharath object is, by definition,
sizeof(struct bharath) bytes.

--
Keith Thompson (The_Other_Keith) kst- <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
jaysome
Guest
Posts: n/a
 
      06-24-2007
On Thu, 21 Jun 2007 16:22:20 -0500, Jack Klein <>
wrote:

>On Thu, 21 Jun 2007 11:29:36 -0400, Clever Monkey
><> wrote in comp.lang.c:
>
>> wrote:

>
> [snip]
>
>> > what i want actually is how much size will be allocated for above
>> > structuer in a 16 bit machine
>> >

>> Enough to hold the structure, plus any padding, on this specific 16-bit
>> machine.

>
>No, actually, just enough to hold the structure. If there is any
>padding in the structure, it is _IN_ the structure.


#include <stdio.h>
#include <stddef.h>

struct foo
{
double d;
char c;
};

int main(void)
{
struct foo f;
printf("offsetof(d) = %d\n", (int)offsetof(struct foo,d));
printf("offsetof(c) = %d\n", (int)offsetof(struct foo,c));
printf("sizeof(d) = %d\n", (int)sizeof(f.d));
printf("sizeof(c) = %d\n", (int)sizeof(f.c));
printf("sizeof(struct foo) = %d\n", (int)sizeof(struct foo));
return 0;
}

Output:

offsetof(d) = 0
offsetof(c) = 8
sizeof(d) = 8
sizeof(c) = 1
sizeof(struct foo) = 16

There appears to padding in this structure, but is it really _IN_ the
structure?

--
jay


 
Reply With Quote
 
J. J. Farrell
Guest
Posts: n/a
 
      06-24-2007
On Jun 24, 1:38 am, jaysome <jays...@hotmail.com> wrote:
> On Thu, 21 Jun 2007 16:22:20 -0500, Jack Klein <jackkl...@spamcop.net>
> wrote:
>
> >On Thu, 21 Jun 2007 11:29:36 -0400, Clever Monkey
> ><spamt...@clevermonkey.org.INVALID> wrote in comp.lang.c:

>
> >> bharath...@gmail.com wrote:

>
> > [snip]

>
> >> > what i want actually is how much size will be allocated for above
> >> > structuer in a 16 bit machine

>
> >> Enough to hold the structure, plus any padding, on this specific 16-bit
> >> machine.

>
> >No, actually, just enough to hold the structure. If there is any
> >padding in the structure, it is _IN_ the structure.

>
> #include <stdio.h>
> #include <stddef.h>
>
> struct foo
> {
> double d;
> char c;
>
> };
>
> int main(void)
> {
> struct foo f;
> printf("offsetof(d) = %d\n", (int)offsetof(struct foo,d));
> printf("offsetof(c) = %d\n", (int)offsetof(struct foo,c));
> printf("sizeof(d) = %d\n", (int)sizeof(f.d));
> printf("sizeof(c) = %d\n", (int)sizeof(f.c));
> printf("sizeof(struct foo) = %d\n", (int)sizeof(struct foo));
> return 0;
>
> }
>
> Output:
>
> offsetof(d) = 0
> offsetof(c) = 8
> sizeof(d) = 8
> sizeof(c) = 1
> sizeof(struct foo) = 16
>
> There appears to padding in this structure, but is it really _IN_ the
> structure?


Yes; that's what your example shows.

 
Reply With Quote
 
Army1987
Guest
Posts: n/a
 
      06-24-2007

"Keith Thompson" <kst-> ha scritto nel messaggio news:...
> Barry Schwarz <> writes:
>> On Sat, 23 Jun 2007 19:54:37 -0700, Keith Thompson <kst->
>> wrote:
>>>Barry Schwarz <> writes:
>>>> On Sun, 24 Jun 2007 00:10:45 -0000, SM Ryan
>>>> <> wrote:
>>>[...]
>>>>>> struct bharath
>>>>>> {
>>>>>> int b;
>>>>>> char c;
>>>>>> float d;
>>>>>> }
>>>>>
>>>>>A variable of type (struct bharath) will be allocated at
>>>>>least sizeof(struct bharath) bytes. How a compiler allocates
>>>>
>>>> Would you care to give an example of when it will be allocated more
>>>> than sizeof(srtuct bharath) bytes?
>>>
>>>malloc(sizeof(struct bharath)) can easily allocate more than
>>>sizeof(struct bharath) bytes. Even for an object declaration, there
>>>may be a gap between the object and the next object in memory.

>>
>> malloc does not allocate objects. It allocates the requested amount
>> of space suitably aligned for any object.

>
> That's arguable. An object is a "region of data storage in the
> execution environment, the contents of which can represent values".
> That's pretty much what malloc allocates.
>
>> Why is everyone stripping out the next section of the original post
>> where he made it clear he was talking about how the compiler allocates
>> space for defined objects?

>
> Um, what section was that? Here's the original post in its entirety:
>
> | how much memory is allocated for following structure
> | struct bharath
> | {
> | int b;
> | char c;
> | float d;
> | }
> |
> | and how?
>
> Just a type declaration.
>
> Now I'm really just nitpicking here, not arguing any significant
> point. It's true that anything that allocates an object of type
> struct bharath, either an object declaration or a malloc call, can
> easily allocate more that sizeof(struct bharath) bytes. It's also
> true that the size of any struct bharath object is, by definition,
> sizeof(struct bharath) bytes.


In something like
int main(void)
{
struct bharath b;
long long ll;
...
}
since the standard requires nothing about the placement of auto
variables, it is well possible that there is some space between
b and ll, but an auto char in an inner block might well be put into
that space. (Yes, I don't think it is very likely to happen...)
So, as far as a reasonable definition of "to allocate" is
concerned, these two declarations allocate exactly sizeof b +
sizeof ll bytes, even if they are not contiguous.


 
Reply With Quote
 
Barry Schwarz
Guest
Posts: n/a
 
      06-24-2007
On Sun, 24 Jun 2007 01:14:43 -0700, Keith Thompson <kst->
wrote:

>Barry Schwarz <> writes:
>> On Sat, 23 Jun 2007 19:54:37 -0700, Keith Thompson <kst->
>> wrote:
>>>Barry Schwarz <> writes:
>>>> On Sun, 24 Jun 2007 00:10:45 -0000, SM Ryan
>>>> <> wrote:
>>>[...]
>>>>>> struct bharath
>>>>>> {
>>>>>> int b;
>>>>>> char c;
>>>>>> float d;
>>>>>> }
>>>>>
>>>>>A variable of type (struct bharath) will be allocated at
>>>>>least sizeof(struct bharath) bytes. How a compiler allocates
>>>>
>>>> Would you care to give an example of when it will be allocated more
>>>> than sizeof(srtuct bharath) bytes?
>>>
>>>malloc(sizeof(struct bharath)) can easily allocate more than
>>>sizeof(struct bharath) bytes. Even for an object declaration, there
>>>may be a gap between the object and the next object in memory.

>>
>> malloc does not allocate objects. It allocates the requested amount
>> of space suitably aligned for any object.

>
>That's arguable. An object is a "region of data storage in the
>execution environment, the contents of which can represent values".
>That's pretty much what malloc allocates.
>
>> Why is everyone stripping out the next section of the original post
>> where he made it clear he was talking about how the compiler allocates
>> space for defined objects?

>
>Um, what section was that? Here's the original post in its entirety:
>
>| how much memory is allocated for following structure
>| struct bharath
>| {
>| int b;
>| char c;
>| float d;
>| }
>|
>| and how?
>

Not entirely. Here is the section I originally responded to and in
which I imbedded my original question regarding the OP's assertion of
**at least** so many bytes:

"A variable of type (struct bharath) will be allocated at
least sizeof(struct bharath) bytes. How a compiler allocates
the space is really up to the compiler. If you need to know
there are field offset macros to find otu where each field
begins in the variable's allocated space."

Note that it talks about the compiler and not the run time library.


Remove del for email
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
An idea for heap allocation at near stack allocation speed Bjarke Hammersholt Roune C++ 14 03-06-2011 08:07 AM
Allocation of a multidimensional array of structures Michel Rouzic C Programming 14 06-24-2007 06:31 PM
static memory allocation versus dynamic memory allocation Ken C Programming 24 11-30-2006 12:37 AM
What is the difference between dynamic memory allocation,and stack allocation ? chris C++ 6 10-28-2005 05:27 AM
structures, structures and more structures (questions about nestedstructures) Alfonso Morra C Programming 11 09-24-2005 07:42 PM



Advertisments