471,310 Members | 1,409 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,310 software developers and data experts.

about structures memory allocation

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

and how?

Jun 21 '07 #1
43 2216
bh********@gmail.com wrote:
how much memory is allocated for following structure
struct bharath
{
int b;
char c;
float d;
}

and how?
Enough to fit at least the total memory requirements of the struct.
Since the primitive types in the struct can vary in width from platform
to platform and implementation to implementation (except char, of
course) the size of the struct will also vary.

How do you do it, or how is memory allocated at runtime? The former is
just allocating for any type, really. If the latter, this is likely
implementation specific, at least based on my lurking in this newsgroup,
and my (poor) knowledge of the Standard.
--
clvrmnky <mailto:sp******@clevermonkey.org>

Direct replies will be blacklisted. Replace "spamtrap" with my name to
contact me directly.
Jun 21 '07 #2
bh********@gmail.com said:
how much memory is allocated for following structure
struct bharath
{
int b;
char c;
float d;
}
None. It's a type, not an object.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Jun 21 '07 #3
On Jun 21, 7:33 pm, Richard Heathfield <r...@see.sig.invalidwrote:
bharath...@gmail.com said:
how much memory is allocated for following structure
struct bharath
{
int b;
char c;
float d;
}

None. It's a type, not an object.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999http://www.cpax.org.uk
email: rjh at the above domain, - www.
let the int size is 4B then the allocation will be?
i want to know the info regarding padding or some other else related
to padding?

Jun 21 '07 #4
bh********@gmail.com wrote:
how much memory is allocated for following structure
struct bharath
{
int b;
char c;
float d;
}

and how?
Objects of that type will have enough memory allocated to hold an
int, a char, a float (in that order), plus any necessary padding.

The details vary with the implementation -- which may include the
option settings for your compilation(s).

If you want to know how much has /actually/ been allocated, use
`sizeof (struct bharath)`.

--
Hewlett-Packard Limited registered no:
registered office: Cain Road, Bracknell, Berks RG12 1HN 690597 England

Jun 21 '07 #5
balu wrote:
On Jun 21, 7:33 pm, Richard Heathfield <r...@see.sig.invalidwrote:
>bharath...@gmail.com said:
>>how much memory is allocated for following structure
struct bharath
{
int b;
char c;
float d;
}
None. It's a type, not an object.
[Please use a newsreader that understands how to trim sigs, or trim them
yourself. I've done this for you here.]
let the int size is 4B then the allocation will be?
i want to know the info regarding padding or some other else related
to padding?
This is exactly the sort of thing that people talk about when they say
something is implementation specific. That is, once you actually
allocate for a type, the specifics of how that memory is allocated is
not necessarily dictated by the Standard.

Simply defining (or is it declaring?) the type, of course, allocates no
memory.
--
clvrmnky <mailto:sp******@clevermonkey.org>

Direct replies will be blacklisted. Replace "spamtrap" with my name to
contact me directly.
Jun 21 '07 #6
On Jun 21, 7:49 pm, Clever Monkey <spamt...@clevermonkey.org.INVALID>
wrote:
balu wrote:
On Jun 21, 7:33 pm, Richard Heathfield <r...@see.sig.invalidwrote:
bharath...@gmail.com said:
>how much memory is allocated for following structure
struct bharath
{
int b;
char c;
float d;
}
None. It's a type, not an object.

[Please use a newsreader that understands how to trim sigs, or trim them
yourself. I've done this for you here.]
let the int size is 4B then the allocation will be?
i want to know the info regarding padding or some other else related
to padding?

This is exactly the sort of thing that people talk about when they say
something is implementation specific. That is, once you actually
allocate for a type, the specifics of how that memory is allocated is
not necessarily dictated by the Standard.

Simply defining (or is it declaring?) the type, of course, allocates no
memory.
--
clvrmnky <mailto:spamt...@clevermonkey.org>

Direct replies will be blacklisted. Replace "spamtrap" with my name to
contact me directly.
what i want actually is how much size will be allocated for above
structuer in a 16 bit machine

Jun 21 '07 #7
On Jun 21, 10:56 am, "bharath...@gmail.com" <bharath...@gmail.com>
wrote:
On Jun 21, 7:49 pm, Clever Monkey <spamt...@clevermonkey.org.INVALID>
wrote:
balu wrote:
On Jun 21, 7:33 pm, Richard Heathfield <r...@see.sig.invalidwrote:
>bharath...@gmail.com said:
>>how much memory is allocated for following structure
>> struct bharath
>> {
>> int b;
>> char c;
>> float d;
>> }
>None. It's a type, not an object.
[Please use a newsreader that understands how to trim sigs, or trim them
yourself. I've done this for you here.]
let the int size is 4B then the allocation will be?
i want to know the info regarding padding or some other else related
to padding?
This is exactly the sort of thing that people talk about when they say
something is implementation specific. That is, once you actually
allocate for a type, the specifics of how that memory is allocated is
not necessarily dictated by the Standard.
Simply defining (or is it declaring?) the type, of course, allocates no
memory.
--
clvrmnky <mailto:spamt...@clevermonkey.org>
Direct replies will be blacklisted. Replace "spamtrap" with my name to
contact me directly.

what i want actually is how much size will be allocated for above
structuer in a 16 bit machine
Here's the exact answer for you: your program will allocate
sizeof(struct bharath) bytes. Other than that, we cannot tell you, as
the exact number of bytes depends on the compiler, the operating
system, and the processor.

What we can tell you is that there may be padding characters inserted
between each element of the structure to maintain data item alignment.
If, *for instance*, ints were 4 bytes wide, and floats were 8 bytes
wide, and both were required to be aligned on "even" boundaries (ints
to &int%4==0 boundaries, floats to &float%8==0 boundaries), then the
compiler would likely insert zero padding bytes between your int and
your char, and seven padding bytes between your char and your float.
Jun 21 '07 #8
Oops... a correction

On Jun 21, 11:09 am, Lew Pitcher <lpitc...@teksavvy.comwrote:
[snip]
What we can tell you is that there may be padding characters inserted
between each element of the structure to maintain data item alignment.
If, *for instance*, ints were 4 bytes wide, and floats were 8 bytes
wide, and both were required to be aligned on "even" boundaries (ints
to &int%4==0 boundaries, floats to &float%8==0 boundaries), then the
compiler would likely insert zero padding bytes between your int and
your char, and seven padding bytes
make that three padding bytes
between your char and your float.

Jun 21 '07 #9
bh********@gmail.com wrote:
On Jun 21, 7:49 pm, Clever Monkey <spamt...@clevermonkey.org.INVALID>
wrote:
>balu wrote:
>>On Jun 21, 7:33 pm, Richard Heathfield <r...@see.sig.invalidwrote:
bharath...@gmail.com said:
how much memory is allocated for following structure
struct bharath
{
int b;
char c;
float d;
}
None. It's a type, not an object.
[...]
*sigh*
>>let the int size is 4B then the allocation will be?
i want to know the info regarding padding or some other else related
to padding?
This is exactly the sort of thing that people talk about when they say
something is implementation specific. That is, once you actually
allocate for a type, the specifics of how that memory is allocated is
not necessarily dictated by the Standard.

Simply defining (or is it declaring?) the type, of course, allocates no
memory.
[...]
*double sigh*
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.

Folks here are not being coy. There is no way to know, in general, how
your compiler will allocate space, even if we know something about it or
the platform is is generating code for. (Like it happens to be a
"16-bit" machine, whatever that is. How are those bits represented?)

If you have a specific implementation in mind, on a specific platform,
then someone with the knowledge and access to the documentation could
tell you with more detail.

This very quality of C is why the compiler has been successfully
targeted to a great many platforms, and why standard code is relatively
easy to port.
--
clvrmnky <mailto:sp******@clevermonkey.org>

Direct replies will be blacklisted. Replace "spamtrap" with my name to
contact me directly.
Jun 21 '07 #10
On Thu, 21 Jun 2007 14:56:39 -0000, in comp.lang.c ,
"bh********@gmail.com" <bh********@gmail.comwrote:
>
what i want actually is how much size will be allocated for above
structuer in a 16 bit machine
There is no correct answer. Depending on compiler settings, hardware
requirements and so on, it could be anything from seven bytes upwards.
Its impossible to say.

Why do you need to know?
--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
Jun 21 '07 #11
"bh********@gmail.com" wrote:
>
how much memory is allocated for following structure
struct bharath {
int b;
char c;
float d;
}
and how?
Exactly "sizeof(struct bjarath);", in bytes. How is system
dependant.

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>
<http://www.aaxnet.com/editor/edit043.html>
cbfalconer at maineline dot net

--
Posted via a free Usenet account from http://www.teranews.com

Jun 21 '07 #12
bh********@gmail.com wrote:
how much memory is allocated for following structure
struct bharath
{
int b;
char c;
float d;
}
This is, for some reason, a common newbie question. It doesn't need to
be. When it comes to struct sizes, there are only two things to keep in
mind:

1. The actual size is implementation-specific. That means it varies
from compiler to compiler, perhaps even with the same compiler
depending on options. There's no way to know from code inspection.

2. When you need to know the size in a program, the sizeof operator
will tell you. The actual value is of little importance.


Brian
Jun 21 '07 #13
bh********@gmail.com wrote:
how much memory is allocated for following structure
struct bharath
{
int b;
char c;
float d;
}
^^ ';' needed

None, until an instance of that structure is declared.
If you have one
struct bharath foo;
the space allocated is
sizeof foo
or
sizeof(struct bharath)
and is at least
sizeof(int)+sizeof(float)+1
and how?
With whatever mechanism your compiler allocates space for other variables.
Jun 21 '07 #14

<bh********@gmail.comha scritto nel messaggio news:11**********************@q19g2000prn.googlegr oups.com...
how much memory is allocated for following structure
struct bharath
{
int b;
char c;
float d;
}
None, because you are not declaring an object.

If you wrote
struct bharath {
int b;
char c;
float d;
} a;

It would allocate sizeof a bytes, i.e. sizeof(struct bharath)
bytes.
and how?
The same way it is allocated for any other object.
Jun 21 '07 #15

<bh********@gmail.comha scritto nel messaggio news:11*********************@q19g2000prn.googlegro ups.com...
On Jun 21, 7:49 pm, Clever Monkey <spamt...@clevermonkey.org.INVALID>
wrote:
>balu wrote:
On Jun 21, 7:33 pm, Richard Heathfield <r...@see.sig.invalidwrote:
bharath...@gmail.com said:
>>how much memory is allocated for following structure
struct bharath
{
int b;
char c;
float d;
}
None. It's a type, not an object.

[Please use a newsreader that understands how to trim sigs, or trim them
yourself. I've done this for you here.]
let the int size is 4B then the allocation will be?
i want to know the info regarding padding or some other else related
to padding?

This is exactly the sort of thing that people talk about when they say
something is implementation specific. That is, once you actually
allocate for a type, the specifics of how that memory is allocated is
not necessarily dictated by the Standard.

Simply defining (or is it declaring?) the type, of course, allocates no
memory.
--
clvrmnky <mailto:spamt...@clevermonkey.org>

Direct replies will be blacklisted. Replace "spamtrap" with my name to
contact me directly.

what i want actually is how much size will be allocated for above
structuer in a 16 bit machine
It doesn't even have to be the same on all 16-bit machines, or even
on the same machine with different compilers.
Jun 21 '07 #16
On Thu, 21 Jun 2007 11:29:36 -0400, Clever Monkey
<sp******@clevermonkey.org.INVALIDwrote in comp.lang.c:
bh********@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.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
Jun 21 '07 #17
"bh********@gmail.com" <bh********@gmail.comwrites:
how much memory is allocated for following structure
struct bharath
{
int b;
char c;
float d;
}

and how?
The comp.lang.c FAQ is at <http://www.c-faq.com/>. Section 2 covers
structures (as well as unions and enumerations). Read it. If you're
still confused, come back and ask again.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <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"
Jun 22 '07 #18
bh********@gmail.com wrote:
On Jun 21, 7:49 pm, Clever Monkey <spamt...@clevermonkey.org.INVALID>
wrote:
>balu wrote:
On Jun 21, 7:33 pm, Richard Heathfield <r...@see.sig.invalidwrote:
bharath...@gmail.com said:
>>how much memory is allocated for following structure
struct bharath
{
int b;
char c;
float d;
}
(fx:snipping)
what i want actually is how much size will be allocated for above
structuer in a 16 bit machine
It depends on the implementation.

Really.

A "16-bit machine" (and what /that/ means isn't unambiguous) might have
a C implementation which used 32-bit ints. Or 16-bit ints. It might have
32-bit floats. Or 64. Floats might need to be allocated on 16-bit boundaries.
Or 32-bit boundaries. Or 8-bit boundaries. chars might be 8 bits wide.
Or 16. You might be able to /select/ these things with, say, command-line
switches.

It depends on the implementation.

--
Chris "just like life, really" Dollin

Hewlett-Packard Limited registered no:
registered office: Cain Road, Bracknell, Berks RG12 1HN 690597 England

Jun 22 '07 #19
"bh********@gmail.com" <bh********@gmail.comwrote:
# 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
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.

--
SM Ryan http://www.rawbw.com/~wyrmwif/
I hope it feels so good to be right. There's nothing more
exhilarating pointing out the shortcomings of others, is there?
Jun 24 '07 #20
On Sun, 24 Jun 2007 00:10:45 -0000, SM Ryan
<wy*****@tango-sierra-oscar-foxtrot-tango.fake.orgwrote:
>"bh********@gmail.com" <bh********@gmail.comwrote:
# 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
Jun 24 '07 #21
>># 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.
Jun 24 '07 #22
Barry Schwarz <sc******@doezl.netwrites:
On Sun, 24 Jun 2007 00:10:45 -0000, SM Ryan
<wy*****@tango-sierra-oscar-foxtrot-tango.fake.orgwrote:
[...]
>> 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) ks***@mib.org <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"
Jun 24 '07 #23
On Sun, 24 Jun 2007 02:39:04 -0000, go***********@burditt.org (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
Jun 24 '07 #24
On Sat, 23 Jun 2007 19:54:37 -0700, Keith Thompson <ks***@mib.org>
wrote:
>Barry Schwarz <sc******@doezl.netwrites:
>On Sun, 24 Jun 2007 00:10:45 -0000, SM Ryan
<wy*****@tango-sierra-oscar-foxtrot-tango.fake.orgwrote:
[...]
>>> 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
Jun 24 '07 #25
Barry Schwarz <sc******@doezl.netwrites:
On Sat, 23 Jun 2007 19:54:37 -0700, Keith Thompson <ks***@mib.org>
wrote:
>>Barry Schwarz <sc******@doezl.netwrites:
>>On Sun, 24 Jun 2007 00:10:45 -0000, SM Ryan
<wy*****@tango-sierra-oscar-foxtrot-tango.fake.orgwrote:
[...]
>>>> 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) ks***@mib.org <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"
Jun 24 '07 #26
On Thu, 21 Jun 2007 16:22:20 -0500, Jack Klein <ja*******@spamcop.net>
wrote:
>On Thu, 21 Jun 2007 11:29:36 -0400, Clever Monkey
<sp******@clevermonkey.org.INVALIDwrote in comp.lang.c:
>bh********@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?

--
jay
Jun 24 '07 #27
On Jun 24, 1:38 am, jaysome <jays...@hotmail.comwrote:
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.INVALIDwrote 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.

Jun 24 '07 #28

"Keith Thompson" <ks***@mib.orgha scritto nel messaggio news:ln************@nuthaus.mib.org...
Barry Schwarz <sc******@doezl.netwrites:
>On Sat, 23 Jun 2007 19:54:37 -0700, Keith Thompson <ks***@mib.org>
wrote:
>>>Barry Schwarz <sc******@doezl.netwrites:
On Sun, 24 Jun 2007 00:10:45 -0000, SM Ryan
<wy*****@tango-sierra-oscar-foxtrot-tango.fake.orgwrote:
[...]
> 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.
Jun 24 '07 #29
On Sun, 24 Jun 2007 01:14:43 -0700, Keith Thompson <ks***@mib.org>
wrote:
>Barry Schwarz <sc******@doezl.netwrites:
>On Sat, 23 Jun 2007 19:54:37 -0700, Keith Thompson <ks***@mib.org>
wrote:
>>>Barry Schwarz <sc******@doezl.netwrites:
On Sun, 24 Jun 2007 00:10:45 -0000, SM Ryan
<wy*****@tango-sierra-oscar-foxtrot-tango.fake.orgwrote:
[...]
> 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
Jun 24 '07 #30
Army1987 wrote:
>
.... snip ...
>
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.
A long long requires <sizeof long long\bytes, not 11.

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>
<http://www.aaxnet.com/editor/edit043.html>
cbfalconer at maineline dot net

--
Posted via a free Usenet account from http://www.teranews.com

Jun 24 '07 #31
Keith Thompson wrote:
Barry Schwarz <sc******@doezl.netwrites:
>On Sun, 24 Jun 2007 00:10:45 -0000, SM Ryan
<wy*****@tango-sierra-oscar-foxtrot-tango.fake.orgwrote:
[...]
>>> 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.
I don't think so. First, sizeof (struct bharath) is 12 at my house. The
offsetof the members are 0, 4 and 8 regardless of their arrangement. If
the char member is the last it is still at offset 8 and sizeof the
struct is still 12. There can be no gap between the object and the next
object.
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).
There are no 'extra bytes' that you can find. That malloc might allocate
more than asked for is true but not at issue.

struct bharath *bhar = malloc(3 * sizeof *bhar);

If successful, this will allocate at least 36 bytes for three instances
of the struct. malloc may allocate more for its own purposes but we
can't know and don't care.

Or did I misunderstand you?

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Jun 24 '07 #32
CBFalconer wrote:
Army1987 wrote:
>>
... snip ...
>>
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.

A long long requires <sizeof long long\bytes, not 11.
A variable named LL requires sizeof LL bytes, and the lowercase equivalent
requires sizeof ll bytes.
Jun 24 '07 #33
CBFalconer <cb********@yahoo.comwrites:
Army1987 wrote:
>>
... snip ...
>>
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.

A long long requires <sizeof long long\bytes, not 11.
Chuck, if you can't tell the difference between "l" (lowercase L) and
"1" (digit one), get a better font. At least take a closer look
before assuming that somebody wrote something nonsensical like "sizeof
11". (Yes, "sizeof 11" is legal, but it makes no sense in context.)

--
Keith Thompson (The_Other_Keith) ks***@mib.org <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"
Jun 24 '07 #34
Joe Wright <jo********@comcast.netwrites:
Keith Thompson wrote:
>Barry Schwarz <sc******@doezl.netwrites:
>>On Sun, 24 Jun 2007 00:10:45 -0000, SM Ryan
<wy*****@tango-sierra-oscar-foxtrot-tango.fake.orgwrote:
[...]
>>>> 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.
I don't think so. First, sizeof (struct bharath) is 12 at my
house. The offsetof the members are 0, 4 and 8 regardless of their
arrangement. If the char member is the last it is still at offset 8
and sizeof the struct is still 12. There can be no gap between the
object and the next object.
Sure there can.

Suppose sizeof(struct bharath) is 12, and suppose you have another
type struct foo that requires 16-byte alignment. Then if you declare

struct bharath obj1;
struct foo obj2;

there easily *could* be a 4-byte gap between obj1 and obj2. Or the
compiler could reverse the order, or it could allocate another object
between obj1 and obj2, or whatever.

I'm not saying this is of any particular importance, just that it's
possible and plausible.

I'm sure you know this, and I suspect I'm misinterpreting your
statement that "There can be no gap between the object and the next
object."; can you clarify what you mean?
>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).
There are no 'extra bytes' that you can find. That malloc might
allocate more than asked for is true but not at issue.
Ok, so what exactly is the issue? I think I've lost track.

I think we all know what the answers are, and we're arguing over what
the question is, or perhaps what the question should be.

[...]

--
Keith Thompson (The_Other_Keith) ks***@mib.org <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"
Jun 24 '07 #35
Keith Thompson wrote:
Joe Wright <jo********@comcast.netwrites:
>Keith Thompson wrote:
>>Barry Schwarz <sc******@doezl.netwrites:
On Sun, 24 Jun 2007 00:10:45 -0000, SM Ryan
<wy*****@tango-sierra-oscar-foxtrot-tango.fake.orgwrote:
[...]
> 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.
I don't think so. First, sizeof (struct bharath) is 12 at my
house. The offsetof the members are 0, 4 and 8 regardless of their
arrangement. If the char member is the last it is still at offset 8
and sizeof the struct is still 12. There can be no gap between the
object and the next object.

Sure there can.

Suppose sizeof(struct bharath) is 12, and suppose you have another
type struct foo that requires 16-byte alignment. Then if you declare

struct bharath obj1;
struct foo obj2;

there easily *could* be a 4-byte gap between obj1 and obj2. Or the
compiler could reverse the order, or it could allocate another object
between obj1 and obj2, or whatever.
I wasn't thinking about a gap between obj1 and obj2. If there is such a
gap we don't care and I think we can't even test for it.
I'm not saying this is of any particular importance, just that it's
possible and plausible.

I'm sure you know this, and I suspect I'm misinterpreting your
statement that "There can be no gap between the object and the next
object."; can you clarify what you mean?
I was alluding to an array of the particular structure.
>>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).
There are no 'extra bytes' that you can find. That malloc might
allocate more than asked for is true but not at issue.

Ok, so what exactly is the issue? I think I've lost track.

I think we all know what the answers are, and we're arguing over what
the question is, or perhaps what the question should be.
Indeed. I got sideways on the 'extra bytes' thing.

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Jun 24 '07 #36
# >>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.

Sometimes a compiler will allocate variables in word sized units
to improve performance. As far as such compilers are concerned,
the variable is a whole number of words which can be greater
than the sizeof.

--
SM Ryan http://www.rawbw.com/~wyrmwif/
OOOOOOOOOO! NAVY SEALS!
Jun 25 '07 #37
On Jun 21, 10:33 am, Richard Heathfield <r...@see.sig.invalidwrote:
bharath...@gmail.com said:
how much memory is allocated for following structure
struct bharath
{
int b;
char c;
float d;
}

None. It's a type, not an object.
A newbie question: surely, the struct definition is stored somewhere.
Else, how would the struct be allocated at runtime? And this would
require some memory...

Regards,
Frodo B

Jun 25 '07 #38
On Mon, 25 Jun 2007 01:21:58 -0000, SM Ryan
<wy*****@tango-sierra-oscar-foxtrot-tango.fake.orgwrote:
># >>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.

Sometimes a compiler will allocate variables in word sized units
to improve performance. As far as such compilers are concerned,
the variable is a whole number of words which can be greater
than the sizeof.
No it cannot. The reason is that elements of an array must abut and
the byte difference between the start of one element and the next
element must be exactly sizeof element. If the compiler elects to pad
a structure out to a word boundary, then that padding is included the
value that sizeof evaluates to.
Remove del for email
Jun 25 '07 #39
Frodo Baggins said:
On Jun 21, 10:33 am, Richard Heathfield <r...@see.sig.invalidwrote:
>bharath...@gmail.com said:
how much memory is allocated for following structure
struct bharath
{
int b;
char c;
float d;
}

None. It's a type, not an object.

A newbie question: surely, the struct definition is stored somewhere.
Yes, the compiler will need to store the definition in its own internal
memory, but in those terms the question is unanswerable without
detailed internal knowledge of the compiler working. But that's not
normally what we mean when we say "how much memory".

Else, how would the struct be allocated at runtime?
It wouldn't be. It's a type, not an object. You don't allocate types.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Jun 25 '07 #40
Johan Bengtsson wrote, On 25/06/07 19:17:
Keith Thompson wrote:
>Ok, so what exactly is the issue? I think I've lost track.

I think we all know what the answers are, and we're arguing over what
the question is, or perhaps what the question should be.
<OT_HUMOR>42</OT_HUMOR>
That's the answer, not the question! Now we can't post the question here
without the universe being replaced by something even more incomprehensible!
(For those not understanding: you need to read Douglas Adams book
"hitchhikers guide to the galaxy")
The radio series is IMHO the best version :-)
Could not resist... sorry
Life, don't talk to me about life.
--
Marvin
I'll just go and stick my head in a bucket of water. I've got one here.
Jun 25 '07 #41
Flash Gordon <sp**@flash-gordon.me.ukwrote:
Johan Bengtsson wrote, On 25/06/07 19:17:
Keith Thompson wrote:
Ok, so what exactly is the issue? I think I've lost track.

I think we all know what the answers are, and we're arguing over what
the question is, or perhaps what the question should be.
<OT_HUMOR>42</OT_HUMOR>

That's the answer, not the question! Now we can't post the question here
without the universe being replaced by something even more incomprehensible!
That has already happened. In 1999, to be precise.

Richard
Jun 26 '07 #42
Richard Bos wrote, On 26/06/07 07:24:
Flash Gordon <sp**@flash-gordon.me.ukwrote:
>Johan Bengtsson wrote, On 25/06/07 19:17:
>>Keith Thompson wrote:
Ok, so what exactly is the issue? I think I've lost track.

I think we all know what the answers are, and we're arguing over what
the question is, or perhaps what the question should be.

<OT_HUMOR>42</OT_HUMOR>
That's the answer, not the question! Now we can't post the question here
without the universe being replaced by something even more incomprehensible!

That has already happened. In 1999, to be precise.
Yes, but we don't want the situation getting any worse!
--
Flash Gordon
Jun 26 '07 #43

"Keith Thompson" <ks***@mib.orgha scritto nel messaggio news:ln************@nuthaus.mib.org...
CBFalconer <cb********@yahoo.comwrites:
>Army1987 wrote:
>>>
... snip ...
>>>
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.

A long long requires <sizeof long long\bytes, not 11.

Chuck, if you can't tell the difference between "l" (lowercase L) and
"1" (digit one), get a better font. At least take a closer look
before assuming that somebody wrote something nonsensical like "sizeof
11". (Yes, "sizeof 11" is legal, but it makes no sense in context.)
And it is very unlikely to be 11 bytes.
Jun 28 '07 #44

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by Berk Birand | last post: by
4 posts views Thread by Thomas Paul Diffenbach | last post: by
18 posts views Thread by Peter Smithson | last post: by
1 post views Thread by Joe.ntang | last post: by
44 posts views Thread by svata | last post: by
23 posts views Thread by TefJlives | last post: by
19 posts views Thread by =?ISO-8859-1?Q?Nordl=F6w?= | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.