Solved

I'd like to know how structs are stored in memory

Posted on 2008-10-01
14
343 Views
Last Modified: 2013-12-14
Hello. I'd like to know how structs are stored in memory. For example, if i have the following struct:

struct A {
    int _a;
    int _b
}

will it be safe to assume that if an instance of struct A is allocated at memory offset 100, the variable _a will be stored at 100 and variable _b will be at 100 + sizeof(int) ?
0
Comment
Question by:devoured_elysium
  • 5
  • 4
  • 3
  • +1
14 Comments
 
LVL 45

Expert Comment

by:Kdo
ID: 22614977

For the most part, yes.  There are often compiler options to regulate alignment that results in each item starting on a character, 16-bit, 32-bit, or 64-bit boundary.  But the default alignment is usually in line with sizeof (int).

Good Luck,
Kent
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 22615023
>> the variable _a will be stored at 100

That is guaranteed, yes. The first struct field always starts at the start address of the struct.


>> variable _b will be at 100 + sizeof(int)

Since int is the standard type, this will be true on most platforms, but there's no guarantee. The compiler might add padding between struct fields if needed.

Note that padding can be added between the fields, and at the end of the struct. Usually padding is added to align the fields on word boundaries.
0
 

Author Comment

by:devoured_elysium
ID: 22615106
If I know the struct declaration, am I able to read the values directly from memory? I mean, if I have a structure defined in project A which is like that one above, and I have one instance which I read from memory and send to another project(called project B), will I be able to just by pointer arithmetic read the contents of the struct, without ever defining it?

Thanks
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 22615167
>> will I be able to just by pointer arithmetic read the contents of the struct, without ever defining it?

If you know which padding was added, and if the other platform uses the same data type sizes and definitions, then that should work yes.

But, I would suggest to use an encoding to transfer data between systems, and then decode it on the target system into its own struct.
0
 
LVL 45

Expert Comment

by:Kdo
ID: 22615181

To do that, you'll have to know what the alignment is so that you know how much padding may be between the data items.

If the default alignment is a 32-bit boundary, note that defining and item as a char will cause the next item to move to the 32-bit boundary.  If you declare consecutive items as *char*, it doesn't matter that it will fit, the second one will be moved to the next alignment boundary.

The only way to be sure how and where the item will align will be to control the alignment yourself (via compiler options or #pragma statements) or to compute them yourself.  The code below should tell you what the alignment size is.  Your only challenge beyond that is to note which items are larger than this and note that they will align on a multiple of this value.


Good Luck,
Kent


struct
{
  a   char;
  b   char;
} aligntest_t;

aligntest_t Test;

  printf ("alignment is %d bytes\n", &Test.b - &Test.a);



Kent
0
 

Author Comment

by:devoured_elysium
ID: 22615201
Hm, I still don't get one thing. The way Windows will pad the struct will be ALWAYS the same on the same computer, or will depend on how full the memory currently is, etc? If I see that windows will right now put the two ints together without padding them for an instance of my struct, is it correct to assume it will do the same for another 100?
0
 
LVL 45

Accepted Solution

by:
Kdo earned 63 total points
ID: 22615230

Since you have multi-platform issues, both end will certainly have to know the exact alignment.  Trying to determine it is somewhat wasteful, so controlling the alignment is probably the best solution.  I'd suggest using #pragma statements to pack the alignment to the byte level.

#pragma pack
#pragma align=pack
#pragma byte
#pragma 8

One form of the lines above will probably work.  Each C compiler has it's own set of #pragma statements so I can't tell you which one will work for you.

The standard used to allow for bad #pragma statements so that you could include all of the lines above and if the compiler didn't recognize any of them they would be ignored.  That allowed you to cover all the bases and have source code that generated a propery object module with any compiler.

But I've noticed that #pragma statements aren't always ignored.  I don't know if this is a change in the standard or just bad implementation.

Still, I might be tempted to give this a try.


Kent
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 45

Expert Comment

by:Kdo
ID: 22615246

The alignment is a compile time decision, controller by you (as compiler options) or the source code.  The amount of memory available at run time has no bearing on alignment.


Kent
0
 
LVL 53

Assisted Solution

by:Infinity08
Infinity08 earned 62 total points
ID: 22615363
>> is it correct to assume it will do the same for another 100?

Most likely yes, assuming that the compiler is consistent in the way it handles padding.
However, technically, you cannot make that assumption, since there is no guarantee by the standard about it.
You could check the compiler documentation to see if they talk about such guarantees or not. That would only be valid for that specific compiler though.

As I said earlier, in general, I don't exchange structs between processes/platforms ... I rather encode them (serialize) on the sending end, and decode on the receiving end. It's a lot more robust, portable, and will certainly avoid a lot of headaches ;)
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 22615634
May I ask why you gave a B grade ? That usually means that something is missing in the answers and/or that something is still unclear. If that's the case, then please do not hesitate to ask for clarification where needed.
0
 

Author Comment

by:devoured_elysium
ID: 22616000
I am sorry, I just gave points and skimmed through the page. If there's a way to still change it, I'll do it. Is there?
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 22616576
No worries. I just wanted to make sure that you got the help you needed.

For your information, here are a few FAQ entries that might interest you :

        http://www.experts-exchange.com/help.jsp#hi403
0
 
LVL 84

Expert Comment

by:ozo
ID: 22617723
Technically what you can assume is that
A pointer to a
structure object, suitably converted, points to its initial member (or if that member is a
bit-field, then to the unit in which it resides), and vice versa.
But
There may be unnamed
padding within a structure object, but not at its beginning.
0
 
LVL 84

Expert Comment

by:ozo
ID: 22617765
the standard header <stddef.h>.
defines
offsetof(type, member-designator)
which expands to an integer constant expression that has type size_t, the value of
which is the offset in bytes, to the structure member (designated by member-designator),
from the beginning of its structure (designated by type). The type and member designator
shall be such that given
static type t;
then the expression &(t.member-designator) evaluates to an address constant. (If the
specified member is a bit-field, the behavior is undefined.)
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

In our object-oriented world the class is a minimal unit, a brick for constructing our applications. It is an abstraction and we know well how to use it. In well-designed software we are not usually interested in knowing how objects look in memory. …
Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now