Can a C++ class determine whether it's on the stack or heap?

后端 未结 15 2188
有刺的猬
有刺的猬 2020-12-13 04:14

I have

class Foo {
....
}

Is there a way for Foo to be able to separate out:

function blah() {
  Foo foo; // on the stack
         


        
15条回答
  •  粉色の甜心
    2020-12-13 04:18

    Take a look at the program here: http://alumni.cs.ucr.edu/~saha/stuff/memaddr.html. With a few casts, it ouputs:

            Address of main: 0x401090
            Address of afunc: 0x401204
    Stack Locations:
            Stack level 1: address of stack_var: 0x28ac34
            Stack level 2: address of stack_var: 0x28ac14
            Start of alloca()'ed array: 0x28ac20
            End of alloca()'ed array: 0x28ac3f
    Data Locations:
            Address of data_var: 0x402000
    BSS Locations:
            Address of bss_var: 0x403000
    Heap Locations:
            Initial end of heap: 0x20050000
            New end of heap: 0x20050020
            Final end of heap: 0x20050010
    

提交回复
热议问题