Mike -- No longer here
Mike -- No longer here

Reputation: 2092

allocating from stack - data alignment issues in C

In another post, I asked a coding question and in the source code to that question, I declared some variables in the following manner:

char datablock[200];
char *pointer1=datablock;
char *pointer2=datablock+100;

However someone mentioned that the code may be incompatible with 64-bit systems because 100 isn't divisible by 8? I can't remember what it was.

But what I want to do is reserve a huge chunk of memory for use with my program and make it execute as fast as possible and I remember because of the way system caching memory works, that using data from the same block of memory is faster than using data from separate blocks. using malloc is also asking for slower memory.

So in code, This is an example of what I want to do. I want to allocate 40,000 bytes and give 4 pointers access to 10,000 bytes each:

char data[40000];
char *string0=data;
char *string1=data+10000;
char *string2=data+20000;
char *string3=data+30000;

This however is not what I want to do as I believe different sections of memory will be accessed:

char string0[10000];
char string1[10000];
char string2[10000];
char string3[10000];

I believe my idea is correct but is the only thing I need to be concerned about is that for 64-bit systems the offset value is a multiple of 8 and for 32-bit systems the offset value is a multiple of 4?

I don't want to pick wrong numbers and receive segmentation faults.

Upvotes: 0

Views: 60

Answers (2)

Manos Nikolaidis
Manos Nikolaidis

Reputation: 22244

Whether the system is 32 or 64 bit will not cause the code you mention to have a segmentation fault. In this example of pointer arithmetic :

char *pointer2=datablock+100;

you are saying : from address pointed to by datablock advance 100 times the size of a char. The number you advance doesn't have to be a multiple of any other number.

Regarding the last code snippet it's likely the 4 sections of memory will be consecutive in the stack but.

You can verify and see what's happening by printing pointer addresses. E.g.

printf("string0 %p\n", string0);
printf("string1 %p\n", string1);
printf("string2 %p\n", string2);
printf("string3 %p\n", string3);

Upvotes: 0

Jack
Jack

Reputation: 133609

The alignment problems that may arise are related to storing something that has a specified alignment outside of its alignment rules.

This is not your case. You are not storing pointers in unaligned addresses, you are just storing addresses in aligned pointers.

Just to make it clear:

char *pointer2=datablock+100;

This declares a pointer which could be on stack or on register according to how this will be compiled but the allocation of the space for the pointer itself is given to the compiler, which will do it correctly for the underlying architecture.

The problem can arise when you do something like:

int* asInteger = (int*) (datablock+1);
*datablock = 10;

In this situation you are trying to store a value which has an alignment requirement (int) in an address which could be unaligned to the requirement of int.

In any case, if I remember correctly, x86 architecture allows it to work but it is slower.

Upvotes: 2

Related Questions