allignment of structure

A

Anthony

Hi,
I've got a question about the allignment of data in a structure.
What happens in memory when i declare the following

struct A
{
int x;
}
stuct B : public A
{
int y;
}

Is it compiler specific how the memory is alligned when I instance B?
Or can do something like this:

B b;
int* z = &(reinterpret_cast<int>(b)); //z point to x
z++; //z points to y

Or does the VTABLE pointer cause problems here?

Thanks in regard,
Anthony Lansbergen
 
V

Victor Bazarov

Anthony said:
I've got a question about the allignment of data in a structure.
What happens in memory when i declare the following

struct A
{
int x;
}
stuct B : public A
{
int y;
}

Is it compiler specific how the memory is alligned when I instance B?
Yes.

Or can do something like this:

B b;
int* z = &(reinterpret_cast<int>(b)); //z point to x
z++; //z points to y

Most likely you can't. The Standard imposes no requirement on
the behaviour of such code. It's called "undefined behaviour".
Or does the VTABLE pointer cause problems here?

There is no such thing in the language as "VTABLE pointer". If
you're talking of a very common way to implement virtual function
mechanism in the language, then what does it matter here? Your
structs have no virtual functions.

Victor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,533
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top