About Memory and Refences - Suggestion

Hi there this course is good so far but I this topic I got some question marks like how the a changed as refa initilazed to b. If there was a grafical version how do values are change it will make more sense and will be easy to understand.

Is there is any chance to make it like that, like showing the adresses and chaning values by… Something like that

Edit: I tried to figure it out from the lesson and I sketch someting, I don’t know is it correct ?


I hope this one is correct and it will be helpfull for others who get confused

Are you able to type that up? I’m having difficulty reading it due to it being rather faint (as well as your handwriting no offense :sweat_smile:)

Thats okay I don’t have good hand writing. I will Draw and text and than send it again.

Untitled
For example this is the memory. (I don’t know how exactly its works just theory)

In VS code

int32 a = 0;
int32& refa = a;
int b = 4;

If we write that up, in the memory there is will be an adress that a and refa signed. For example we will say ‘0x000’

This is the reason if we change value of refa also a’s value will change as well:

refa = b; // or refa = (hard value). Same as a 

However if we try change the value of b just after refa = b; this will not change value of a and refa(or something like that.)

Reason of why refa does not affect, b is in the another memory adress for example 0x0050.

I don’t know is it exactly right I just tried to solve out how its work. I’t will be really nice if the lesson grafically shows to us how the memory works.

I hope I did well and explained. Also I hope it was clear.
at least I tried my best.

That’s basically it, yes. Every object has to live somewhere in memory and they have an address to that location.

int is typically 32-bits so if you have

int a = 4;
int b = 10;
int& ref = a;

Then a could live at 0x00 and b at 0x20.
refa would then be referring to the memory address 0x00. You can think of references as aliases, refa is just another way to say a. So any writes to a or refa are to that same memory address.

1 Like

That explanation helped me a lot, thanks for your time

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.

Privacy & Terms