简体   繁体   English

内存中取消引用的未初始化指针的位置?

[英]Location of a dereferenced uninitialized pointer in memory?

I have this code example in c from an introductory Embedded system course quiz :我在介绍性的嵌入式系统课程测验中使用 c 语言编写了此代码示例:

#include <stdlib.h>
#include <stdint.h>

//cross-compiled for MSP432 with cortex-m0plus
int main() {

    int * l2;

    return 0;
}

I want to know the memory segment ,sub-segment, permissions and lifetime of * l2 in memory.我想知道 * l2在内存中的内存段、子段、权限和生命周期。

What I understand is that the pointer l2 is going to be allocated in the stack sub-segment first then because it's uninitialized it's going to get a garbage value which is in this case any value it finds in the stack;我的理解是,指针 l2 将首先在堆栈子段中分配,然后因为它未初始化,它将获得一个垃圾值,在这种情况下,它是它在堆栈中找到的任何值; I assumed it was in the .text or .const with a static lifetime and none of these answers were right, so am I missing something here ?我认为它在具有静态生命周期的 .text 或 .const 中,并且这些答案都不正确,所以我在这里遗漏了什么吗?


Edit:编辑:

After I passed the quiz without solving this point correctly, the solution table says it's in the heap with indefinite lifetime .在我没有正确解决这一点的情况下通过了测验后,解决方案表说它在堆中,生命周期不确定 what i got from this answer is that : because a pointer itself is stored in stack and the object it points to is uninitialized (it's not auto or static), it's stored in the heap.. I guess ??我从这个答案中得到的是:因为指针本身存储在堆栈中并且它指向的对象未初始化(它不是自动或静态的),它存储在堆中..我猜?

It depends on the implementation.这取决于实现。

Usually as it is local automatic variable it will be located on the stack.通常因为它是局部自动变量,所以它会位于堆栈上。 Its lifetime is the same as lifetime of the main function.它的生命周期与main函数的生命周期相同。 It can be only accessed from the main function.它只能从主函数访问。

But in real life as you do not do anything with it, it will be just removed by the compiler as not needed even if if you compile it with no optimizations https://godbolt.org/z/1Y6W5j .但在现实生活中,由于您没有对它做任何事情,即使您在没有优化的情况下编译它,它也会被编译器删除,因为不需要https://godbolt.org/z/1Y6W5j In this case its location is "nowhere"在这种情况下,它的位置是“无处”

Objects can be also kept in the registers and not be placed in the memory https://godbolt.org/z/8nWxxz对象也可以保存在寄存器中,而不是放在内存中https://godbolt.org/z/8nWxxz

Most modern C implementations place code in the .text segment, initialized static storage location variables in the .data segment, not initialized static storage location variables in the .bss segment and read only data in the .rodata segment .大多数现代 C 实现将代码放在.text段中,在.data段中初始化静态存储位置变量,在.bss段中未初始化静态存储位置变量,而在.rodata段中只读取数据。 You may have plenty other memory segments in your program - but there are so many options.您的程序中可能有很多其他内存段 - 但有很多选择。 You can also have your own segments and place objects there.您也可以拥有自己的段并将对象放置在那里。

Stack and heap location are 100% implementation defined.堆栈和堆位置是 100% 实现定义的。

The value stored in l2 is indeterminate - it can even be a trap representation.存储在l2的值是不确定的——它甚至可以是一个陷阱表示。 The l2 object itself has auto storage duration and its lifetime is limited to the lifetime of the enclosing function. l2对象本身具有auto存储持续时间,其生命周期仅限于封闭函数的生命周期。 What that translates into in terms of memory segment depends on the specific implementation.在内存段方面转换成什么取决于具体的实现。

You can't say anything about the value of *l2 , unless your specific implementation documents exactly how uninitialized pointers are handled.你不能说有关的任何有价值的东西*l2 ,除非你的具体实施文件,究竟是如何初始化的指针处理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM