指针解除引用 [英] pointer dereference

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

问题描述

大家好,

我对指针有一个疑问。我有一个小代码,如下所述提到的



#include< stdio.h>

#include< stdlib.h>

int main(无效)

{

int * p;

int * ptr = malloc(sizeof(int *));

返回0;

}

我的疑问是if(sizeof(int *));未定义?因为p对于任何地方都是点b / b $ b $。所以,当我尝试解除引用时,它会显示未定义的

行为吗?我在阐述它会崩溃。但是在gcc版本3.2.2

20030222(Red Hat Linux 3.2.2-5)没有崩溃。


问候,

Somanath

Hi All,
I have one doubt regarding pointer .I have one small code as
mentioned bellow .

#include<stdio.h>
#include<stdlib.h>
int main (void)
{
int *p;
int *ptr= malloc(sizeof(int *));
return 0;
}
My doubt is if (sizeof(int *)); is undefined ? Because "p" is point
to any where .So when I try to dereference will it show undefined
behavior ?. I was expatiating it will crash .But in gcc version 3.2.2
20030222 (Red Hat Linux 3.2.2-5) not crashing .

Regards,
Somanath

推荐答案

7月12日下午1点55分,somenath< somenath ... @ gmail.comwrote:
On Jul 12, 1:55 pm, somenath <somenath...@gmail.comwrote:

大家好,

我对指针有一个疑问。我有一个小代码为

如下所述。


#include< stdio.h>

#include< stdlib.h>

int main (无效)

{

int * p;

int * ptr = malloc(sizeof(int *));

返回0;}


我的疑问是if(sizeof(int *));未定义?因为p对于任何地方都是点b / b $ b $。所以,当我尝试解除引用时,它会显示未定义的

行为吗?我在阐述它会崩溃。但是在gcc版本3.2.2

20030222(Red Hat Linux 3.2.2-5)没有崩溃。


问候,

Somanath
Hi All,
I have one doubt regarding pointer .I have one small code as
mentioned bellow .

#include<stdio.h>
#include<stdlib.h>
int main (void)
{
int *p;
int *ptr= malloc(sizeof(int *));
return 0;}

My doubt is if (sizeof(int *)); is undefined ? Because "p" is point
to any where .So when I try to dereference will it show undefined
behavior ?. I was expatiating it will crash .But in gcc version 3.2.2
20030222 (Red Hat Linux 3.2.2-5) not crashing .

Regards,
Somanath



这里malloc为int *分配内存,这是2字节因此程序没有

崩溃,这里sizeof(int *)未定义为2字节。

Here malloc allocate memory for int * which is 2 byte thus program not
crashes, here sizeof(int *) is not undefined it is 2 byte.


7月12日,09:55,somenath< somenath ... @gmail。 comwrote:
On 12 Jul, 09:55, somenath <somenath...@gmail.comwrote:

大家好,

我对指针有一个疑问。我有一个小代码为

下面提到。


#include< stdio.h>

#include< stdlib.h>

int main(void)

{

int * p;
Hi All,
I have one doubt regarding pointer .I have one small code as
mentioned bellow .

#include<stdio.h>
#include<stdlib.h>
int main (void)
{
int *p;



你不做任何事情。

You don''t do anything with this.


int * ptr = malloc(sizeof( int *));
int *ptr= malloc(sizeof(int *));



malloc返回一个内存区域的地址,该区域适合保存一个

指向整数的指针。


你已经将该地址放在指向整数的指针中,而不是指向

指向整数的指针。


逻辑上你应该声明为int ** ptr = malloc(sizeof(int

*));"

malloc returned the address of a region of memory suitable to hold a
pointer to integer.

You have put that address in a pointer to integer, not a pointer to
pointer to integer.

Logically you should have declared "int **ptr = malloc(sizeof(int
*));"


return 0;}


我的疑问是if(sizeof(int *));未定义?
return 0;}

My doubt is if (sizeof(int *)); is undefined ?



" int *"是一种有效的数据类型,具有已知的大小。为什么这将是

undefined?

"int *" is a valid data type, with a known size. Why would this be
undefined?


因为p对于任何地方都是点b / b $ b $。所以,当我尝试解除引用时,它会显示未定义的

行为吗?
Because "p" is point
to any where .So when I try to dereference will it show undefined
behavior ?.



什么有p和这里的任何事都有关系吗?你不做任何事情

" p"在这个示例代码中完全没有 - 取消引用它。

What has "p" got to do with anything here? You don''t do anything with
"p" at all in this sample code - certainly not dereference it.


我正在阐述它会崩溃。
I was expatiating it will crash .



所以你不期待未定义的行为:-)


这是一个特别令人困惑的帖子 - 示例代码完全是没有意义的。当你写下

时,你真正想要了解的是什么呢?

So you weren''t expecting undefined behaviour :-)

This is a particularly confusing post - the sample code is totally
pointless. What were you actually trying to understand when you wrote
it?


7月12日,10:01,Anurag< anurag ... @ gmail.comwrote:
On 12 Jul, 10:01, Anurag <anurag...@gmail.comwrote:

7月12日下午1点55分,somenath< somenath ... @ gmail.comwrote:
On Jul 12, 1:55 pm, somenath <somenath...@gmail.comwrote:

大家好,

我对指针有一个疑问。我有一个小代码,如下所述提到的

Hi All,
I have one doubt regarding pointer .I have one small code as
mentioned bellow .


#include< stdio.h>

#include< stdlib.h>

int main(void)

{

int * p;

int * ptr = malloc(sizeof(int *));

返回0;}
#include<stdio.h>
#include<stdlib.h>
int main (void)
{
int *p;
int *ptr= malloc(sizeof(int *));
return 0;}


我的疑问是if(sizeof(int *));未定义?因为p对于任何地方都是点b / b $ b $。所以,当我尝试解除引用时,它会显示未定义的

行为吗?我在阐述它会崩溃。但是在gcc版本3.2.2

20030222(Red Hat Linux 3.2.2-5)没有崩溃。
My doubt is if (sizeof(int *)); is undefined ? Because "p" is point
to any where .So when I try to dereference will it show undefined
behavior ?. I was expatiating it will crash .But in gcc version 3.2.2
20030222 (Red Hat Linux 3.2.2-5) not crashing .


问候,

Somanath
Regards,
Somanath



这里malloc分配内存为int *


Here malloc allocate memory for int *



True

True


即2字节
which is 2 byte



不一定 - 在我使用的任何机器上都不是2个字节。 (如果

你感兴趣,而且你不需要,那么一些就是4字节,而其他一些则是8

字节)。

Not necessarily - it''s not 2 bytes on any machine I work with. (If
you''re interested, and you need not be, it''s 4 bytes on some and 8
bytes on some others).


因此程序没有
崩溃,这里sizeof(int *)未定义为2字节。
thus program not
crashes, here sizeof(int *) is not undefined it is 2 byte.



没有它的sizeof(int *)无论大小可能是特定的

实现。


如果sizeof(int *)没有被定义,那么该程序不会编译。

崩溃与此问题无关。

No it''s sizeof(int *) whatever that size might be for a particular
implementation.

If sizeof(int *) wasn''t "defined" the program would not have compiled.
Crashing is not relevant to this question.


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

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