I have a class like this:
class Object {
public:
unsigned char data[8];
// other variables
// functions etc...
};
The question is - are the object members all stored in the same place in memory relative to the object? So if I have an array: Object array[3], given a char pointer
char* data_ptr = array[0].data
, will
data_ptr + (sizeof(Object))
then always point to array[1].data?
(I've read a couple of Q/As about how there might be padding inbetween data members of classes and structs - but I don't think they answer my question.)
Thanks in advance, Ben
Allocating array storage in C Calls to malloc commonly use a sizeof expression to specify the size in bytes of the requested storage. To allocate storage for an array, just multiply the size of each array element by the array dimension.
The important thing about arrays is that array elements are always stored in consecutive memory locations. We can verify this fact by printing the memory addresses of the elements.
Unless it's a VLA, array size is a compile time constant, so it does not matter how much memory you're actually using, it will take up the amount of memory same as the size of the array given at the time of definition.
The object will be stored in the stack when it is created inside the block and when the control exits the block or function then the object is removed or destroyed. In case of dynamically allocated objects (during runtime) the object will be stored on the heap. This is done with the help of new operator.
sizeof Object
already includes all internal padding of the class Object
. including any padding at its end. Arrays don't allow any additional padding. Therefore it is true that data_ptr + sizeof Object
will have the address of array[1].data
.
However I'm not sure if this is actually allowed. That is, the compiler might be allowed to assume that you never add a value larger than 8 (the size of the member array data
) to array[0].data
, and therefore it might apply optimizations that fail if you violate the rules. That is, your code might actually exhibit undefined behavior (which is the standard term for "the compiler is allowed to do anything in this case").
However since you are using a pointer to char
, for which there are more permissive rules (you can do many things with char*
which you could not do with general types), it may be that it's actually defined behaviour anyway.
If the aim of your question is to understand how things are in memory, then it is acceptable.
But if you want to do that for real: What you want to do is actually criminal against all your colleagues.
The proper way to go through collection in C++ is not to use an array, but an std::vector, or another std collection if it is really needed. We shouuld no longer use C arithmetics, but access the items of the vector collection through an iterator. That's the reason of the C++ standard library :-)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With