querry related to structure padding

L

Lalatendu Das

hi let's say i have a structure
struct test {
int A;
char B[5];
int C;
};
this above structure defination always going to take 16 byte in
memeory in whatever manner we align the member variables while
declaring a variable to it .
because variable 'A' going to take 4 byte then four charachter of
array gonna take another 4 bytes but the remaining will take also 4
bytes due to four byte alignment nature of compiler .
and last four byte by integer C .

what i want is there any way i can start storing integer C just after
the storing completion of array B's last element i.e. B[4] , so that i
can suppress padding . Any program which make memory manager store in
the above manner is most welcome.
thanks priorly because i am sure i am gonna get innumerable answer to
it .
 
R

Robert Gamble

Lalatendu said:
hi let's say i have a structure
struct test {
int A;
char B[5];
int C;
};
this above structure defination always going to take 16 byte in
memeory in whatever manner we align the member variables while
declaring a variable to it .
because variable 'A' going to take 4 byte then four charachter of
array gonna take another 4 bytes but the remaining will take also 4
bytes due to four byte alignment nature of compiler .
and last four byte by integer C .

what i want is there any way i can start storing integer C just after
the storing completion of array B's last element i.e. B[4] , so that i
can suppress padding . Any program which make memory manager store in
the above manner is most welcome.
thanks priorly because i am sure i am gonna get innumerable answer to
it .

No, not portably. See: http://www.c-faq.com/struct/padding.html

Robert Gamble
 
L

Lew Pitcher

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Lalatendu said:
hi let's say i have a structure
struct test {
int A;
char B[5];
int C;
};
this above structure defination always going to take 16 byte in
memeory in whatever manner we align the member variables while
declaring a variable to it .

Not on some platforms. It is conceivable that this structure will take up no
more than 6 bytes on some platforms, with or without tightly packed members.
because variable 'A' going to take 4 byte

Says who?
then four charachter of
array gonna take another 4 bytes but the remaining will take also 4
bytes due to four byte alignment nature of compiler .

Again, says who?
and last four byte by integer C .

Again, says who?

Byte size (in bits), int size (in bytes), and alignment requirements are
compiler and platform specific, and my compiler and your compiler may differ
in any and all of the specifics.
what i want is there any way i can start storing integer C just after
the storing completion of array B's last element i.e. B[4] , so that i
can suppress padding . Any program which make memory manager store in
the above manner is most welcome.

That would take knowledge of how your compiler works, and what sort of options
it gives you.

You /might/ gain some economy of storage by re-arranging the members within
the structure so as to take advantage of your compiler's built-in alignment
requirements. For instance,

struct test {
int A;
int C;
char B[5];
};

might actually take less space, even without compiler options, than the
organization you suggested.
thanks priorly because i am sure i am gonna get innumerable answer to
it .


- --
Lew Pitcher

Master Codewright & JOAT-in-training | GPG public key available on request
Registered Linux User #112576 (http://counter.li.org/)
Slackware - Because I know what I'm doing.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2.2 (GNU/Linux)

iD8DBQFEHNiWagVFX4UWr64RAmzbAKDfsX5onft4vltCCqjHxlQuJCAX+wCg9lMY
a71cSjn13xF0Klbb2khYa+A=
=BJZs
-----END PGP SIGNATURE-----
 
L

Lew Pitcher

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Lew said:
Lalatendu said:
hi let's say i have a structure
struct test {
int A;
char B[5];
int C;
};
this above structure defination always going to take 16 byte in
memeory in whatever manner we align the member variables while
declaring a variable to it .

Not on some platforms. It is conceivable that this structure will take up no
more than 6 bytes on some platforms, with or without tightly packed members.

Oops. Make that "no more than 7 bytes on some platforms"

- --
Lew Pitcher

Master Codewright & JOAT-in-training | GPG public key available on request
Registered Linux User #112576 (http://counter.li.org/)
Slackware - Because I know what I'm doing.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2.2 (GNU/Linux)

iD8DBQFEHNjfagVFX4UWr64RAgcLAKDwTDodfvOFbfsUI1pbOvL4hvriJgCgiluS
iylhlsuEYd0OxDex1os3KjI=
=gQEL
-----END PGP SIGNATURE-----
 
J

Jack Klein

hi let's say i have a structure
struct test {
int A;
char B[5];
int C;
};
this above structure defination always going to take 16 byte in
memeory in whatever manner we align the member variables while
declaring a variable to it .

No, you are completely wrong. It is going to occupy sizeof(struct
test) bytes in memory, no more and no less.

On two different compilers that I use that will be exactly 7 bytes. On
several other compilers that I use that will be 9 bytes, while on
others it will be 10.
because variable 'A' going to take 4 byte then four charachter of

Variable 'A' is going to occupy sizeof(int) bytes. On various
compilers that I use, that varies between 1 and 4 bytes. There may or
may not be padding bytes after 'A'.
array gonna take another 4 bytes but the remaining will take also 4
bytes due to four byte alignment nature of compiler .
and last four byte by integer C .

No, the array 'B' will not take 4 bytes and another 4 bytes, that is 8
bytes in total. Unlike everything else in your post, this is one
thing that is absolute. The array 'B' will occupy exactly 5 bytes, on
your compiler and on every C compiler that ever existed.

There might be padding bytes after 'B' before the start of the next
member. Perhaps that is what you are talking about. Those padding
bytes do not change the size of 'B', an array of 5 char will always
have a size of exactly 5 bytes.
what i want is there any way i can start storing integer C just after
the storing completion of array B's last element i.e. B[4] , so that i
can suppress padding . Any program which make memory manager store in
the above manner is most welcome.

What "memory manager"? There is no "memory manager" in C. The
compiler is allowed to insert padding after any member of a structure
to maintain alignment. On some hardware architectures, incorrect
alignment will cause a hardware trap that will shut down a program.
thanks priorly because i am sure i am gonna get innumerable answer to
it .

C does not define any mechanism for a programmer to override the
compiler's alignment decisions. Your particular compiler might
provide some non-standard mechanism to do this. You need to ask in a
compiler specific support group to find out if this is so, or study
your compiler's documentation. Even if such a non-standard mechanism
is available, it can significantly slow down the program on some
architectures.
 
S

Simon Biber

Lalatendu said:
hi let's say i have a structure
struct test {
int A;
char B[5];
int C;
};
this above structure defination always going to take 16 byte in
memeory in whatever manner we align the member variables while
declaring a variable to it .
because variable 'A' going to take 4 byte then four charachter of
array gonna take another 4 bytes but the remaining will take also 4
bytes due to four byte alignment nature of compiler .
and last four byte by integer C .

what i want is there any way i can start storing integer C just after
the storing completion of array B's last element i.e. B[4] , so that i
can suppress padding . Any program which make memory manager store in
the above manner is most welcome.

Ok, declare your struct like this:

struct test {
int A;
char B[5];
unsigned char C[sizeof (int)];
};

To read an int out of the non-aligned area, use this function:

int get_nonaligned_int(const unsigned char *b)
{
int i, temp;
unsigned char *a = (unsigned char *)&temp;

for(i = 0; i < sizeof temp; i++)
a = b;

return temp;
}

To store an int into the non-aligned area, use this function:

void set_nonaligned_int(unsigned char *b, int value)
{
int i;
unsigned char *a = (unsigned char *)&value;

for(i = 0; i < sizeof value; i++)
b = a;
}

For example:

int main(void)
{
struct test foo;

foo.A = 13;
strcpy(foo.B, "abcd");
set_nonaligned_int(foo.C, 42);

return 0;
}
 
K

Keith Thompson

Lalatendu Das said:
hi let's say i have a structure
struct test {
int A;
char B[5];
int C;
};
this above structure defination always going to take 16 byte in
memeory in whatever manner we align the member variables while
declaring a variable to it .
because variable 'A' going to take 4 byte then four charachter of
array gonna take another 4 bytes but the remaining will take also 4
bytes due to four byte alignment nature of compiler .
and last four byte by integer C .

Not necessarily. The size and alignment of type int are
implementation-specific.
what i want is there any way i can start storing integer C just after
the storing completion of array B's last element i.e. B[4] , so that i
can suppress padding . Any program which make memory manager store in
the above manner is most welcome.
thanks priorly because i am sure i am gonna get innumerable answer to
it .

<http://www.c-faq.com/>, question 2.12.
 
R

Robert Gamble

Lew said:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Lalatendu said:
hi let's say i have a structure
struct test {
int A;
char B[5];
int C;
};
this above structure defination always going to take 16 byte in
memeory in whatever manner we align the member variables while
declaring a variable to it .

Not on some platforms. It is conceivable that this structure will take up no
more than 6 bytes on some platforms, with or without tightly packed members.
because variable 'A' going to take 4 byte

Says who?
then four charachter of
array gonna take another 4 bytes but the remaining will take also 4
bytes due to four byte alignment nature of compiler .

Again, says who?
and last four byte by integer C .

Again, says who?

Byte size (in bits), int size (in bytes), and alignment requirements are
compiler and platform specific, and my compiler and your compiler may differ
in any and all of the specifics.
what i want is there any way i can start storing integer C just after
the storing completion of array B's last element i.e. B[4] , so that i
can suppress padding . Any program which make memory manager store in
the above manner is most welcome.

That would take knowledge of how your compiler works, and what sort of options
it gives you.

You /might/ gain some economy of storage by re-arranging the members within
the structure so as to take advantage of your compiler's built-in alignment
requirements. For instance,

struct test {
int A;
int C;
char B[5];
};

might actually take less space, even without compiler options, than the
organization you suggested.

Assuming ints are 4 bytes and require 4-byte alignment (as indicated by
the OP) I do not see how this could possibly take less space.

Robert Gamble
 
R

Robert Gamble

Simon said:
Lalatendu said:
hi let's say i have a structure
struct test {
int A;
char B[5];
int C;
};
this above structure defination always going to take 16 byte in
memeory in whatever manner we align the member variables while
declaring a variable to it .
because variable 'A' going to take 4 byte then four charachter of
array gonna take another 4 bytes but the remaining will take also 4
bytes due to four byte alignment nature of compiler .
and last four byte by integer C .

what i want is there any way i can start storing integer C just after
the storing completion of array B's last element i.e. B[4] , so that i
can suppress padding . Any program which make memory manager store in
the above manner is most welcome.

Ok, declare your struct like this:

struct test {
int A;
char B[5];
unsigned char C[sizeof (int)];
};

If the original structure took 16 bytes on the OP's implementation I
find it very difficult to believe that your suggestion would change the
storage required since it would still need 3 bytes of padding on the
end to maintain the alignment requirements of A (which is required to
support an array of struct test).

Robert Gamble
 
S

Simon Biber

Robert said:
Simon said:
Lalatendu said:
hi let's say i have a structure
struct test {
int A;
char B[5];
int C;
};
this above structure defination always going to take 16 byte in
memeory in whatever manner we align the member variables while
declaring a variable to it .
because variable 'A' going to take 4 byte then four charachter of
array gonna take another 4 bytes but the remaining will take also 4
bytes due to four byte alignment nature of compiler .
and last four byte by integer C .

what i want is there any way i can start storing integer C just after
the storing completion of array B's last element i.e. B[4] , so that i
can suppress padding . Any program which make memory manager store in
the above manner is most welcome.

Ok, declare your struct like this:

struct test {
int A;
char B[5];
unsigned char C[sizeof (int)];
};


If the original structure took 16 bytes on the OP's implementation I
find it very difficult to believe that your suggestion would change the
storage required since it would still need 3 bytes of padding on the
end to maintain the alignment requirements of A (which is required to
support an array of struct test).

That's true. If you want to eliminate the padding, use the same trick
for element A.

struct test {
unsigned char A[sizeof (int)];
char B[5];
unsigned char C[sizeof (int)];
};
 
S

santosh

Jack said:
hi let's say i have a structure
struct test {
int A;
char B[5];
int C;
};
this above structure defination always going to take 16 byte in
memeory in whatever manner we align the member variables while
declaring a variable to it .

No, you are completely wrong. It is going to occupy sizeof(struct
test) bytes in memory, no more and no less.

On two different compilers that I use that will be exactly 7 bytes. On
several other compilers that I use that will be 9 bytes, while on
others it will be 10.
because variable 'A' going to take 4 byte then four charachter of

Variable 'A' is going to occupy sizeof(int) bytes. On various
compilers that I use, that varies between 1 and 4 bytes. There may or
may not be padding bytes after 'A'.
array gonna take another 4 bytes but the remaining will take also 4
bytes due to four byte alignment nature of compiler .
and last four byte by integer C .

No, the array 'B' will not take 4 bytes and another 4 bytes, that is 8
bytes in total. Unlike everything else in your post, this is one
thing that is absolute. The array 'B' will occupy exactly 5 bytes, on
your compiler and on every C compiler that ever existed.

There might be padding bytes after 'B' before the start of the next
member. Perhaps that is what you are talking about. Those padding
bytes do not change the size of 'B', an array of 5 char will always
have a size of exactly 5 bytes.

Dumb question:

A "byte" need not be 8 bits in size, though it is under PCs right?
 
I

Ian Collins

Simon said:
Robert said:
Simon said:
Lalatendu Das wrote:

hi let's say i have a structure
struct test {
int A;
char B[5];
int C;
};
this above structure defination always going to take 16 byte in
memeory in whatever manner we align the member variables while
declaring a variable to it .
because variable 'A' going to take 4 byte then four charachter of
array gonna take another 4 bytes but the remaining will take also 4
bytes due to four byte alignment nature of compiler .
and last four byte by integer C .

what i want is there any way i can start storing integer C just after
the storing completion of array B's last element i.e. B[4] , so that i
can suppress padding . Any program which make memory manager store in
the above manner is most welcome.


Ok, declare your struct like this:

struct test {
int A;
char B[5];
unsigned char C[sizeof (int)];
};



If the original structure took 16 bytes on the OP's implementation I
find it very difficult to believe that your suggestion would change the
storage required since it would still need 3 bytes of padding on the
end to maintain the alignment requirements of A (which is required to
support an array of struct test).


That's true. If you want to eliminate the padding, use the same trick
for element A.

struct test {
unsigned char A[sizeof (int)];
char B[5];
unsigned char C[sizeof (int)];
};
So how would you (portably) access A or C as an int?

Remember some architectures (Sparc for instance) fault at misaligned
access, and others will incur a significant runtime penalty.
 
K

Keith Thompson

santosh said:
Dumb question:

A "byte" need not be 8 bits in size, though it is under PCs right?

Right. A byte is CHAR_BIT bits; CHAR_BIT is required to be at least 8.
 
C

CBFalconer

Lew said:
Lalatendu said:
hi let's say i have a structure
struct test {
int A;
char B[5];
int C;
};
this above structure defination always going to take 16 byte in
memeory in whatever manner we align the member variables while
declaring a variable to it .

Not on some platforms. It is conceivable that this structure will
take up no more than 6 bytes on some platforms, with or without
tightly packed members.

No, with a CHAR_BIT of 16 it could conceivably take as little as 7
bytes, but with any smaller CHAR_BIT it will require at least 9
bytes. I would then expect at least one byte of padding for most
systems, making the expected minimum size 10. The more usual
systems will require 16 bytes.

--
Some informative links:
http://www.geocities.com/nnqweb/
http://www.catb.org/~esr/faqs/smart-questions.html
http://www.caliburn.nl/topposting.html
http://www.netmeister.org/news/learn2quote.html
 
R

Robert Gamble

Ian said:
Simon said:
Robert said:
Simon Biber wrote:

Lalatendu Das wrote:

hi let's say i have a structure
struct test {
int A;
char B[5];
int C;
};
this above structure defination always going to take 16 byte in
memeory in whatever manner we align the member variables while
declaring a variable to it .
because variable 'A' going to take 4 byte then four charachter of
array gonna take another 4 bytes but the remaining will take also 4
bytes due to four byte alignment nature of compiler .
and last four byte by integer C .

what i want is there any way i can start storing integer C just after
the storing completion of array B's last element i.e. B[4] , so that i
can suppress padding . Any program which make memory manager store in
the above manner is most welcome.


Ok, declare your struct like this:

struct test {
int A;
char B[5];
unsigned char C[sizeof (int)];
};



If the original structure took 16 bytes on the OP's implementation I
find it very difficult to believe that your suggestion would change the
storage required since it would still need 3 bytes of padding on the
end to maintain the alignment requirements of A (which is required to
support an array of struct test).


That's true. If you want to eliminate the padding, use the same trick
for element A.

struct test {
unsigned char A[sizeof (int)];
char B[5];
unsigned char C[sizeof (int)];
};
So how would you (portably) access A or C as an int?

Simon provided a pair of functions for accessing A and C in his
original post which I responded to.
Remember some architectures (Sparc for instance) fault at misaligned
access, and others will incur a significant runtime penalty.

Robert Gamble
 
L

Lew Pitcher

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Robert said:
Lew Pitcher wrote: [snip]
struct test {
int A;
int C;
char B[5];
};

might actually take less space, even without compiler options, than the
organization you suggested.

Assuming ints are 4 bytes and require 4-byte alignment (as indicated by
the OP) I do not see how this could possibly take less space.

A starts on an int boundary, and takes up an int's worth of space. This means
that C now starts on an int boundary, and there is no padding between A and C.
Assuming that char alignment can be on a char boundary, then there is no
padding between C and B either. So, with this layout, we get a structure
without padding between it's members.

OTOH, with the original structure,
struct test {
int A;
char B[5];
int C;
};

there may be padding between the B and C members, in order to ensure that the
C member is aligned on an int boundary.

Thus, the alternative layout I suggested /can/ be smaller (because of less
padding) than the original layout.


- --
Lew Pitcher

Master Codewright & JOAT-in-training | GPG public key available on request
Registered Linux User #112576 (http://counter.li.org/)
Slackware - Because I know what I'm doing.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2.2 (GNU/Linux)

iD8DBQFEHX2BagVFX4UWr64RAiJeAKC5gga/8y0lbqXuRP7ZnZDxca0FwgCfbRVW
SXjEPPl3AWGwsq/ahL3IJOY=
=YTg7
-----END PGP SIGNATURE-----
 
R

Robert Gamble

Lew said:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Robert said:
Lew Pitcher wrote: [snip]
struct test {
int A;
int C;
char B[5];
};

might actually take less space, even without compiler options, than the
organization you suggested.

Assuming ints are 4 bytes and require 4-byte alignment (as indicated by
the OP) I do not see how this could possibly take less space.

A starts on an int boundary, and takes up an int's worth of space. This means
that C now starts on an int boundary, and there is no padding between A and C.
Assuming that char alignment can be on a char boundary, then there is no
padding between C and B either. So, with this layout, we get a structure
without padding between it's members.

There may not be padding *between* the members but there is going to be
padding after C which, in this case, will be the same amount of padding
that existed between B and C in the original structure.

Robert Gamble
 
C

CBFalconer

Robert said:
.... snip ...

There may not be padding *between* the members but there is going
to be padding after C which, in this case, will be the same amount
of padding that existed between B and C in the original structure.

The only reason for this reply is that the misspelling in the
subject has been driving me mad. Mad, as in insane, or totally
discombobulated, I tell you.

I rejected quarry and quail as possible corrections.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
Also see <http://www.safalra.com/special/googlegroupsreply/>
 
K

Keith Thompson

Simon Biber said:
Lalatendu said:
hi let's say i have a structure
struct test {
int A;
char B[5];
int C;
};
this above structure defination always going to take 16 byte in
memeory in whatever manner we align the member variables while
declaring a variable to it .
because variable 'A' going to take 4 byte then four charachter of
array gonna take another 4 bytes but the remaining will take also 4
bytes due to four byte alignment nature of compiler .
and last four byte by integer C .
what i want is there any way i can start storing integer C just after
the storing completion of array B's last element i.e. B[4] , so that i
can suppress padding . Any program which make memory manager store in
the above manner is most welcome.

Ok, declare your struct like this:

struct test {
int A;
char B[5];
unsigned char C[sizeof (int)];
};
[...]

The compiler is still free to insert padding between B and C, or after
C, either because it can make the code potentially more efficient (for
example, memcpy() might be faster for aligned character arrays) or
because the compiler writer was in a funny mood that day.

If you want to fully control the layout of a structure, declare the
whole thing as an array of unsigned char, define the offsets and sizes
yourself, and write your own code to extract whatever data you need.
That's the only truly portable way to match an externally defined data
layout (assuming consistent byte sizes, and assuming you're dealing
correctly with byte ordering).

Or you can use some compiler-specific tricks to control the layout
(which will almost certainly result in slower code).

But the real question (to which I don't recall seeing an answer) is
*why* the OP wants to eliminate padding between the structure members.
The compiler inserts that padding for very good reasons; are you sure
that you know better than the compiler does how the structure should
be laid out? Quite possibly you do, in which case you'll need to do
something non-portable and/or ugly -- but if you just have a general
idea that you want to save space, consider letting the compiler do its
job.
 
B

Ben C

hi let's say i have a structure
struct test {
int A;
char B[5];
int C;
};
[...] what i want is there any way i can start storing integer C just
after the storing completion of array B's last element i.e. B[4] , so
that i can suppress padding.

#pragma pack(1)
struct test
{
int A;
char B[5];
int C;
};
#pragma pack()

will do what you want; this works with at least gcc 4, some Microsoft
compilers and perhaps some others.

It is not standard or portable, and everything everyone else has said
about asking yourself why you really want this goes.

Also, watch out for address alignment problems if you do this kind of
thing. Compilers have good reasons for padding structs!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top