4

I have several questions to ask that pertains to data position and alignment in C++. Do classes have the same memory placement and memory alignment format as structs?

More specifically, is data loaded into memory based on the order in which it's declared? Do functions affect memory alignment and data position or are they allocated to another location? Generally speaking, I keep all of my memory alignment and position dependent stuff like file headers and algorithmic data within a struct. I'm just curious to know whether or not this is intrinsic to classes as it is to structs and whether or not it will translate well into classes if I chose to use that approach.

Edit: Thanks for all your answers. They've really helped a lot.

4 Answers4

5

Do classes have the same memory placement and memory alignment format as structs?

The memory placement/alignment of objects is not contingent on whether its type was declared as a class or a struct. The only difference between a class and a struct in C++ is that a class have private members by default while a struct have public members by default.

More specifically, is data loaded into memory based on the order in which it's declared?

I'm not sure what you mean by "loaded into memory". Within an object however, the compiler is not allowed to rearrange variables. For example:

class Foo {
    int a;
    int b;
    int c;
};

The variables c must be located after b and b must be located after a within a Foo object. They are also constructed (initialized) in the order shown in the class declaration when a Foo is created, and destructed in the reverse order when a Foo is destroyed.

It's actually more complicated than this due to inheritance and access modifiers, but that is the basic idea.

Do functions affect memory alignment and data position or are they allocated to another location?

Functions are not data, so alignment isn't a concern for them. In some executable file formats and/or architectures, function binary code does in fact occupy a separate area from data variables, but the C++ language is agnostic to that fact.

Generally speaking, I keep all of my memory alignment and position dependent stuff like file headers and algorithmic data within a struct. I'm just curious to know whether or not this is intrinsic to classes as it is to structs and whether or not it will translate well into classes if I chose to use that approach.

Memory alignment is something that's almost automatically taken care of for you by the compiler. It's more of an implementation detail than anything else. I say "almost automatically" since there are situations where it may matter (serialization, ABIs, etc) but within an application it shouldn't be a concern.

With respect with reading files (since you mention file headers), it sounds like you're reading files directly into the memory occupied by a struct. I can't recommend that approach since issues with padding and alignment may make your code work on one platform and not another. Instead you should read the raw bytes a couple at a time from the file and assign them into the structs with simple assignment.

In silico
  • 51,091
  • 10
  • 150
  • 143
  • Functions are stored in memory, if I'm not mistaken (or a paging file). Another thing I'm curious to understand is how the compiler decides to order and position non-inlined functions (not that it really matters in most cases) within memory when the application executed. –  Jul 30 '11 at 05:12
  • @David Young: Yes, it's in memory, but how a compiler arranges function code around in memory is totally implementation-dependent. Details can change between compilers and even between compiler versions! – In silico Jul 30 '11 at 05:19
1

Do classes have the same memory placement and memory alignment format as structs?

Yes. Technically there is no difference between a class and a struct. The only difference is the default member access specification otherwise they are identical.

More specifically, is data loaded into memory based on the order in which it's declared?

Yes.

Do functions affect memory alignment and data position or are they allocated to another location?

No. They do not affect alignment. Methods are compiled separately. The object does not contain any reference to methods (to those that say virtual tables do affect members the answer is yes and no but this is an implementation detail that does not affect the relative difference between members. The compiler is allowed to add implementation specific data to the object).

Generally speaking, I keep all of my memory alignment and position dependent stuff like file headers and algorithmic data within a struct.

OK. Not sure how that affects anything.

I'm just curious to know whether or not this is intrinsic to classes as it is to structs

Class/Structs different name for the same thing.

and whether or not it will translate well into classes if I chose to use that approach.

Choose what approach?

Martin York
  • 257,169
  • 86
  • 333
  • 562
  • Not entirely on the second point. See http://stackoverflow.com/questions/6577906/is-size-of-the-object-affected-by-type-of-access-specifier-and-type-of-inheritanc/6578021#6578021 -- long story short, access specifiers muck the whole thing up. – Billy ONeal Jul 30 '11 at 05:14
0

C++ classes simply translate into structs with all the instance variables as the data contained inside the structs, while all the functions are separated from the class and are treated like functions with accept those structs as an argument.

The exact way instance variables are stored depends on the compiler used, but they generally tend to be in order.

marklu
  • 96
  • 2
  • Ah. So that's where the "this" pointer comes into play. For some reason I thought functions were positioned relative to the first instance of the object. Now that I reflect on it, that makes absolutely no sense and I was foolish to be so naive, since it would mean copying the functions or at the very least, the implementation of some sort of tracking mechanism. Ignorance is bliss. –  Jul 30 '11 at 05:06
0

C++ classes do not participate in "persistence", like binary-mode structures, and shouldn't have alignment attached to them. Keep the classes simple. Putting alignment with classes may have negative performance benefits and may have side effects too.

Ajay
  • 18,086
  • 12
  • 59
  • 105