473,471 Members | 1,905 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Order of data member in a structure

When we have a structure in the following form

typedef struct {
int I1;
int I2;
int I3;
int I4;
float F1;
float F2;
float F3;
float F4;
} TMyStru;

and a code segment as follows;

TMyStru D;
D.I1=0; D.I2=1; D.I3=2; D.I4=3;
D.F1=4.0; D.F2=4.0; D.F3=6.0; D.F4=7.0;
unsigned char *Buf = new unsigned char [sizeof(TMyStru)];
memcpy(Buf, &D, sizorf(TMyStru));
Does "Buf" ALWAYS (regardless of a particular C/C++ compiler) contains the
content of "D" in I1,I2,I3,I4,F1,F2,F3,F4 order ?
ie I1 appears infront of I2 ...
Jul 22 '05 #1
14 1994
posted:
When we have a structure in the following form

typedef struct {
int I1;
int I2;
int I3;
int I4;
float F1;
float F2;
float F3;
float F4;
} TMyStru;

and a code segment as follows;

TMyStru D;
D.I1=0; D.I2=1; D.I3=2; D.I4=3;
D.F1=4.0; D.F2=4.0; D.F3=6.0; D.F4=7.0;
unsigned char *Buf = new unsigned char [sizeof(TMyStru)];
memcpy(Buf, &D, sizorf(TMyStru));
Does "Buf" ALWAYS (regardless of a particular C/C++ compiler) contains the content of "D" in I1,I2,I3,I4,F1,F2,F3,F4 order ?
ie I1 appears infront of I2 ...

Yes it does.

Just so you know, you don't need to use the heap:

unsigned char Buf[sizeof(TMyStru)];
Only thing to be concerned about is padding bits. On some
systems, some types of variables need to be aligned on a 4
byte boundary in memory. So for your structure, you may
have the following in memory:

---------
| int |
---------
---------
| int |
---------
---------
| int |
---------
---------
| int |
---------
---------
|padding|
---------
---------
| float |
---------
---------
| float |
---------
---------
| float |
---------
---------
| float |
---------

So when you copy or over overwrite the padding bits, you're
accessing memory that isn't yours. For instance, the system
may utilize those bits for some other variable, like so:

TMyStru blah;
char kk[4];

Now the system may stick kk into the vacant spot:

---------
| int |
---------
---------
| int |
---------
---------
| int |
---------
---------
| int |
---------
---------
| kk |
---------
---------
| float |
---------
---------
| float |
---------
---------
| float |
---------
---------
| float |
---------
I don't know if there's any rules as to where and when you
can expect padding, but I do know that your always safe
with:

unsigned char
char
signed char

and arrays of them.
-JKop
Jul 22 '05 #2

"JKop" <NU**@NULL.NULL> wrote in message
news:eW*****************@news.indigo.ie...
posted:
When we have a structure in the following form

typedef struct {
int I1;
int I2;
int I3;
int I4;
float F1;
float F2;
float F3;
float F4;
} TMyStru;

and a code segment as follows;

TMyStru D;
D.I1=0; D.I2=1; D.I3=2; D.I4=3;
D.F1=4.0; D.F2=4.0; D.F3=6.0; D.F4=7.0;
unsigned char *Buf = new unsigned char [sizeof(TMyStru)];
memcpy(Buf, &D, sizorf(TMyStru));
Does "Buf" ALWAYS (regardless of a particular C/C++

compiler) contains
the content of "D" in I1,I2,I3,I4,F1,F2,F3,F4 order ?
ie I1 appears infront of I2 ...

Yes it does.

Just so you know, you don't need to use the heap:

unsigned char Buf[sizeof(TMyStru)];
Only thing to be concerned about is padding bits. On some
systems, some types of variables need to be aligned on a 4
byte boundary in memory. So for your structure, you may
have the following in memory:

---------
| int |
---------
---------
| int |
---------
---------
| int |
---------
---------
| int |
---------
---------
|padding|
---------
---------
| float |
---------
---------
| float |
---------
---------
| float |
---------
---------
| float |
---------

So when you copy or over overwrite the padding bits, you're
accessing memory that isn't yours. For instance, the system
may utilize those bits for some other variable, like so:

TMyStru blah;
char kk[4];

Now the system may stick kk into the vacant spot:

---------
| int |
---------
---------
| int |
---------
---------
| int |
---------
---------
| int |
---------
---------
| kk |
---------
---------
| float |
---------
---------
| float |
---------
---------
| float |
---------
---------
| float |
---------


Yikes!! I hope that isn't true. I know about padding but that the first time
anyone has said that the compiler can use that padding for other purposes,
rather than just leaving it unused.

In fact I'm pretty sure it can't be true, because it would mean the memset
would affect an unrelated variable, making memset unusable on POD types
(unless they happen to be char or arrays of char). If that really is true
then the standard is stupider than I thought.

john
Jul 22 '05 #3
John Harrison posted:

"JKop" <NU**@NULL.NULL> wrote in message
news:eW*****************@news.indigo.ie...
posted:
> When we have a structure in the following form
>
> typedef struct {
> int I1;
> int I2;
> int I3;
> int I4;
> float F1;
> float F2;
> float F3;
> float F4;
> } TMyStru;
>
> and a code segment as follows;
>
> TMyStru D;
> D.I1=0; D.I2=1; D.I3=2; D.I4=3;
> D.F1=4.0; D.F2=4.0; D.F3=6.0; D.F4=7.0;
> unsigned char *Buf = new unsigned char [sizeof (TMyStru)]; > memcpy(Buf, &D, sizorf(TMyStru));
>
>
> Does "Buf" ALWAYS (regardless of a particular C/C++ compiler) > contains the content of "D" in I1,I2,I3,I4,F1,F2,F3,F4 order ? > ie I1 appears infront of I2 ...

Yes it does.

Just so you know, you don't need to use the heap:

unsigned char Buf[sizeof(TMyStru)];
Only thing to be concerned about is padding bits. On some systems, some types of variables need to be aligned on a 4 byte boundary in memory. So for your structure, you may
have the following in memory:

---------
| int |
---------
---------
| int |
---------
---------
| int |
---------
---------
| int |
---------
---------
|padding|
---------
---------
| float |
---------
---------
| float |
---------
---------
| float |
---------
---------
| float |
---------

So when you copy or over overwrite the padding bits, you're accessing memory that isn't yours. For instance, the system may utilize those bits for some other variable, like so:

TMyStru blah;
char kk[4];

Now the system may stick kk into the vacant spot:

---------
| int |
---------
---------
| int |
---------
---------
| int |
---------
---------
| int |
---------
---------
| kk |
---------
---------
| float |
---------
---------
| float |
---------
---------
| float |
---------
---------
| float |
---------


Yikes!! I hope that isn't true. I know about padding but

that the first time anyone has said that the compiler can use that padding for other purposes, rather than just leaving it unused.

In fact I'm pretty sure it can't be true, because it would mean the memset would affect an unrelated variable, making memset unusable on POD types (unless they happen to be char or arrays of char). If that really is true then the standard is stupider than I thought.
john

What I'm getting it is that there's nothing in the Standard
that says that that *can't* be done, and for example in the
following:

struct Cow
{
int a;
char b;
int c;
};

I think it would be perfectly reasonable to actually make
use of the vacant space between b and c, which is probably
2 bytes, and so:

int main()
{
Cow blah;

char jack[2];
}

---------
| a |
---------
---------
| b |
---------
---------
|jack[2]|
---------
---------
| c |
---------
C++ contains unions, so why wouldn't it do the above?
-JKop
Jul 22 '05 #4
>
What I'm getting it is that there's nothing in the Standard
that says that that *can't* be done, and for example in the
following:

struct Cow
{
int a;
char b;
int c;
};

I think it would be perfectly reasonable to actually make
use of the vacant space between b and c, which is probably
2 bytes, and so:

int main()
{
Cow blah;

char jack[2];
}

---------
| a |
---------
---------
| b |
---------
---------
|jack[2]|
---------
---------
| c |
---------
C++ contains unions, so why wouldn't it do the above?
-JKop


I don't know what the standard says, but I think any compiler manufacturer
that actually did that would have trouble selling their compilers.

Are you confident that the standard allows this?

john
Jul 22 '05 #5
John Harrison posted:
I don't know what the standard says, but I think any compiler manufacturer that actually did that would have trouble selling their compilers.
Well, from experience working with with MSWindows, they
always use the likes of:

SOMESTUPIDSTRUCTURE blah;
memset(&blah,0,sizeof(blah));
But we're not talking about a competent company here, so
that's a moot point.

Are you confident that the standard allows this?

Confident? I'm neither confident nor unconfident about it,
I'm just proposing the possibility. I'm not about to read
every page of the Standard, although I suppose I could
search for "padding".
-JKop
Jul 22 '05 #6
In message <2l************@uni-berlin.de>, John Harrison
<jo*************@hotmail.com> writes

"JKop" <NU**@NULL.NULL> wrote in message
news:eW*****************@news.indigo.ie...
[...]


So when you copy or over overwrite the padding bits, you're
accessing memory that isn't yours. For instance, the system
may utilize those bits for some other variable, like so:

TMyStru blah;
char kk[4];

Now the system may stick kk into the vacant spot:

[...]
Yikes!! I hope that isn't true. I know about padding but that the first time
anyone has said that the compiler can use that padding for other purposes,
rather than just leaving it unused.

In fact I'm pretty sure it can't be true, because it would mean the memset
would affect an unrelated variable, making memset unusable on POD types
(unless they happen to be char or arrays of char). If that really is true
then the standard is stupider than I thought.


Don't worry about it. JKop refuses to buy a copy of the standard, so
what he has to say on the subject is mostly worthless FUD.

--
Richard Herring
Jul 22 '05 #7
Richard Herring posted:

Don't worry about it. JKop refuses to buy a copy of the standard, so what he has to say on the subject is mostly worthless

FUD.

Actually,
http://www.lrde.epita.fr/~burrus_n/tmp/
Where in the C++ Standard does it say that the opinion of
Richard Herring is not "mostly worthless FUD"?
-JKop
Jul 22 '05 #8
In message <Fb*****************@news.indigo.ie>, JKop <NU**@NULL.NULL>
writes
Richard Herring posted:

Don't worry about it. JKop refuses to buy a copy of thestandard, so
what he has to say on the subject is mostly worthless

FUD.

Actually,

http://www.lrde.epita.fr/~burrus_n/tmp/


But, elsewhere:
I'm not about to read every page of the Standard, although I suppose I
could search for "padding".
It's no use unless you read it.
Where in the C++ Standard does it say that the opinion of
Richard Herring is not "mostly worthless FUD"?

Same place it says yours isn't. In this newsgroup, facts are preferred
over ill-informed opinion. Ill-informed speculation disguised as facts
is even worse.

--
Richard Herring
Jul 22 '05 #9
JKop wrote:
John Harrison posted:

I don't know what the standard says, but I think any


compiler
manufacturer that actually did that would have trouble


selling their
compilers.

Well, from experience working with with MSWindows, they
always use the likes of:

SOMESTUPIDSTRUCTURE blah;
memset(&blah,0,sizeof(blah));


And this has exactly what to do with padding and whether a compiler
uses intra-variable pad as temporary storage?

-n
Jul 22 '05 #10
Nikolaos D. Bougalis posted:
SOMESTUPIDSTRUCTURE blah; memset(&blah,0,sizeof(blah));


And this has exactly what to do with padding and whether a compiler
uses intra-variable pad as temporary storage?

-n

Really now, if I were you I'd feel cheated - how come everyone else gets a
fully functional brain while I get this piece of crap.

I never mentioned "intra-variable" nor "temporary storage".

Long story short, the padding bits aren't yours, but ofcourse you can always
use them anyway, just like you can:

int* k;

*k = 4;
Using memset as in my Microsoft example above in not portable code, as it
contains Undefined Behaviour.

On a PC with an Intel Pentium family CPU, running a Microsft Windows family
operating system, then you *may* have ownership of the padding bits, so
there *may* be nothing wrong with my aformentioned example.

But then this isn't a Microsoft newsgroup.
-JKop
Jul 22 '05 #11
JKop wrote:
Nikolaos D. Bougalis posted:

SOMESTUPIDSTRUCTURE blah; memset(&blah,0,sizeof(blah));
And this has exactly what to do with padding and whether a compiler
uses intra-variable pad as temporary storage?

-n


Really now, if I were you I'd feel cheated - how come everyone else gets a
fully functional brain while I get this piece of crap.


Resorting to ad-hominems a bit early aren't you?
I never mentioned "intra-variable" nor "temporary storage".
This comment was made in a thread dealing specifically with padding
between variables in a structure, in which you said that a compiler may
use the padding to store temporary variables, since "C++ contains unions."

I quote, from your post, eW*****************@news.indigo.ie:
TMyStru blah;
char kk[4];

Now the system may stick kk into the vacant spot:

---------
| int |
---------
---------
| int |
---------
---------
| int |
---------
---------
| int |
---------
---------
| kk |
---------
---------
| float |
---------
---------
| float |
---------
---------
| float |
---------
---------
| float |
---------
Long story short, the padding bits aren't yours, but ofcourse you can always
use them anyway, just like you can:

int* k;

*k = 4;
Using memset as in my Microsoft example above in not portable code, as it
contains Undefined Behaviour.


It does not contain undefined behavior, your baseless assertions to
that end notwithstanding.

Can you provide one citation from the standard that indicates that that
is undefined behavior? Oh, what's that? You don't read the standard?

-n
Jul 22 '05 #12
Nikolaos D. Bougalis posted:
This comment was made in a thread dealing specifically with padding
between variables in a structure, in which you said that a compiler may use the padding to store temporary variables, since "C++ contains unions."
Where the hell did I mention that they have to be
temporary? I suppose if I say "sibling" then that
automically means "brother" too.

It does not contain undefined behavior, your baseless assertions to
that end notwithstanding.

Can you provide one citation from the standard that indicates that that
is undefined behavior? Oh, what's that? You don't read

the standard?
I found 14 instances of "padding" in the Standard, none of
them gave anything.

A while back, Ioannis Vranos said that messing with padding
bits is Undefined Behaviour. You there? Could you please
elaborate?
-JKop
Jul 22 '05 #13
JKop wrote:
Nikolaos D. Bougalis posted:

This comment was made in a thread dealing


specifically with
padding
between variables in a structure, in which you said that


a compiler may
use the padding to store temporary variables, since "C++


contains
unions."

Where the hell did I mention that they have to be
temporary? I suppose if I say "sibling" then that
automically means "brother" too.


The example you provided used what appeared to be temporary TMyStru and
a temporary 4-character array. Even if you did not mean them to be
temporary, and instead intended them to be subsumed by another object,
that changes very little.
It does not contain undefined behavior, your


baseless assertions
to
that end notwithstanding.

Can you provide one citation from the standard that


indicates that
that
is undefined behavior? Oh, what's that? You don't read


the standard?
I found 14 instances of "padding" in the Standard, none of
them gave anything.

A while back, Ioannis Vranos said that messing with padding
bits is Undefined Behaviour. You there? Could you please
elaborate?


The standard prescribes certain actions as having "undefined behavior."
In 1.3.12, it also suggests that undefined behavior may occur where the
standard fails to explicitly state the behavior. In that sense, there
may be some gray area here.

Frankly, I can see where using memset() on an instance of a class may
be problematic, as you may end up overwriting v-tables or other critical
"internal plumbing." With that said, however, using memset() for a
plain-old C-style structure is safe, and a common programming idiom.

-n
Jul 22 '05 #14
John Harrison wrote:
...
In fact I'm pretty sure it can't be true, because it would mean the memset
would affect an unrelated variable, making memset unusable on POD types
(unless they happen to be char or arrays of char). If that really is true
then the standard is stupider than I thought.
...


Formally speaking, in general case 'memset' _is_ pretty much unusable on
any type other than one from the 'char' group. However, storing
unrelated variables in padding space of a struct would break the
functionality of 'memcpy', when used to copy data between two object of
the same POD struct type.

--
Best regards,
Andrey Tarasevich

Jul 22 '05 #15

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

Similar topics

7
by: Lynn | last post by:
I am rewriting some memory management code and I need to have a forward declaration of a data structure. I am not converting this data structure into a class (yet). How do I generate a forward...
2
by: Vince | last post by:
I have a very specific problem to solve but I cannot find a data structure for it. I don't know if I am posting on the good newsgroup but I cannot find a software.design group. I would like to...
3
by: Richard Webb | last post by:
Hi all, I guess this is more of a design problem than a language problem, but I'm confused either way! I have a class and it has a private data member which is a struct. The size of the struct is...
4
by: Thomas Paul Diffenbach | last post by:
Can anyone point me to an open source library of /statically allocated/ data structures? I'm writing some code that would benefit from trees, preferably self balancing, but on an embedded system...
11
by: theshowmecanuck | last post by:
As a matter of academic interest only, is there a way to programmatically list the 'c' data types? I am not looking for detail, just if it is possible, and what function could be used to...
6
by: STom | last post by:
I have a class declare like this: <Serializable>Public Class Talk Public Structure _localTalk Public _iTalkNumber as Integer End structure End Class I can see the structure from other...
5
by: Peter | last post by:
I put the result into an array getting from another db, the arrangement should not be changed, MemberList = "'007910', '006853', '007965'" SQL = "Select MemberID, name From Member" SQL = SQL...
31
by: aarklon | last post by:
Hi all, this is a question which i saw in a book typedef struct mall_li_header_ { int refcnt; uchar pool; uchar flag; ushort magic_no; char data;
24
by: Hurricane | last post by:
When I create a view in SQL and include an ORDER BY clause i can see it in Management Studio. However, when I call the same view from an ASP page the order goes completely haywire. Any ideas?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.