将本地炭指针返回数组不会导致segmantation故障
为什么此代码不引起细分故障? 我认为函数返回后,将删除本地变量的内容。
char* test(){
char buffer[BUFSIZ] = "Hello";
char* word = buffer;
return word;
}
int* test2(){
int x = 10;
int *ptr = &x;
return ptr;
}
int main() {
char* str = test();
printf("str : %s\n", str);
int *ptr = test2();
printf("ptr : %d\n", *ptr);
return 0;
}
Why doesn't this code cause segmentation fault ?
I thought after function returns, the content of local variables will be deleted.
char* test(){
char buffer[BUFSIZ] = "Hello";
char* word = buffer;
return word;
}
int* test2(){
int x = 10;
int *ptr = &x;
return ptr;
}
int main() {
char* str = test();
printf("str : %s\n", str);
int *ptr = test2();
printf("ptr : %d\n", *ptr);
return 0;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
该程序具有不确定的行为,因为在退出功能后,功能中声明的自动存储持续时间的变量尚未活着。
该程序可以输出预期的结果,因为变量占据的内存尚未覆盖。
例如,如果要更改程序以下方式
,则其输出可能看起来像是
函数
test1
的调用,导致覆盖记忆的记忆,由指向指针指向的阵列占用的阵列> str
。The program has undefined behavior because the variables with automatic storage duration declared in the functions are not alive after exiting the functions.
The program can output the expected result because the memory occupied by the variables was not yet overwritten.
If to change your program for example the following way
then its output might look like
That is the call of the function
test1
results in overwriting of the memory early occupied by the array pointed to by the pointerstr
.C没有这样的规则。它没有“删除”操作。
创建对象时,为其保留内存。这只是意味着可以安排一些特定的内存来存储对象的值,而其他任何内容都不会使用该内存。在典型的C实现中,硬件堆栈上的某些内存用于自动对象(自动对象(变量的对象定义了不是静态或线程局部的函数)。
当对象的寿命结束时,就像函数返回时,该内存不再保留。
这就是C标准的所有保证:内存不再保留。它不能保证将删除内存,清除内存或立即为其他目的重用内存。这就像在酒店房间里退房一样:您退房的那一刻,酒店的客房工作人员可能会冲进房间并清理房间,或者他们在做其他工作时可能会离开它。
当您使用不再保留的内存时,C标准不会定义行为。此外,如果您有指向对象的指针,则如果在对象结束后使用指针的值,则C标准将无法定义行为。
当C标准无法定义行为时,它对程序的行为没有任何要求。如果程序将数据留在内存中并在尝试使用时获取其值,则符合C标准。如果程序在函数退出时覆盖内存并且对象的值已消失,则还符合C标准。通常,C环境不会为您管理事情;编写符合规则的代码是您的工作。
C has no such rule. It has no “delete” operation.
When an object is created, memory is reserved for it. This just means arrangements are made that some particular memory is available to store the value of the object and nothing else will use that memory for anything else. In typical C implementations, some memory on the hardware stack is used for automatic objects (the objects of variables defined inside functions that are not static or thread-local).
When the lifetime of the object ends, as when a function returns, that memory is no longer reserved.
That is all the C standard guarantees: That the memory is no longer reserved. It does not guarantee that the memory is erased, that the memory is cleared, or that the memory is immediately reused for another purpose. It is like checking out of a hotel room: The moment you check out, the hotel housekeeping staff might rush into the room and clean it up, or they might leave it while doing other work.
The C standard does not define the behavior when you use memory that is no longer reserved. Further, if you have a pointer to an object, the C standard does not define the behavior if you use the value of the pointer after the lifetime of the object ends.
When the C standard does not define the behavior, it imposes no requirements on how the program behaves. If the program leaves the data in memory and fetches its value when you try to use it, that conforms to the C standard. If the program overwrites the memory as the function is exiting and the value of the object is gone, that also conforms to the C standard. Generally, the C environment will not manage things for you; it is your job to write code that complies with the rules.