Chinaunix首页 | 论坛 | 博客
  • 博客访问: 5142637
  • 博文数量: 1696
  • 博客积分: 10870
  • 博客等级: 上将
  • 技术积分: 18357
  • 用 户 组: 普通用户
  • 注册时间: 2007-03-30 15:16
文章分类
文章存档

2017年(1)

2016年(1)

2015年(1)

2013年(1)

2012年(43)

2011年(17)

2010年(828)

2009年(568)

2008年(185)

2007年(51)

分类: C/C++

2008-12-06 02:11:56

I am wondering what it means when a pointer is aligned?

Could someone perhaps enlighten me or point me in the right direction?

Thank you in advance.



--
Sig
Thu Sep 18 12:21:07 EDT 2003

  #  
Old November 13th, 2005, 06:57 PM
Hallvard B Furuseth
Guest
 
Posts: n/a
Default Re: What is "Pointer Alignment"

signuts wrote:
[color=blue]
> I am wondering what it means when a pointer is aligned?[/color]

On some architectures, some data types can or should only be accessed
if their address is a multiple of 4 or the size of the datatype or
whatever. Otherwise the program crashes, or gives wrong results, or
maybe the pointer access just goes a lot slower.

For a type T, a pointer is aligned for T access if it contains an
address which is a multiple of whatever number T* pointers should be a
multiple of.

--
Hallvard
  #  
Old November 13th, 2005, 06:57 PM
Lew Pitcher
Guest
 
Posts: n/a
Default Re: What is "Pointer Alignment"

On Thu, 18 Sep 2003 12:22:33 -0400, signuts wrote:
[color=blue]
>I am wondering what it means when a pointer is aligned?
>
>Could someone perhaps enlighten me or point me in the right direction?[/color]

In some systems, certain data types can only be stored such their addresses are
all on an even number boundary. In other systems, some data types require
addresses be on 4 octet boundaries.

If you had

char c;
char *Pc = &c;
int *Pi;

and you

Pi = (int *)Pc;

you might find that references to *Pi would cause errors because Pc didn't point
to a data item aligned on an "integer" boundary.



--
Lew Pitcher
IT Consultant, Enterprise Technology Solutions
Toronto Dominion Bank Financial Group

(Opinions expressed are my own, not my employers')
  #  
Old November 13th, 2005, 06:57 PM
Jens.Toerring@physik.fu-berlin.de
Guest
 
Posts: n/a
Default Re: What is "Pointer Alignment"

signuts wrote:[color=blue]
> I am wondering what it means when a pointer is aligned?
> Could someone perhaps enlighten me or point me in the right direction?[/color]

Since this isn't directly C related off-topicality warning:


There are several architectures where e.g. an integer must start at
an even address (or addresses that can be divided by 4). This is
related to the machines commands for e.g. loading an int from memory
into the CPU: when an int gets loaded it's probably done using a
machine instruction that roughly translates to "load word from
memory address 0xc73204". If you try to load a "word" from an odd
address this machine instruction does not work and you get a bus
error.

But in C you can write code that looks like this (error checking
omitted):

int a;
unsigned char *buf = malloc( 128 );
a = * ( int * ) ( buf + 1 );

malloc() will always return memory that's properly aligned, i.e.
the address you get can be used for all kinds of types without
running into danger of getting a bus error. So buf will be an
even address. But when you now try to assign to 'a' in the way
it's done above, you try to load 'a' from an odd memory address
and then you're in trouble, at least on machines which require
proper alignment (on other machines this might only result in a
slower memory access). So the above (if it's really necessary)
must be written e.g. as

memcpy( &a, buf + 1, sizeof a );

because memcpy() has to be written in a way that won't use a
"load word" (or similar) instruction that could result in a
bus error.

BTW, this is also the reason why structures often need padding.
If you have a structure like

struct {
char c;
int a;
} my_struct;

the compiler will have to insert at least one extra byte between
'c' and 'a' because otherwise 'a' would end up at an odd address
and

my_struct.a = 0;

might lead to a bus error.

Regards, Jens
--
_ _____ _____
| ||_ _||_ _|
_ | | | | | |
| |_| | | | | |
\___/ens|_|homs|_|oerring
阅读(1821) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~