结构和指针 [英] Structures and Pointers

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

问题描述

我很困惑为什么下面的代码不会产生分段

错误。它确实有效,我在屏幕上输出12。我想b $ b我以为我需要使用malloc获取内存吗?


我注意到如果我使用malloc,然后免费(ptr),我仍然可以使用

ptr-> z并成功分配给z?


struct myStruct {

char x [10];

char y [1000];

int z;

};


int main(。 。){$ / $

struct myStruct * ptr;


ptr-> z = 12;

printf( %d,prt-> z);


}

I am confused as to why the code below does not produce a segmenation
fault. It actually works, and I get 12 outputted on my screen. I
would have thought I needed to get memory using malloc?

I noticed also if I do use malloc, then free(ptr), I can still use
ptr->z and assign to z successfully?

struct myStruct {
char x[10];
char y[1000];
int z;
};

int main(..) {

struct myStruct *ptr;

ptr->z = 12;
printf("%d",prt->z);

}

推荐答案

Sally< wo**************@yahoo.com>潦草地写道:
Sally <wo**************@yahoo.com> scribbled the following:
我很困惑为什么下面的代码不会产生分段错误。它确实有效,我在屏幕上输出12。我想我需要使用malloc来获取内存吗?


它是偶然的。代码调用未定义的行为,但

未定义的行为不会自动意味着分段

错误。如果你在不同的情况下尝试它,它可能很好

崩溃。

我注意到如果我使用malloc,然后免费(ptr),我仍然可以使用
ptr-> z并成功分配给z?


同样的推理适用。

struct myStruct {
char x [10];
char y [1000];
int z;
};
int main(..){
struct myStruct * ptr;
ptr-> z = 12;
printf("%d",prt-> z);
}
I am confused as to why the code below does not produce a segmenation
fault. It actually works, and I get 12 outputted on my screen. I
would have thought I needed to get memory using malloc?
It works by accident. The code invokes undefined behaviour, but
undefined behaviour does not automatically mean segmentation
faults. If you try it in different circumstances, it might very well
crash.
I noticed also if I do use malloc, then free(ptr), I can still use
ptr->z and assign to z successfully?
The same reasoning applies.
struct myStruct {
char x[10];
char y[1000];
int z;
}; int main(..) { struct myStruct *ptr; ptr->z = 12;
printf("%d",prt->z); }




-

/ - Joona Palaste(pa*****@cc.helsinki.fi) -------------芬兰-------- \

\-- http://www.helsinki.fi/~palaste ------------------- - 规则! -------- /

这不是最胖的生存,而是适者生存。

- Ludvig von Drake



--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"It''s not survival of the fattest, it''s survival of the fittest."
- Ludvig von Drake


" Sally" < WO ************** @ yahoo.com>在消息中写道

news:30 ************************** @ posting.google.c om ...
"Sally" <wo**************@yahoo.com> wrote in message
news:30**************************@posting.google.c om...
我很困惑为什么下面的代码不会产生分段错误。


这不是必需的。

它实际上有效


由于(不幸)意外。这一次。

我在屏幕上输出12。我想我需要使用malloc来获取内存吗?


如果你希望你的程序的行为是明确定义和可预测的,你会这样做,并且保证来自

关于它的行为的语言标准。

我注意到如果我使用malloc,然后免费(ptr),我仍然可以使用
ptr-> z并成功分配给z? br />
struct myStruct {
char x [10];
char y [1000];
int z;
};
int main(..){

struct myStruct * ptr;

ptr-> z = 12;
printf("%d",prt - > z);

}
I am confused as to why the code below does not produce a segmenation
fault.
It''s not required to.
It actually works
By (unfortunate) accident. This time.
and I get 12 outputted on my screen. I
would have thought I needed to get memory using malloc?
You do if you want your program''s behavior to be
well-defined and predictable, with guarantees from
the language standard about its behavior.
I noticed also if I do use malloc, then free(ptr), I can still use
ptr->z and assign to z successfully?

struct myStruct {
char x[10];
char y[1000];
int z;
};

int main(..) {

struct myStruct *ptr;

ptr->z = 12;
printf("%d",prt->z);

}




以上是未定义行为的示例。根据语言标准

,编译器可以生成

代码* *。这可能会从暴力的b $ b崩溃到看似工作(理论上这些事情,因为无处不在的鼻子恶魔)。在你的情况下,工作似乎是b $ b。下一次它可能会做一些完全不同的事情。

墨菲定律规定这将在申请

首先向潜在客户证明。


道德:不要这样做。


您还有另一个未定义行为的情况:

调用''printf()'',范围内没有原型。


你需要一个return语句for main()。


-Mike



The above is an example of ''undefined behavior''. According to
the language standard, the compiler is allowed to produce
code that does *anything*. This can vary from a violent
crash, to ''seeming to work'' (and theoretically such things
as the ubiquitous ''nasal demons''). In your case, it ''seems
to work.'' Next time it might do something completely different.
Murphy''s law dictates that this will happen when the application
is first demonstrated to a potential client.

Moral: Don''t Do That.

You also have another case of undefined behavior:
invocation of ''printf()'' with no prototype in scope.

And you need a return statement for main().

-Mike


wo ************** @ yahoo.com (莎莉)在

新闻中写道: 30**************************@posting.google.c om:
wo**************@yahoo.com (Sally) wrote in
news:30**************************@posting.google.c om:
我很困惑至于为什么下面的代码不会产生分段错误。它确实有效,我在屏幕上输出12。我想我需要使用malloc来获取内存吗?


你有幸(非)幸运。在这种情况下,您确实需要使用malloc()。你做了什么

是错的。

我也注意到如果我使用malloc,然后免费(ptr),我仍然可以使用
ptr-> z并成功分配给z?


再次,不要这样做。如果按预期工作,那只是运气。

struct myStruct {
char x [10];
char y [1000];
int z;
};

int main(..){

struct myStruct * ptr;

ptr-> z = 12;


请*不*这样做。

printf("%d",prt-> z);

}
I am confused as to why the code below does not produce a segmenation
fault. It actually works, and I get 12 outputted on my screen. I
would have thought I needed to get memory using malloc?
You got (un)lucky. You do need to use malloc() in this case. What you did
is wrong.
I noticed also if I do use malloc, then free(ptr), I can still use
ptr->z and assign to z successfully?
Again, do not do this. If it works as hoped it''s just luck.
struct myStruct {
char x[10];
char y[1000];
int z;
};

int main(..) {

struct myStruct *ptr;

ptr->z = 12;
Do *not* do this.
printf("%d",prt->z);

}




-

- 马克 - >

-



--
- Mark ->
--


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

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