memory alignment

J

Janice

My working system requires the starting address of a piece of data (an int,
a long, etc.) must be a multiple of 4. If the requirement is not met, a
segmentation fault occurs.
My question is the following.
For example,
int a = buf[x]; //The buf is an int array
How come the x must be a multiple of 4?
If the x cannot be a multiple of 4, will the following statement also cause
segmentation fault?
buf[3]=10;

Then, why the following is a solution?
x=3;
memcpy(a, buf+x, sizeof(int));

Thanx
 
J

Jack Klein

My working system requires the starting address of a piece of data (an int,
a long, etc.) must be a multiple of 4. If the requirement is not met, a
segmentation fault occurs.

The C standard allows an implementation to provide alignment
requirements. In some cased there is no way to circumvent them,
because the underlying processor hardware traps on misaligned memory
accesses. ARM processors will do this, for example.
My question is the following.
For example,
int a = buf[x]; //The buf is an int array
How come the x must be a multiple of 4?

Because your implementation requires it, as the C standard allows it
to. And perhaps because the underlying hardware requires it, which is
indeed a good reason for the C language to require it.
If the x cannot be a multiple of 4, will the following statement also cause
segmentation fault?
buf[3]=10;

The question is pretty meaningless, because 'x' can always be defined
in such a way that it is properly aligned. No special effort on the
part of the programmer is required. Just define it as:

int buf [however_many];

....and it will be perfectly aligned to 4, or 8, or 1024, or whatever
value is required. All automatically.

Then, why the following is a solution?
x=3;
memcpy(a, buf+x, sizeof(int));

Who says the that is a solution? If the buf is defined, as opposed to
dynamically allocated, and its type is not either pointer to int or
array of int, then your code has undefined behavior regardless of
alignment issues.

Store ints into memory defined to store ints and you won't have any
problems.
 
K

Keith Thompson

Janice said:
My working system requires the starting address of a piece of data (an int,
a long, etc.) must be a multiple of 4. If the requirement is not met, a
segmentation fault occurs.

Ok. That's pretty common.
My question is the following.
For example,
int a = buf[x]; //The buf is an int array
How come the x must be a multiple of 4?

What makes you think x needs to be a multiple of 4? If buf is
declared as an int array, you can access any element as an int, and
each element will be properly aligned. The compiler will take care of
this for you.

Here's a program that illustrates what's going on:

#include <stdio.h>
int main(void)
{
int buf[20] = { 3, 1, 4, 1, 5, 9, 2, 6, 5, 3,
5, 8, 9, 7, 9, 2, 3, 2, 8, 4 };
int i;
i = buf[0]; /* no problem */
printf("buf[0] = %d\n", i);
i = buf[1]; /* no problem */
printf("buf[1] = %d\n", i);
i = buf[19]; /* again, no problem */
printf("buf[19] = %d\n", i);

printf("Address of buf[0] is %p\n", (void*)&buf[0]);
printf("Address of buf[1] is %p\n", (void*)&buf[1]);
printf("Address of buf[19] is %p\n", (void*)&buf[19]);

return 0;
}

I ran it on a system with characteristics similar to yours (8-bit
bytes, 32-bit int, 4-byte alignment for int), and got the following
output:

buf[0] = 3
buf[1] = 1
buf[19] = 4
Address of buf[0] is 0x22eec0
Address of buf[1] is 0x22eec4
Address of buf[19] is 0x22ef0c

Both the values of the addresses and the output format of the "%p"
option are system-specific. On this system, addresses are displayed
in hexadecimal, and you can tell from the output that all the
addresses are 4-byte aligned.
 

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

No members online now.

Forum statistics

Threads
474,166
Messages
2,570,903
Members
47,444
Latest member
Michaeltoyler01

Latest Threads

Top