指针初始化。 [英] Pointer initialization.

查看:76
本文介绍了指针初始化。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



亲切的朋友们。


有人可以解释下面的代码吗?


/ *创建纹理的存储空间* /

AUX_RGBImageRec * TextureImage [1]; / *第1行* /

/ *将指针设置为NULL * /

memset(TextureImage,0,sizeof(void *)* 1); / *第2行* /


根据我在第一行的知识

将TextureImage变量声明为

数组一个指向AUX_RGBImageRec的指针。


这是我的问题。

我根本不理解第二行。

特别是sizeof(void *)* 1的东西。

这是什么意思?


Pointer数组的初始化

可能更好像这样

AUX_RGBImageRec * TextureImage [1] = {NULL};

对吗?它有相同的效果吗?


(我知道区别,但让我们假设

将NULL和0翻译为相同的

目前的事情。)


非常感谢您的帮助。


-

tuko the ugly。

解决方案

tuko写道:


有人可以解释请下面的代码?

/ *为纹理创建存储空间* /
AUX_RGBImageRec * TextureImage [1]; / *第1行* /
/ *将指针设置为NULL * /
memset(TextureImage,0,sizeof(void *)* 1); / *第2行* /

根据我在第一行的知识
将TextureImage变量声明为
一个指向AUX_RGBImageRec的指针数组。




对于所有实际目的来说,这是愚蠢的废话。相信自己

并编写更好更清晰的代码。你可能想要的是:


AUX_RGBImageRec * TextureImage;

TextureImage = NULL;


没有误导性评论。


-

计算机软件行业最惊人的成就

是它继续取消稳定和令人咋舌的计算机硬件行业的收益...... - Petroski


tuko< tu ** @ away.com>写道:

有人可以解释下面的代码吗?

/ *为纹理创建存储空间* /
AUX_RGBImageRec * TextureImage [1] ; / *第1行* /
/ *将指针设置为NULL * /
memset(TextureImage,0,sizeof(void *)* 1); / *第2行* /


第二行有问题(见下文)并且没有做

评论建议的内容。

根据我在第一行的知识
将TextureImage变量声明为一个指向AUX_RGBImageRec的指针的数组。


是的。

这是我的问题。
我根本不理解第二行。
特别是sizeof (void *)* 1件事。
这是什么意思?


它产生一个通用对象指针的大小,也称为指向

void的指针。将'sizeof(void *)''与1相乘无效。


因此``memset''调用设置`sizeof(void *)''连续字节,

从地址`TextureImage''开始,到零(即它将这些字节的所有位

设置为零)。

数组的初始化指针
可能更像这样
AUX_RGBImageRec * TextureImage [1] = {NULL};
对吗?


是的。或者,如果分配而不是初始化是




TextureImage [0] = NULL;


会起作用。

它有同样的效果吗?


嗯,不,原始代码不一定正常工作。它有两个问题。
两个问题。首先,`memset''调用将所有位设置为零,

但这不一定是null

指针的正确表示。 OTOH,初始化为NULL或赋值为NULL

将指针设置为空指针,即使空指针的内部表示为/ b $ b是/非/全位-zero。


其次,`sizeof(void *)''(通用对象指针的大小)需要

与`sizeof不一样(AUX_RGBImageRec *)''(指针大小

到'AUX_RGBImageRec''),所以'memset''调用甚至没有达到大小

是的。

(我知道区别,但是我们假设暂时将NULL和0翻译成相同的东西。)




允许'NULL''扩展为'0'',无论'NULL''是正确的,

写'0'代替是正确的同样。在需要指针为
的上下文中,0被视为空指针常量,而不是整数0.


Martin

-

, - 。 Martin Dickopp,德国德累斯顿,=, - _-。 =。

/, - ) http://www.zero -based.org/ ((_ /)oo(\_))

\` - ''` - ''(。)` - ''

` - 。 Debian,GNU操作系统的一种变体。 \ _ /


Martin Dickopp写道:


[snip]


其次,`sizeof(void *)''(通用对象指针的大小)需要与`sizeof(AUX_RGBImageRec *)''不一致(指针的大小
到'AUX_RGBImageRec''),所以'memset''调用甚至没有正确的大小




你是说那个` sizeof(AUX_RGBImageRec *)!= sizeof(void *)''?


因此,`AUX_RGBImageRec *''可以在

比较函数中使用_never_ `qsort(3C)'',因为它的指针大小可能与
的大小不同于'void *''的大小?!!!

Stephen



Hello kind people.

Can someone explain please the following code?

/* Create Storage Space For The Texture */
AUX_RGBImageRec *TextureImage[1]; /* Line 1*/
/* Set The Pointer To NULL */
memset(TextureImage,0,sizeof(void *)*1); /* Line 2*/

According to my knowledge in the first line
the TextureImage variable is declared as
an array of one pointer to AUX_RGBImageRec.

Here is my question.
I don''t understand the second line at all.
Especially the sizeof(void *) *1 thing.
What does it mean?

The initialization of the array of Pointer
could be better like this
AUX_RGBImageRec *TextureImage[1] = {NULL};
Right? It has the same effect?

(I know the distinction, but let''s assume
that NULL and 0 is translated as the same
thing for the moment.)

Many thanks for any help.

--
tuko the ugly.

解决方案

tuko wrote:


Can someone explain please the following code?

/* Create Storage Space For The Texture */
AUX_RGBImageRec *TextureImage[1]; /* Line 1*/
/* Set The Pointer To NULL */
memset(TextureImage,0,sizeof(void *)*1); /* Line 2*/

According to my knowledge in the first line
the TextureImage variable is declared as
an array of one pointer to AUX_RGBImageRec.



It''s stupid nonsense, to all practical purposes. Trust yourself
and write better and clearer code. What you probably want is:

AUX_RGBImageRec *TextureImage;
TextureImage = NULL;

without the misleading comments.

--
"The most amazing achievement of the computer software industry
is its continuing cancellation of the steady and staggering
gains made by the computer hardware industry..." - Petroski


tuko <tu**@away.com> writes:

Can someone explain please the following code?

/* Create Storage Space For The Texture */
AUX_RGBImageRec *TextureImage[1]; /* Line 1*/
/* Set The Pointer To NULL */
memset(TextureImage,0,sizeof(void *)*1); /* Line 2*/
The second line is problematic (see below) and doesn''t do what the
comment suggests.
According to my knowledge in the first line
the TextureImage variable is declared as
an array of one pointer to AUX_RGBImageRec.
Yes.
Here is my question.
I don''t understand the second line at all.
Especially the sizeof(void *) *1 thing.
What does it mean?
It yields the size of a generic object pointer, also known as pointer to
void. Multiplying `sizeof (void *)'' with 1 has no effect.

The `memset'' call therefore sets `sizeof (void *)'' consecutive bytes,
starting from the address `TextureImage'', to zero (i.e. it sets all bits
of these bytes to zero).
The initialization of the array of Pointer
could be better like this
AUX_RGBImageRec *TextureImage[1] = {NULL};
Right?
Yes. Alternatively, if an assignment instead of an initialization is
desired,

TextureImage [0] = NULL;

would work.
It has the same effect?
Well, no, the original code doesn''t necessarily work correctly. It has
two problems. First of all, the `memset'' call sets all bits to zero,
but that is not necessarily the correct representation of a null
pointer. OTOH, initialization with NULL or assignment of NULL always
sets the pointer to a null pointer, even if the internal representation
of a null pointer is /not/ all-bits-zero.

Secondly, `sizeof (void *)'' (the size of a generic object pointer) need
not be the same as `sizeof (AUX_RGBImageRec *)'' (the size of a pointer
to `AUX_RGBImageRec''), so the `memset'' call doesn''t even get the size
right.
(I know the distinction, but let''s assume
that NULL and 0 is translated as the same
thing for the moment.)



`NULL'' is allowed to expand to `0'', and wherever `NULL'' is correct,
writing `0'' instead is correct as well. In a context where a pointer is
needed, `0'' is treated as a null pointer constant, not as the integer 0.

Martin
--
,--. Martin Dickopp, Dresden, Germany ,= ,-_-. =.
/ ,- ) http://www.zero-based.org/ ((_/)o o(\_))
\ `-'' `-''(. .)`-''
`-. Debian, a variant of the GNU operating system. \_/


Martin Dickopp wrote:

[ snip ]


Secondly, `sizeof (void *)'' (the size of a generic object pointer) need
not be the same as `sizeof (AUX_RGBImageRec *)'' (the size of a pointer
to `AUX_RGBImageRec''), so the `memset'' call doesn''t even get the size
right.



Are you saying that `sizeof (AUX_RGBImageRec *) != sizeof (void *)''?

So, `AUX_RGBImageRec *'' could _never_ be used inside the
compare function of `qsort(3C)'' since its pointer size may
be different than the size of a `void *''?!!!
Stephen


这篇关于指针初始化。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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