473,385 Members | 1,958 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Question about size and memory layout of a Union.

Hi!

I have stumbled on the following C question and I am wondering wether
the answer is implementation specific or if the answer is always
valid, especially question b).

BRs!

----------------------------------------------------
Following C code runs on a "little endian" machine that has a 32-bit
word alignment for memory access. An unsigned integer is 32 bit long.
struct A
{
unsigned int a1;
char a2;
};

union B
{
unsigned char d[10];
struct A a;
};
union B b;

memset((void*)&b, 0xff, sizeof(b));
memset((void*)&(b.a), 0, sizeof(b.a));
b.a.a1 = 0x01020304;
b.a.a2 = 0x5;

a) What is the size of variable b?
Answer: 12
b) Write down the memory dump of b in the form of hex numbers,
starting from lower address.
Answer: 0x4 0x3 0x2 0x1 0x5 0x0 0x0 0x0 0xff 0xff 0xff 0xff
----------------------------------------------------

Oct 15 '07 #1
9 5678
dspfun said:
Hi!

I have stumbled on the following C question and I am wondering wether
the answer is implementation specific or if the answer is always
valid, especially question b).
It's implementation-specific in both cases.
Following C code runs on a "little endian" machine that has a 32-bit
word alignment for memory access. An unsigned integer is 32 bit long.
struct A
{
unsigned int a1;
char a2;
};
The implementation can put an arbitrary number of padding bytes after any
structure member. There's nothing to stop the implementation putting 32
bits of padding between a1 and a2 if it likes.
>
union B
{
unsigned char d[10];
struct A a;
};
union B b;

memset((void*)&b, 0xff, sizeof(b));
memset((void*)&(b.a), 0, sizeof(b.a));
The cast is unnecessary in both cases.
b.a.a1 = 0x01020304;
b.a.a2 = 0x5;

a) What is the size of variable b?
sizeof b
Answer: 12
<shrugMaybe, but not necessarily, whereas sizeof b is definitely right.
b) Write down the memory dump of b in the form of hex numbers,
starting from lower address.
unsigned char *p = (unsigned char *)*b;
size_t n = sizeof b;
while(n--)
{
printf(" 0x%x", *p++);
}
putchar('\n');
Answer: 0x4 0x3 0x2 0x1 0x5 0x0 0x0 0x0 0xff 0xff 0xff 0xff
Maybe, but maybe not. The Standard does not guarantee this.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Oct 15 '07 #2
On 15 Okt, 09:21, Richard Heathfield <r...@see.sig.invalidwrote:
It's implementation-specific in both cases.
Thank you for great answers!

Just to make sure:
The question itself specifies that the machine is little endian and
has a 32-bit word alignment for memory access and an unsigned integer
is 32 bit long. Does that make the answers explicit/valid, or are the
answers still implementation specific?

BRs!

Oct 15 '07 #3
dspfun said:
On 15 Okt, 09:21, Richard Heathfield <r...@see.sig.invalidwrote:
>It's implementation-specific in both cases.

Thank you for great answers!

Just to make sure:
The question itself specifies that the machine is little endian and
has a 32-bit word alignment for memory access and an unsigned integer
is 32 bit long. Does that make the answers explicit/valid, or are the
answers still implementation specific?
Well, your answers demonstrate one reasonable response to the code by an
implementation on such a platform. Possibly the *most* reasonable
response. Unfortunately for the purpose of your question, the C Standard
does not require that implementations must be reasonable; it requires only
that they act in accordance with the Standard. And it is certainly
possible - even easy - to imagine answers different to yours that are
still in accordance with the Standard, even on such hardware as you
specify.

Let's just say that I'd be - um... aha! - surprised to learn that an
implementation that targets your platform does so in a way different to
what you clearly expect, unless specifically instructed so to do.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Oct 15 '07 #4
On Mon, 15 Oct 2007 07:21:47 +0000,
Richard Heathfield <rj*@see.sig.invalidwrote:
dspfun said:
>union B
{
unsigned char d[10];
struct A a;
};
union B b;
>b) Write down the memory dump of b in the form of hex numbers,
starting from lower address.

unsigned char *p = (unsigned char *)*b;
ITYM

unsigned char *p = (unsigned char *)&b;

Martien
--
|
Martien Verbruggen | Freudian slip: when you say one thing but
| mean your mother.
|
Oct 15 '07 #5
Martien Verbruggen said:
On Mon, 15 Oct 2007 07:21:47 +0000,
Richard Heathfield <rj*@see.sig.invalidwrote:
>dspfun said:
>>union B
{
unsigned char d[10];
struct A a;
};
union B b;
>>b) Write down the memory dump of b in the form of hex numbers,
starting from lower address.

unsigned char *p = (unsigned char *)*b;

ITYM

unsigned char *p = (unsigned char *)&b;
Er, yes, I do, don't I? :-)

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Oct 15 '07 #6
Just to make sure:
The question itself specifies that the machine is little endian and
has a 32-bit word alignment for memory access and an unsigned integer
is 32 bit long. Does that make the answers explicit/valid, or are the
answers still implementation specific?

Well, your answers demonstrate one reasonable response to the code by an
implementation on such a platform. Possibly the *most* reasonable
response. Unfortunately for the purpose of your question, the C Standard
does not require that implementations must be reasonable; it requires only
that they act in accordance with the Standard. And it is certainly
possible - even easy - to imagine answers different to yours that are
still in accordance with the Standard, even on such hardware as you
specify.
Thanks again for your answers!

Could you give some "easy" examples of how the answers could be
different while still running on the specified hardware?

BRs!

Oct 18 '07 #7
On Thu, 18 Oct 2007 04:26:19 -0700, in comp.lang.c , dspfun
<ds****@hotmail.comwrote:
Just to make sure:
The question itself specifies that the machine is little endian and
has a 32-bit word alignment for memory access and an unsigned integer
is 32 bit long. Does that make the answers explicit/valid, or are the
answers still implementation specific?

Well, your answers demonstrate one reasonable response to the code by an
implementation on such a platform. Possibly the *most* reasonable
response. Unfortunately for the purpose of your question, the C Standard
does not require that implementations must be reasonable; it requires only
that they act in accordance with the Standard. And it is certainly
possible - even easy - to imagine answers different to yours that are
still in accordance with the Standard, even on such hardware as you
specify.

Thanks again for your answers!

Could you give some "easy" examples of how the answers could be
different while still running on the specified hardware?
Many compilers offer a pragma "pack" which alters how memory is laid
out in unions and structs.
A 16-bit compiler might give a different answer from a 32-bit compiler
on the same hardware (ISTR that MSVC 1.5 and MSVC 4.0 managed this).

Further discussion is probably offtopic here.


--
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
Oct 18 '07 #8
On Oct 18, 12:26 pm, dspfun <dsp...@hotmail.comwrote:
Just to make sure:
The question itself specifies that the machine is little endian and
has a 32-bit word alignment for memory access and an unsigned integer
is 32 bit long. Does that make the answers explicit/valid, or are the
answers still implementation specific?
Well, your answers demonstrate one reasonable response to the code by an
implementation on such a platform. Possibly the *most* reasonable
response. Unfortunately for the purpose of your question, the C Standard
does not require that implementations must be reasonable; it requires only
that they act in accordance with the Standard. And it is certainly
possible - even easy - to imagine answers different to yours that are
still in accordance with the Standard, even on such hardware as you
specify.

Thanks again for your answers!

Could you give some "easy" examples of how the answers could be
different while still running on the specified hardware?
The compiler might put 37 bytes of padding between the two members of
struct A. That would be an odd thing for it to do, but it would be
perfectly valid and fully compliant with the C Standard.

Oct 18 '07 #9
Thank you for your answers!

To try to conclude by refering to the sections of the C-standard I
beleive question a) above is implementation defined due to the
following text in the C-standard:
---------------------------------------------
6.5.3.4 The sizeof operator
4. The value of the result is implementation-defined, and its type (an
unsigned integer type) is size_t, defined in <stddef.h(and other
headers).
---------------------------------------------

Question b) is implementation defined due to the following text in the
C-standard:
----------------------------------------------
6.2.6.1 General
6 When a value is stored in an object of structure or union type,
including in a member object, the bytes of the object representation
that correspond to any padding bytes take unspecified values.42) The
value of a structure or union object is never a trap representation,
even though the value of a member of the structure or union object may
be
a trap representation.

7 When a value is stored in a member of an object of union type, the
bytes of the object representation that do not correspond to that
member but do correspond to other members take unspecified values.

6.7.2.1 Structure and union specifiers
15. There may be unnamed padding at the end of a structure or union.
----------------------------------------------

Do you agree?

BRs!

Oct 19 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

36
by: Bhalchandra Thatte | last post by:
I am allocating a block of memory using malloc. I want to use it to store a "header" structure followed by structs in my application. How to calculate the alignment without making any assumption...
9
by: Alfonso Morra | last post by:
Hi, I am having some probs with copying memory blocks around (part of a messaging library) and I would like some confirmation to make sure that I'm going about things the right way. I have...
10
by: Bonj | last post by:
I almost understand TSTs, to the point where I just need to know the answer to this: When making a TST (in C++) that will have as its leaf nodes words that make up SQL language and an categorising...
19
by: lawtrevor | last post by:
I have a question regarding the use of free() based on some code I need to decipher: { struct A {<A fields>}; struct B {<A fields+ <Bfields>}; typedef struct A Aobj; typedef struct B Bobj;
30
by: Brian | last post by:
I am using Borland C++ Builder 5 for my application. When I build my application, it states that I have several if-statements that will always be false. The statements appear correct, so could...
28
by: kyle york | last post by:
Greetings, Why does the C standard require the members of a structure not be re-ordered (6.2.5.20)? Padding is allowed, and platform dependent, which means one cannot rely on the exact layout...
2
by: somenath | last post by:
Hi All, I have one question regarding the alignment of pointer returned by malloc. In K&R2 page number 186 one union is used to enforce the alignment as mentioned bellow. typedef long...
4
by: hugo.arregui | last post by:
Hi! I have two struts like that: struct { int num; int num2; struct b arrayOfB; } a;
5
by: =?Utf-8?B?SmVzc2ljYQ==?= | last post by:
Hello, I have a pInvoke question. This is the C function that is exported from one of the C dll, extern __declspec(dllexport) IM_RET_CODE ST_import (IM_MODE mode, char *filename,...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.