指针返回类型是否总是需要在堆上分配?
我是 C++ 新手,目前正在学习堆和堆栈。在我们的类中,我们有一个像这样有问题的函数,
int* Problematic(int capacity) {
int data[capacity];
// do something
return data;
}
我知道除了一个悬空指针之外,它不会返回任何内容(是吗?),因为堆栈在完成执行时会释放函数中的所有内容。我的问题是:
- 这个问题是否会发生在所有返回类型是指针但在堆栈上运行的函数上?(因为它们只是返回一个地址,并且当函数运行完成时堆栈会删除所有内容)?
- 除了将堆栈连接到堆(例如指向堆上的地址)之外,指针还可以做其他事情吗?
- 根据我的理解,栈和堆的区别在于,对于栈来说,不是由程序员手动分配内存,而是由系统自动连续分配内存。所以我想知道我是否有这样的功能
int ReturnInt() {const int a = 5;return a;}
这个函数实际上返回什么,如果它返回一个地址,不会像以前那样堆栈删除5吗?如果这个函数返回一个整数5,那么如何解释内存中的这个整数,因为它只是在RAM中添加了4个字节,并且内存地址仍然发生变化?
非常感谢您的帮助:)
I am new to C++ and I am currently learning Heap and Stack. In our class we had a problematic function like this
int* Problematic(int capacity) {
int data[capacity];
// do something
return data;
}
I know this won't return anything but a dangling pointer(is it?) since stack will deallocate everything in the function when it finishes executing. And here comes my questions:
- Will this problem happened to all the functions which their return type is a pointer but they are running on stack?(Since they just return an address and stack delete every thing when the function finished running)?
- Is there any other things that pointer can do besides connecting stack to heap(Like Pointing to an address on heap)?
- From my understanding, the difference between stack and heap is that for stack, instead of manually allocating memory by programmer, stack allocating memory automatically by system continuously. So I am wondering if I have a function like this
int ReturnInt() {const int a = 5;return a;}
what actually this function returns, if it returns an address, won't stack deleted the 5 as before? If this function returns an integer 5, how to explain this integer in memory since it just add 4 bytes in RAM and changes still happens in memory address?
Thank you so much for help :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
是的。返回本地非静态地址没有什么价值。此类返回的地址无法用于解除引用。您仍然可以
printf("%p\n",(void*)the_address)
它们,但这就是您对它们所能做的一切。 (不过,返回本地static
的地址是有意义的。这样返回的本地地址可以安全地取消引用。)指针可以指向任何东西:全局变量、静态变量,并且它们可以从调用者(例如,可以在堆栈上分配其目标)。
int ReturnInt(){const int a = 5;return a;}
在大多数平台上通过寄存器返回。如果不可能,编译器将确保调用者具有用于返回值的堆栈分配空间。Yes. Returning a local non-static address has little value. Such returned addresses are unusable for dereferencing. You can still
printf("%p\n",(void*)the_address)
them but that's about all you can do with them. (Returning the address of a localstatic
makes sense, though. Such a returned local address is safe to dereference.)Pointers can point to anything: globals, statics, and they can be passed from a caller (who could allocate their target on the stack for example).
int ReturnInt(){const int a = 5;return a;}
returns through a register on most platforms. If that's not possible, the compiler will have made sure the caller has stack-allocated space for the return value.是的,如果它们返回一个指向函数堆栈上分配的内容的指针。
当然。例如,从堆分配的一个对象可能包含指向在堆上分配的另一个对象的指针。
它不返回地址。它的返回类型是int,并且返回整数值5,而不是任何地址。该函数返回值 5。不要求实现将 5 存储在内存中,并且它可能会或可能不会按照自己的意愿这样做。例如,它可以在寄存器中返回值 5。
Yes, if they return a pointer to something allocated on the function's stack.
Sure. For example, one object allocated from the heap might contain a pointer to another object allocated on the heap.
It doesn't return an address. Its return type is
int
and it returns the integer value 5, not any address at all. The function returns the value 5. There is no requirement that the implementation store that 5 in memory and it may or may not do so as it wishes. It could, for example, return the value 5 in a register.#2 问题(不清楚,但这是一个镜头)
你还有第三个存储空间选项 - 静态。这是由您在编译时要求的内容决定的固定大小
buffer
是静态分配的 500 字节#2 Question (not clear but heres a shot)
you also have a third storage space option - static. Thats fixed size determined by what you ask for at compile time
buffer
is 500 bytes statically allocated绝对是的。如果函数返回一个指向其堆栈上分配的数据的指针,则该堆栈在函数退出后将变为无效,并且该指针将指向无效位置。
问题不清楚。
堆栈是内存中分配给函数内部使用的空间。它被保留用于函数调用,并在函数返回后被释放以供任何其他用途。在您的示例中
int ReturnInt() {const int a = 5;return a;}
没有指针。结果按值返回。换句话说,变量a
的值在函数返回之前被复制到函数返回值的位置。这会保留该值并允许在函数返回后使用它。下面的指针不起作用:int *ReturnIntPtr(){int a = 5;返回&a;}
。这里您返回一个指向堆栈的指针,该指针在返回后无效。absolutely. If a function returns a pointer to a data allocated on its stack, the stack becomes invalid after exit from the function and the pointer will point to invalid location.
the question is not clear.
stack is a space in memory which is allocated for internal use of a function. It gets reserved for the function call and is released for any other use after function returns. In your example
int ReturnInt() {const int a = 5;return a;}
there is no pointers. The result is returned by value. In other words, the value of variablea
is copied to the location of the function return value before its return. This preserves the value and allows it to be used after the function return. The following with pointers will not work:int *ReturnIntPtr(){int a = 5; return &a;}
. Here you return a pointer to stack which is not valid after the return.