简体   繁体   English

如果没有指针,堆栈上的内存如何识别?

[英]How is memory on the stack identified if it doesn't have pointers?

Correct me if I'm wrong but pointers are identifiers (an ID) given to each bit of memory. 如果我错了,请纠正我,但指针是给每个内存位的标识符(ID)。 If the memory on the heap has pointers it can be identified which gives the code ability to delete, modify or even change it's size. 如果堆上的内存有指针,则可以识别它,使代码能够删除,修改甚至更改它的大小。 The confusion comes when I've read a book saying stack doesn't have pointers. 当我读到一本书说堆栈没有指针时,就会出现混乱。

I've been reading the book 'Head First C', and it was talking about pointers. 我一直在读“Head First C”这本书,它正在谈论指针。 Was it simply refering to pointers just as an example? 它只是简单地引用指针作为一个例子吗? Or does the memory on the stack have some kind of pointer to memory? 或者堆栈上的内存是否有某种指向内存的指针?

If not, how is it identified? 如果没有,它是如何识别的? The confusion or contradiction between web posts and books have really confused me on a subject I am only just understanding. 网络帖子和书籍之间的混淆或矛盾让我对一个我只是理解的主题感到困惑。 Can anyone clear this up? 任何人都可以清除这个吗?

Every memory location has an address . 每个内存位置都有一个地址 There is nothing like the idea you suggest that stack memory does not have addresses. 没有什么比你建议堆栈内存没有地址的想法。

A pointer value is the address of a memory location, and a pointer variable is a variable that can store a pointer value. 指针值是存储器位置的地址,指针变量是可以存储指针值的变量。

Data in the stack is referenced by an offset from the stack/base pointer. 堆栈中的数据由堆栈/基址指针的偏移量引用。

When your program is executed, a chunk of memory is allocated by the OS for its purposes. 执行程序时,操作系统会为其目的分配一块内存。 The stack will then be used by your program to store data that is required for normal execution. 然后程序将使用该堆栈来存储正常执行所需的数据。 Note that this chunk of memory may not be sequential in the physical memory but a virtual memory space mapped by the OS. 请注意,这块内存可能不是物理内存中的顺序内存,而是OS映射的虚拟内存空间。

When a variable is defined locally in a function, the compiler will generate code that reads a chunk of data in the stack that is used to represent your variable. 当一个变量在函数中本地定义时,编译器将生成代码,该代码读取堆栈中用于表示变量的一大块数据。 Care is taken by the compiler to ensure multiple variables will not occupy the same space. 编译器要小心确保多个变量不会占用相同的空间。 Note that a optimising compiler can still use the same space for several variables if previous definitions are deemed as useless within the execution flow (Not to be confused with lifetime validity of a variable in C++). 请注意,如果先前的定义在执行流程中被认为是无用的,那么优化编译器仍然可以为多个变量使用相同的空间(不要与C ++中变量的生命周期有效性混淆)。

This process however, is only applicable when you know exactly how much memory you need at compile time, meaning the compiler will be able to generate an offset for each named variable defined in your program. 但是,只有在编译时确切知道需要多少内存时,此过程才适用,这意味着编译器将能够为程序中定义的每个命名变量生成偏移量。 This means that there is no need to explicitly read a memory location, aka dereferencing of pointers. 这意味着不需要显式读取内存位置,也就是解引用指针。 However, this does not mean that the variable itself does not reside in a memory location and does not have an address that can reference it. 但是,这并不意味着变量本身不驻留在内存位置,也没有可以引用它的地址。

When writing programs that require dynamic memory allocation, you will have to explicitly tell the OS to allocate more memory to your program. 编写需要动态内存分配的程序时,必须明确告诉操作系统为程序分配更多内存。 Since this memory location is dynamic, the address will not be known at compile time, meaning data access can only be done via dereferencing memory locations. 由于此存储器位置是动态的,因此在编译时将不知道该地址,这意味着只能通过解除引用存储器位置来完成数据访问。 In this case, it will be the additional chunk of memory allocated by the OS. 在这种情况下,它将是OS分配的额外内存块。

Last but not least, compilers can and will always try generate code that stores variables in registers, hence referencing a variable can be very different depending on the code generated. 最后但并非最不重要的是,编译器可以并且将始终尝试生成将变量存储在寄存器中的代码,因此根据生成的代码,引用变量可能会有很大差异。

暂无
暂无

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

相关问题 函数指针如何指向内存中尚不存在的内容? 原型为何有不同的地址? - How can pointers to functions point to something that doesn't exist in memory yet? Why do prototypes have different addresses? 如何编译以减少 memory /STACK 似乎没有任何改变? - how to compile to reduce memory /STACK doesn't seem to change anything? 删除带有指针列表的对象并不能真正释放相应的内存 - Deleting an object with a list of pointers doesn't really free the corresponding memory 我有一个指向对象的向量。 如何释放记忆? - I have a vector of pointers to objects. How to free memory? 如果可能,在堆栈内存中创建一个指向映射的指针数组 - Create an array of pointers to maps in stack memory, if possible 创建新指针循环时,输出取消引用的指针不会增加内存使用量 - When creating a loop of new pointers, outputting the dereferenced pointer doesn't increase memory usage C++ 你能设计一个数据结构,将指针保存在连续的内存中并且不会使它们失效吗? - C++ can you design a data structure that keeps pointers in contiguous memory and doesn't invalidate them? 如何存储指向 class 不拥有的对象的指针? - How to store pointers to objects that class doesn't own? 指向结构的指针不起作用 - Pointers to struct doesn't work 继承和指针指针:为什么它不起作用,我该如何解决它? - Inheritance and pointers to pointers: why doesn't it work and how do I get around it?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM