如何使局部变量的地址是8字节对齐? [英] How to make local variable's address is 8 byte alignment?

查看:896
本文介绍了如何使局部变量的地址是8字节对齐?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

HP-UX 11.23,HP aC ++ B3910B A.03.63


以下是样本。


bjhp1 / nfs / users / pjia> ; cat tt.c

#include< stdlib.h>


int main()

{

int i = 10;

char p [1024] ="" ;


printf("%p,%p \ n",& i,p);

返回0;

}


bjhp1 / nfs / users / pjia> aCC + DD64 tt.c


bjhp1 / nfs / users / pjia> ./a.out

800003ffbfff0f78,8000000ffbfff0f7c


我希望指针p'的地址是800003ffbfff0f80,而不是

800003ffbfff0f7c

有没有选择来实现这个目标?

HP-UX 11.23, HP aC++ B3910B A.03.63

Here is a samples.

bjhp1 /nfs/users/pjia>cat tt.c
#include <stdlib.h>

int main()
{
int i =10 ;
char p[1024] ="" ;

printf("%p, %p\n", &i, p) ;
return 0 ;
}

bjhp1 /nfs/users/pjia>aCC +DD64 tt.c

bjhp1 /nfs/users/pjia>./a.out
800003ffbfff0f78, 800003ffbfff0f7c

I hope the pointer p''s address is 800003ffbfff0f80, not
800003ffbfff0f7c
Is there any option to realize this?

推荐答案

Pengjun Jia写道:
Pengjun Jia wrote:

HP-UX 11.23,HP aC ++ B3910B A.03.63


以下是样本。


bjhp1 / nfs / users / pjia> cat tt.c

#include< stdlib.h>


int main()

{

int i = 10;

char p [1024] ="" ;


printf("%p,%p \ n",& i,p);

返回0;

}


bjhp1 / nfs / users / pjia> aCC + DD64 tt.c


bjhp1 / nfs / users / pjia> ./a.out

800003ffbfff0f78,8000000ffbfff0f7c


我希望指针p'的地址是800003ffbfff0f80,而不是

800003ffbfff0f7c
HP-UX 11.23, HP aC++ B3910B A.03.63

Here is a samples.

bjhp1 /nfs/users/pjia>cat tt.c
#include <stdlib.h>

int main()
{
int i =10 ;
char p[1024] ="" ;

printf("%p, %p\n", &i, p) ;
return 0 ;
}

bjhp1 /nfs/users/pjia>aCC +DD64 tt.c

bjhp1 /nfs/users/pjia>./a.out
800003ffbfff0f78, 800003ffbfff0f7c

I hope the pointer p''s address is 800003ffbfff0f80, not
800003ffbfff0f7c



为什么这对你很重要?

Why is this important to you?


是否有任何选项可以实现这一点?
Is there any option to realize this?



这是特定于您的平台的,我想,所以在

平台特定组中询问会更合适。


一般情况下,我认为你需要尝试在联盟(或可能是结构)中嵌入p

来玩弄技巧强制对齐。


例如,你可能会发现类似的东西: -


union {

double for_alignment ;

char p [1024];

}软糖。


将以8字节边界对齐。 />

但我重申这是特定于平台的,所以YMMV ......

This is specific to your platform, I''d think, so asking in a
platform-specific group would be more appropriate.

In general, I think you''d need to try playing tricks with embedding "p"
in a union (or possibly a structure) to force alignment.

For example, you may find that something like :-

union {
double for_alignment;
char p[1024];
} fudge.

Would get aligned at an 8-byte boundary.

But I repeat this is platform-specific, so YMMV...


Pengjun Jia写道:
Pengjun Jia wrote:

>

HP-UX 11.23,HP aC ++ B3910B A.03.63


以下是样本。


bjhp1 / nfs / users / pjia> cat tt.c

#include< stdlib.h>


int main()

{

int i = 10;

char p [1024] ="" ;


printf("%p,%p \ n",& i,p);

返回0;

}


bjhp1 / nfs / users / pjia> aCC + DD64 tt.c


bjhp1 / nfs / users / pjia> ./a.out

800003ffbfff0f78,8000000ffbfff0f7c


我希望指针p'的地址是800003ffbfff0f80,而不是

800003ffbfff0f7c

有没有选择来实现这个目标?
>
HP-UX 11.23, HP aC++ B3910B A.03.63

Here is a samples.

bjhp1 /nfs/users/pjia>cat tt.c
#include <stdlib.h>

int main()
{
int i =10 ;
char p[1024] ="" ;

printf("%p, %p\n", &i, p) ;
return 0 ;
}

bjhp1 /nfs/users/pjia>aCC +DD64 tt.c

bjhp1 /nfs/users/pjia>./a.out
800003ffbfff0f78, 800003ffbfff0f7c

I hope the pointer p''s address is 800003ffbfff0f80, not
800003ffbfff0f7c
Is there any option to realize this?



/ * BEGIN eight_bytes.c * /


#include< stdio.h>


#define BYTES 8

#define NUM 256

int main(无效)

{

int i [NUM + BYTES / sizeof(int)] = {10};

char * p = BYTES +(char *)i;

printf("%p,%p \ n",(void *)i,(void *)p);

返回0;

}


/ * END eight_bytes.c * /


-

pete

/* BEGIN eight_bytes.c */

#include <stdio.h>

#define BYTES 8
#define NUM 256

int main(void)
{
int i[NUM + BYTES / sizeof(int)] = {10};
char *p = BYTES + (char *)i ;

printf("%p, %p\n", (void *)i, (void *)p) ;
return 0 ;
}

/* END eight_bytes.c */

--
pete


pete写道,On 10/01/08 11:42:
pete wrote, On 10/01/08 11:42:

Pengjun Jia写道:
Pengjun Jia wrote:

> HP-UX 11.23,HP aC ++ B3910B A.03.63

这是一个示例。

bjhp1 / nfs / users / pjia> ; cat tt.c
#include< stdlib.h>

int main()
{i / 10;
char p [ 1024] ="" ;

printf("%p,%p \ n",& i,p);
返回0;
}

bjhp1 / nfs / users / pjia> aCC + DD64 tt.c

bjhp1 /nfs/users/pjia>./a.out
800003ffbfff0f78,8000000ffbfff0f7c

我希望指针p'的地址是800003ffbfff0f80,而不是
800003ffbfff0f7c
是否有任何选项可以实现这一点?
>HP-UX 11.23, HP aC++ B3910B A.03.63

Here is a samples.

bjhp1 /nfs/users/pjia>cat tt.c
#include <stdlib.h>

int main()
{
int i =10 ;
char p[1024] ="" ;

printf("%p, %p\n", &i, p) ;
return 0 ;
}

bjhp1 /nfs/users/pjia>aCC +DD64 tt.c

bjhp1 /nfs/users/pjia>./a.out
800003ffbfff0f78, 800003ffbfff0f7c

I hope the pointer p''s address is 800003ffbfff0f80, not
800003ffbfff0f7c
Is there any option to realize this?



/ * BEGIN eight_bytes.c * /


#include< stdio.h>


#define BYTES 8

#define NUM 256

int main(无效)

{

int i [NUM + BYTES / sizeof(int)] = {10};


/* BEGIN eight_bytes.c */

#include <stdio.h>

#define BYTES 8
#define NUM 256

int main(void)
{
int i[NUM + BYTES / sizeof(int)] = {10};



您已经更改了可能存在问题的i类型。你还没有注意到这很可能*不能*做你想做的事情,因为C

语言并不保证在内存中如何安排事情。对于

实例,一些编译器开始玩巧妙的技巧,其中数组是放置在堆栈上的数组和/或放置保护值。数组的任一侧要检测缓冲区溢出和/或尝试检测它们何时发生。

You have change the type of i which is likely to be a problem. You have
also failed to note that this may well *not* do what is wanted as the C
language does not guarantee how things will be arranged in memory. For
instance some compiler start playing clever tricks with where arrays are
placed on the stack and/or put "guard values" either side of arrays to
detect buffer overflows and/or try and detect when they have occurred.


char * p = BYTES +( char *)i;


printf("%p,%p \ n",(void *)i,(void *)p);
char *p = BYTES + (char *)i ;

printf("%p, %p\n", (void *)i, (void *)p) ;



如果你告诉OP为什么你添加了

演员,那会很有帮助。原因是%p特别需要一个void *指针

和其他指针类型可以有不同的大小,表示或

传递机制。

It would have been helpful if you had told the OP why you added the
casts. The reason being that %p specifically requires a void* pointer
and other pointer types can have different sizes, representations or
passing mechanisms.


返回0;

}


/ * END eight_bytes.c * /
return 0 ;
}

/* END eight_bytes.c */



-

Flash Gordon

--
Flash Gordon


这篇关于如何使局部变量的地址是8字节对齐?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆