How is this a pass by value?!

Started by
11 comments, last by noatom 9 years, 9 months ago

Ivalue will obviously remain 0 since we pass by value, and svalue will become bar(because we use a pointer).

That was my initial response without running the code. To my surprise, after running it, i was right about ivalue, but not about svalue.

Looking at the code again, I realized that foo is actually a literal, hence, it cannot be changed at all. Is that why the pointers are copied by value? Is there some kind of rule about 2 pointers refering to the same literal?

Advertisement

Ivalue will obviously remain 0 since we pass by value, and svalue will become bar(because we use a pointer).

That was my initial response without running the code. To my surprise, after running it, i was right about ivalue, but not about svalue.

Looking at the code again, I realized that foo is actually a literal, hence, it cannot be changed at all. Is that why the pointers are copied by value? Is there some kind of rule about 2 pointers refering to the same literal?

svalue is a pointer variable, and a copy of its value is passed to the set function. The set function then assigns the local variable a new value, but the original pointer remains the same.

Now replace the two set functions with these instead that takes the parameters by reference.


void set(char const *&value)
{
    value = "bar";
}

void set(int &value)
{
    value = 42;
}

As I said, pointers are variables that work just like any other type. They just happen to have additional functionality that lets you access other objects through the pointer, but the pointers themselves are just values that are copied like any other value.

edit: There is a huge difference between a pointer, and the object a pointer points to. The pointer is copied, but the pointed-to object is not and can be reassigned across functions.

Triple-facepalm-picard-812.jpg

I can't believe I didn't notice this:

I failed so hard because I was thinking about objects. You know, you have an object, and 2 pointers, a pointer points to it, and you make the second pointer point to it too, and no matter which pointer changes something about the object(using the dereference op, which is not used here, another triple facepalm for me), the other pointer will obviously see the change too.

Here, the situation was more like: the 2 pointers point to the same object, but later one of them is made to point to another different object...

void set(char const *&value)

There you took a reference to a pointer, and of course doing any change inside the function is actually made to the pointer outside the function.

You could've in a way, modify the outside char array if you could use the dereference operator, but since the array is constant(because its a literal), there is no way in which you can change the array.(ONLY if you were to dynamicly allocate it, you could freely change the array) and of course, not take a pointer to a constant value as an argument.

I explained it so if anyone will ever have the same problem as me, will see things the clear way.(MASSIVE HINT: Look for the GODDAMN dereference operator damn it!)

I'm literally cringing in shame when I think about this....oh god....

Thanks Brother Bob smile.png

This topic is closed to new replies.

Advertisement