This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Alignment - Structures and Other Things


I'm confused about if and how memory alignment relates to struct types. Say you have a struct like so:

struct foo
{
    int a;
    char b;
    float c;
};

Now, in the typical use case instances of type foo will be allocated either on the stack or on the heap via malloc(), so in either case it's the implementation which decides if the struct's base address is going to be specially aligned in some way. But is this even really important for structs? What if I were to do something like the following:

struct foo s;
s.a = 7;
s.b = 'a';
s.c = 1.1f;

char* bytes = malloc(1 + sizeof(struct foo));
bytes++;
*(struct foo*)bytes = s;

Would the system be unhappy in any way because the struct is being stored in a region shifted by one by over from where malloc() might "want" it to be?

Thanks for any help in advance,
-Patrick

P.S.
I'm also a bit confused about how memory alignment relates to _non_ struct types as well, I'm pretty confused about alignment in general. I've been programming for a while now, but I've never had to deal with low level enough code that memory alignment issues would come under my radar; until today. If anyone could briefly explain, or point me to a good resource, that would be awesome.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]