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

后端 未结 15 2208
有刺的猬
有刺的猬 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:36

    A more direct, and less intrusive method would be to look up the pointer in the memory region maps (such as /proc//maps). Each thread has a region allocated to its stack. Static and global variables will live in the .bss section, constants in a rodata or const segment, and so on.

提交回复
热议问题