什么是const char * const * p; [英] What is const char * const *p;

查看:160
本文介绍了什么是const char * const * p;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,


我几乎没有问题。它们是:


1.是const char * const * p;一个有效的构造?

2.如何在32字节边界对齐给定的结构?

3.然后,我如何断言给定的对象比如说,

在32字节边界?

问候,


-

Vijay Kumar R. Zanvar

主页 - http:// geocities。 com / vijoeyz /

解决方案

Vijay Kumar R. Zanvar写道:



我几乎没有问题。

1.是const char * const * p;一个有效的结构?


是的。 p和p都是const的char。

2.如何在32字节边界对齐给定的结构?


char * q =(char *)malloc(64);

char * p =(q + 31)&(~31);


p在四字边界上对齐。


3.然后,如何断言给定对象是否对齐,比如说,
32字节边界?




断言(p ==((p + 31)&(~31)));


vi*****@gmail.com (Vijay Kumar R. Zanvar)写道:

1.是const char * const * p;一个有效的结构?


是。

2.如何在32字节边界对齐给定的结构?
3.那么,怎么做我声称一个给定的对象在32字节边界处对齐,比如




你不是,你可以合理但不是完全可以肯定,




也就是说,如果你碰巧_know_某个C类型必须对齐

on 32字节的边界,那么你也知道那个类型的所有实际对象

确实如此对齐,你得到的内存也是如此......来自malloc()的
(因为必须为任何类型正确对齐,

包括你的32字节)。

问题是找到类似的类型,因为标准中没有任何内容br />
要求任何类型的对齐,除了几种类型必须与密切相关的类型具有相同的对齐方式

(例如,签名)和unsigned int必须是相同的aligne d; void *具有与b *相同的要求
;等)并且malloc()返回一个

全合适的指针。没有什么要么要求某种类型与某些边界对齐,或者允许你使用
找出某种类型的对齐要求。

哦,一个例外:因为数组成员是连续的,所有类型都可以

没有比sizeof(类型)更严格的对齐要求。但是因为

它们也可以更松散地对齐(比如,尺寸(类型)的一半),

如果你真的需要,这实际上并没有帮助你一个特定严格的对齐。特别是,任何类型为struct align的对象{

char align [32]; }并不保证在32字节边界上对齐,

或实际上任何边界。


至于声明现有对象的对齐方式,你可以尝试

检查((intptr_t)& object)%32(或者如果你使用C89,((unsigned

long)& object)% 32)为零。这并不是严格要求工作,因为从指针到整数的转换是实现定义的,但是假设在所有现代实现中, b
是不合理的事实上,这确实按预期工作 - 如果只是因为实现者

将不得不竭尽全力让它失败。所以期望它在DS9K< g>上非常惊人地失败了

,但在任何常见系统上都没有。


Richard


",E。 Robert Tisdale <,E ************** @ jpl.nasa.gov>。在消息中写道

news:co ********** @ nntp1.jpl.nasa.gov ...

Vijay Kumar R. Zanvar写了一个匆匆一点:



我几乎没有问题。

1.是const char * const * p;有效的构造?



是的。 p和p都是const的char。




不! p指向常量字符的常量指针。你可以改变p,但是你不能改变它指向的指针,而不是这个指针

指向的字符。

2.如何在32字节边界对齐给定结构?



char * q =(char *)malloc(64 );
char * p =(q + 31)&(~31);

p在四字边界上对齐。



四字?一个单词的大小是多少?你在混合比特和字节吗?

上面的代码不能在一个配置合理的编译器上编译,因为它是
在整数和指针之间进行隐式转换。您可以使用此

代替:


unsigned char * q = malloc(32 + 31);

void * p = (void *)(((unsigned long)q + 31)& ~31);


但它也不是可移植代码。在符合C99的环境中,你可以使用

intptr_t而不是unsigned long来允许指向不同大小的指针

而不是long,但是所使用的算术技巧并不能保证对所有体系结构上的指针对齐都有

所需的效果。

3.然后,如何断言给定的对象在32字节边界对齐,比如



断言(p ==((p + 31)&(~31)));




相同的注释:指针和整数之间的隐式转换。

有了上述限制,请使用:


断言(((unsigned long)p& 31)== 0);


-

Chqrlie。


Hello,

I have few questions. They are:

1. Is "const char * const *p;" a valid construct?
2. How do I align a given structure, say, at 32-byte boundary?
3. Then, how do I assert that a given object is aligned, say,
at 32-byte boundary?
Regards,

--
Vijay Kumar R. Zanvar
Home Page - http://geocities.com/vijoeyz/

解决方案

Vijay Kumar R. Zanvar wrote:



I have few questions.

1. Is "const char* const *p;" a valid construct?
Yes. Both p and the char to which p ponts are const.
2. How do I align a given structure, say, at 32-byte boundary?
char* q = (char*)malloc(64);
char* p = (q + 31)&(~31);

p is aligned on a quad word boundary.

3. Then, how do I assert that a given object is aligned, say,
at 32-byte boundary?



assert(p == ((p + 31)&(~31)));


vi*****@gmail.com (Vijay Kumar R. Zanvar) wrote:

1. Is "const char * const *p;" a valid construct?
Yes.
2. How do I align a given structure, say, at 32-byte boundary?
3. Then, how do I assert that a given object is aligned, say,
at 32-byte boundary?



You don''t, and you can be reasonably but not perfectly sure,
respectively.

That is, if you happen to _know_ that a certain C type must be aligned
on 32-byte boundaries, then you also know that both all actual objects
of that type are indeed so aligned, and that so is the memory you get
from malloc() (because that must be correctly aligned for any type,
including your 32-byte one).
The problem is finding a type like that, because nothing in the Standard
demands anything about the alignment of any type except that several
kinds of types must have the same alignment as closely related types
(e.g., signed and unsigned int must be identically aligned; void * has
the same requirements as char *; etc.) and that malloc() returns an
all-suitable pointer. There is nothing that either demands that a
certain type is aligned to certain boundaries, or that allows you to
find out which alignment requirements a type has.
Oh, one exception: because array members are contiguous, all types can
have no stricter alignment requirements than to sizeof(type). But since
they could also be aligned more loosely (say, to half of sizeof(type)),
this doesn''t actually help you any if you really need an alignment of a
particular strictness. In particular, any object of type struct align {
char align[32]; } is not guaranteed to be aligned on a 32-byte boundary,
or indeed any boundary at all.

As for asserting the alignment of an existing object, you can try to
check that ((intptr_t)&object)%32 (or if you use C89, ((unsigned
long)&object)%32) is zero. This is not strictly required to work, since
conversion from pointers to integers is implementation-defined, but it
would not be unreasonable to assume that on all modern implementations,
this does in fact work as expected - if only because the implementor
would have to go out of his way to make it fail. So expect it to fail
quite spectacularly on the DS9K <g>, but not on any common system.

Richard


"E. Robert Tisdale" <E.**************@jpl.nasa.gov> wrote in message
news:co**********@nntp1.jpl.nasa.gov...

Vijay Kumar R. Zanvar wrote a bit hastily:



I have few questions.

1. Is "const char* const *p;" a valid construct?



Yes. Both p and the char to which p ponts are const.



No! p points to a constant pointer to constant chars. You can change p, but
you cannot change the pointer it points to, not the characters this pointer
points to.

2. How do I align a given structure, say, at 32-byte boundary?



char* q = (char*)malloc(64);
char* p = (q + 31)&(~31);

p is aligned on a quad word boundary.



quad word ? what is the size of a word ? are you mixing bits and bytes ?
The above code will not compile on a decently configured compiler because it
does implicit conversions between integers and pointers. you can use this
instead:

unsigned char *q = malloc(32 + 31);
void *p = (void*)(((unsigned long)q + 31) & ~31);

But it not portable code either. On a C99 conformant environment, you may use
intptr_t instead of unsigned long to allow for pointers with a different size
than long, but the arithmetic trick played is not guaranteed to have the
required effect on pointer alignment on all architectures.

3. Then, how do I assert that a given object is aligned, say,
at 32-byte boundary?



assert(p == ((p + 31)&(~31)));



Same remark : implicit conversions between pointer and integer.
With the restrictions expressed above, use this:

assert(((unsigned long)p & 31) == 0);

--
Chqrlie.


这篇关于什么是const char * const * p;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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