I know that '*' is used to define a pointer at the time of creating a variable, for example:
objeto* variable;
But, in this case from a university exercise:
function_init(tPopularity* object) {
tPopularity newPopu;
/*Relleno de de datos, no importante para la pregunta*/
object=&newPopu;
}
My initial idea was to leave is the one above, that is:
object=&newPopu;
The problem is that when exiting the function the data of the object was lost, so what I have done, without knowing very well why it is:
*object=newPopu;
And works! My data is not lost and the tests pass correctly... now my question is... Why are my results now as expected? Why was the data corrupted and why not now? What does that asterisk in front of an assignment mean in my code?
If we use variables it looks better:
It doesn't matter how many times you execute the above code, it will always print
0
and the reason is very simple:var
it is a copy ofa
, so the changes we make tovar
it will not be echoed invar
. It's like cloning a sheep... shearing the clone will not automatically shear the original sheep.Now let's see the same example but with pointers:
The program prints again
0
... But we are using pointers! ... already, but as in the previous case,var
it is a copy ofptr
. The only thing that both pointers share is that they point to the same memory address, but they are still independent variables.And this is where the trick comes in . What it does
var=10
is modify the address pointed to byvar
. After this change, itptr
will point to one memory address anda
a different one.Now instead, imagine we change the example a bit:
Now the program, by magic, will print a 10!!!
What happened now?
What happens now is that, thanks to the use of the asterisk, we are modifying the memory shared by both pointers,
var
andptr
.That is why this change is propagated outside the function.
Having said this, I take this opportunity to clarify a small detail: In your first case, it is not that the data was corrupted... it is that you were not modifying the original memory , which remained unchanged as it was when the function was called.
Why was the data corrupted and why not now?
The data was corrupted because in your
function_init
you were assigning the memory address of the variablenewPopu
to the pointerobject
, the content of that address was pointed to by both variables, but when exiting the function that content was removed just likenewPopu
and thereforeobject
not pointed to the data you expected. When you do*object = newPopu
this by assigning the content of the variable and not the memory address it is in, then you have two references to that content, one isobject
and one isnewPopu
, when you exit the function itnewPopu
ceases to exist butobject
still points to an address of memory that contains the value that was assigned inside the functionfunction_init
What does that asterisk in front of an assignment mean in my code?
That asterisk indicates that you are going to access the content at the memory address that is pointed to by your variable, in this case
object
.Let's imagine the following:
Let's assume the memory addresses that our memory block will have (this block is basically where the
a
and members will be storedb
) that the implicit pointernewPopu
(delfunction_init
) points to.Now, with this data, let's start by seeing how the following line works:
We are clear that this statement is equivalent to this:
What you are assigning to the pointer
object
(in this case0x4
) is the first memory address (in this case0x4
) of the block A that it points tonewPopu
, but then we realize that the pointerobject
is a parameter and that later, when the functionfunction_init
finishes its execution , the memory space where the pointer is locatedobject
, will be freed from the program and also block A. In short, your function caused it to lose all the information that block A has and that is the reason why the data corruption happens .Now the solution to this problem is the one you proposed:
Now I'll add a little code to reflect why:
What this simple code does is copy all the data that the object (pointed
newPopu
to) has to the object pointed toob
(delmain
).And to make this clearer, the code:
It is equivalent to :
And with this we check WHY data corruption does not occur.
I hope my explanation helps you! Cheers!