Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > memory alignment

Reply
Thread Tools

memory alignment

 
 
Janice
Guest
Posts: n/a
 
      07-11-2005
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


 
Reply With Quote
 
 
 
 
Jack Klein
Guest
Posts: n/a
 
      07-11-2005
On Mon, 11 Jul 2005 01:05:40 GMT, "Janice" <> wrote in
comp.lang.c:

> 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.

> Thanx


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

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
 
Reply With Quote
 
 
 
 
Keith Thompson
Guest
Posts: n/a
 
      07-11-2005
"Janice" <> writes:
> 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.

--
Keith Thompson (The_Other_Keith) kst- <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Memory Padding and alignment Paul_Huang C++ 2 09-21-2004 06:05 AM
Questions about "alignment" in memory J. Campbell C++ 14 10-09-2003 06:23 PM
Re: memory alignment? Peter van Merkerk C++ 1 08-01-2003 04:13 PM
Re: memory alignment? Andrew Koenig C++ 1 08-01-2003 02:15 PM
Re: memory alignment? Thomas Matthews C++ 0 08-01-2003 11:57 AM



Advertisments