Jayden Rice
Jayden Rice

Reputation: 301

Where are these nullptr located?

int* x = nullptr;
class_example* obj = nullptr;

I understand what nullprt is, but where are these variables x and obj located?

Heap? or Stack?

Upvotes: 1

Views: 97

Answers (2)

eerorika
eerorika

Reputation: 238361

int* x = nullptr;
class_example* obj = nullptr;

where are these variables x and obj located?

These variables have static storage duration, because they are declared in the namespace scope without thread_local keyword. The language standard does not specify where the objects will be located. It is up to the language implementation to decide.

Heap? or Stack?

Typically, neither.

For example, in the ELF executable format, zero initialised static variables would be located in memory segment named BSS.

Upvotes: 1

cigien
cigien

Reputation: 60228

Pointers are just ordinary variables, that happen to have values that are addresses of other objects (and those addresses could be on the heap).

So in this snippet:

int main() 
{
  int* x = nullptr;
  class_example* obj = nullptr;
}

just like regular local variables, these pointers will be located on the stack.

Upvotes: 5

Related Questions