last day (15 days later) » 

10:32 PM
8
A: How is the memory layout of a class vs. a struct

Columbo Is there a guarantee e.g. that the public variables will be first in memory then the private variable? No, such a guarantee is not made - C++11 standard, [class.mem]/14: Nonstatic data members of a (non-union) class with the same access control (Clause 11) are allocated so that later...

 
So I can not assume they are in sequential order if I have both public and private variables? What if I have only public or only private?
 
Then they have indeed increasing addresses. As the quote says.
 
It only says the order, but not that the first variable actually start at the "first address"? Lets assume a class without inheritance.
 
@Phataas Allright, see the bottom of my answer
@Phataas Also added the requirement list.
 
This gave me good insight and was very well explained! I was thinking of using the class to be able to cast to this class from a pointer to the start of an array, but I am now not sure if this is possible because of the accessor member functions and other possible padding etc in the class taking memory?
Hi
 
10:38 PM
Hi.
> I was thinking of using the class to be able to cast to this class from a pointer to the start of an array
So.. can you show me an example of what you mean?
 
It can be possible if you make sure the compiler doesn't pad the class (#pragma pack: stackoverflow.com/questions/3318410/pragma-pack-effect) and you make sure there are no virtual functions in the class heirarchy.
 
I was thinking something like: MyStruct myStruct = (MyStruct) &myArray[0]
 
@Phataas The address of an array is always the same as the address of its first element. By definition.
 
Yeah, but by casting it as a MyStruct with all private members and accessor member functions I am not sure if everything will be ok.
 
Post a minimalistic example and I'll tell you whether its valid or not.
From what it sounds like I'm not sure I understood everything :)
 
10:50 PM
The minimal example is: MyStruct myStruct = (MyStruct) &myArray[0]
I want the data of the array to map to the member variables of the class/object
 
He wants to do: SomeStruct* ss = reinterpret_cast<SomeStruct*>(arrayPtr);
 
I guess that's the C++ way of doing it :P
 
Ah, then do a normal C cast.
It needs to be a pointer to your class though
 
Your right :P Getting sleepy
 
SomeStruct array[10];
SomeStruct* ss = array; // Implicit array to pointer decay
ss now points to &array[0].
I.e. assert( ss == &array[0] ) succeeds. Regardless of what SomeStruct is.
 
10:57 PM
uint8_t array[10];
MyClass *c = &array[0];
That's basically what I want to do =)
Thank you all for all your help =) I learn some new things today and that is good =)
 
Errm, and how is MyClass defined in this case?
Smells a lot like undefined behavior.
 
11:17 PM
uint8_t data[50];
Class *c;
c = new Class();
c = (Class *) &data[0]
Now all the data bytes are mapped to the class member variables
I am still a little afraid of padding, but I tried with MSVC and the debugger shows correct values on all its members. I have a simple class with all private member variables and getter/setters.
 
Class *c;
c = new Class();
c = (Class *) &data[0]
This is not doing what you think it does.
I think you're lacking some C++ basics.
Are you trying to construct a Class object inside data?
 
If I have an array of bytes that is received and I know that 10 bytes are 1 frame. Some of the data might be 1 byte or 2 bytes etc. To easier be able to reference to the byte array fields in a more understandable way, using a getter function it is desired to map to a class (or struct as I usually use in C).
 

last day (15 days later) »