1

When I tried to find where Pointers store themselves, their addresses, and address values. The same with References.

Example 1:

int a = 1;
int *b = new int{ 5 };
int &c = a;

I know that a with 1 are stored on the stack, but are they in the same memory block?

I have information, that *b with a reference to value 5 are stored on the stack, but the value 5 is stored in the heap.

I heard that &c with a reference to a isn't stored in the memory because it's an alias. But other people said another thing, so the same question as for *b, how are they all stored in memory?

I think it would be much more interesting to know deeper about this topic.

Example 2:

void Func1(int a)
{
// code
}

void Func2(int *b)
{
// code
}

void Func3(int &c)
{
// code
}

I know that argument a is copied to the function memory on the stack.

I have information that *b is copied to the function memory on the stack, but copied only *b, not its reference to the value which is stored in the heap.

I heard that &c don't copy because the function uses the value located on the heap directly through this reference.


How do examples 1 and 2 actually work in memory?

I tried to find answers to this question using Google, Bing AI and ChatGPT, but answers vary.

My compiler is x64 MSVC 14.35.32215

fzyier
  • 103
  • 5
  • 2
    `a` *might* be on the stack. There's not enough context to say. Also note that the C++ specification doesn't actually specify exactly where variables are stored, it's just that it's easy for compiler to store local variables on the stack. – Some programmer dude Mar 07 '23 at 08:10
  • 1
    Regarding `b`, it's the variable `b` itself that might be stored on the stack. It's value is then the memory address that was allocated by `new`. So `b` is the variable on the stack, `*b` is the value `5` stored on the heap. – Some programmer dude Mar 07 '23 at 08:12
  • Not quite a duplicate but might answer your question: https://stackoverflow.com/a/10157210/1968 – Konrad Rudolph Mar 07 '23 at 08:32
  • 4
    Of course answers with Bing AI and ChatGPT vary - they just make stuff up. There is no reason to expect any of them to be correct about anything. – molbdnilo Mar 07 '23 at 09:16

2 Answers2

0

As you mentioned, the types int and int* are stored on the stack. A reference is an alias, and as such, may not require any additional storage, depending on compiler implementation.

Phillip Ngan
  • 15,482
  • 8
  • 63
  • 79
0

First, address of a variable is just an integer. Now, look into your first example:

int a = 1;
int *b = new int{ 5 };
int &c = a;

a is on stack, it takes 4 bytes. The bytes represent value 1 in binary format, this reference visualizes how is an integer stored in memory.

About b, you're right. b is a pointer, it has its own address on stack and takes 4 bytes (or 8 bytes depending on OS, see here https://learn.microsoft.com/en-us/cpp/cpp/raw-pointers?view=msvc-170). The 4 bytes represent an integer value, which is address of 5 lying on heap (new int{ 5 }).

About c, it's exactly an alias of a, C++ might need memory to help other parts of your program aware of the existence of the alias.

From here, hope you can self-answer your questions.

TaQuangTu
  • 2,155
  • 2
  • 16
  • 30
  • While a reference is an alias, it is tied to a specific address of what's aliased. Very similar to the way a const ptr is tied to a specific address. For example, `int& ri = v[0];;` where v is a vector of ints is not an alias of v[0] if one pushes an int onto v because the location of `v[0]` may well be changed. Be very careful when thinking of a reference as an alias. There be dragons. – doug Mar 07 '23 at 20:23