Jump to content

  • Log In with Google      Sign In   
  • Create Account


How is this a pass by value?!


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
12 replies to this topic

#1 noatom   Members   -  Reputation: 782

Like
0Likes
Like

Posted 01 July 2014 - 04:31 PM

// maximum of two C-strings (call-by-value) 
inline char const* max (char const* a, char const* b) 
{ 
    return std::strcmp(a,b) < 0 ? b : a; 
} 

// maximum of three values of any type (call-by-reference) 
template <typename T> 
inline T const& max (T const& a, T const& b, T const& c) 
{ 
    return max (max(a,b), c); // error, because max(a,b) uses call-by-value 
} 

int main () 
{ 
    

    const char* s1 = "frederic"; 
    const char* s2 = "anica"; 
    const char* s3 = "lucas"; 
    ::max(s1, s2, s3); // ERROR 

} 

So when the 3 parameter function is called, it takes a constant reference to the listed pointers. So basically inside the function, a would be a constant reference to a constant pointer.

 

Then the 2 parameter max function is called, which takes a constant char pointer(WHY IS IT THEN CONSIDERED BY VALUE, when clearly we see it takes pointers, and not values!)

 

Its kinda late here, maybe I'm interpreting this in a very stupid way, can someone clear it up for me?


Edited by noatom, 01 July 2014 - 05:27 PM.


Sponsor:

#2 Chris_F   Members   -  Reputation: 2207

Like
0Likes
Like

Posted 01 July 2014 - 04:48 PM

Your code generates no errors for me, only a warning about returning a reference to a local variable. What exactly is the purpose of this code and why have you written it this way with templates and references?


Edited by Chris_F, 01 July 2014 - 04:48 PM.


#3 Chris_F   Members   -  Reputation: 2207

Like
0Likes
Like

Posted 01 July 2014 - 04:56 PM

The following works for me:

 

#include <iostream>
#include <algorithm>
#include <cstring>

template<class T>
constexpr const T& max(const T& a, const T& b, const T& c)
{
    return std::max(std::max(a, b), c);
}

inline const char* max(const char* a, const char* b)
{
    return std::strcmp(a,b) < 0 ? b : a;
}

int main ()
{
    const char* s1 = "frederic";
    const char* s2 = "anica";
    const char* s3 = "lucas";
    std::cout << max(s1, s2, s3);
}


#4 noatom   Members   -  Reputation: 782

Like
0Likes
Like

Posted 01 July 2014 - 04:59 PM

Its from a template book, more exactly: Adison Wesley's C++ Templates The Complete Guide.

 

In the chapter he's showing template functions and overloading, and he gave this example. He was trying to show how writing too many overloads can cause a headache and make mistakes in code, aka returning a reference to a local.

 

But the problem is, he says in the commented code that the two arg function is a call by value, which is not! I'll give you the full code.

// maximum of two values of any type (call-by-reference) 
template <typename T> 
inline T const& max (T const& a, T const& b) 
{ 
    return a < b ? b : a; 
} 

// maximum of two C-strings (call-by-value) 
inline char const* max (char const* a, char const* b) 
{ 
    return std::strcmp(a,b) < 0 ? b : a; 
} 

// maximum of three values of any type (call-by-reference) 
template <typename T> 
inline T const& max (T const& a, T const& b, T const& c) 
{ 
    return max (max(a,b), c); // error, if max(a,b) uses call-by-value 
} 

int main () 
{ 
    ::max(7, 42, 68); // OK 

    const char* s1 = "frederic"; 
    const char* s2 = "anica"; 
    const char* s3 = "lucas"; 
    ::max(s1, s2, s3); // ERROR 

} 

But I just noticed another thing: Why wouldn't the 3 parameter function call the first function, which takes its parameters by reference?! the 3 parameter function itself has its parameters by reference, so it would only make sense to call the first function, and not the second.

 

 

 

I know the code works, the problem is I can't get the: "error, if max(a,b) uses call-by-value" how would it be called by value when we can clearly see it takes pointers and not values?!


Edited by noatom, 01 July 2014 - 04:59 PM.


#5 Brother Bob   Moderators   -  Reputation: 7923

Like
2Likes
Like

Posted 01 July 2014 - 05:09 PM


I know the code works, the problem is I can't get the: "error, if max(a,b) uses call-by-value" how would it be called by value when we can clearly see it takes pointers and not values?!

You are confused about what is actually passed to the function. In your max-function that is overloaded for char const *, the pointers a and b are not references to the pointers you pass to it, but value-copied of the original pointers. If you are confused about pointers and pass-by-value, it is easier to hide the pointer behind a typedef.

typedef char const *cstring;

cstring max (cstring a, cstring b)
{ ... }

Now compare with passing int, or float, or double, or char, and you'll see that the cstring object, which is a char const *, is actually passed by value.



#6 noatom   Members   -  Reputation: 782

Like
0Likes
Like

Posted 01 July 2014 - 05:18 PM

In your max-function that is overloaded for char const *, the pointers a and b are not references to the pointers you pass to it

I never said they are references, I was trying to say that I'm basically passing a pointer to a function which requires pointers, so why would there be values involved at all?!

Wasn't pointer assignment supposed to only copy the address?


Edited by noatom, 01 July 2014 - 05:23 PM.


#7 Brother Bob   Moderators   -  Reputation: 7923

Like
0Likes
Like

Posted 01 July 2014 - 05:32 PM

The pointers are passed by value to the function, and returned by value from the function. That's the problem here; the pointer that is returned from the max function is then returned from a function that returns a reference to the pointer. Since the return value is local to the function, the reference is thus a reference to a local value.

 

As I said, replace your char const * types with my cstring typedef and see where the values and references to local values comes into the picture. Pointers are not treated differently that any other int, float or double value.



#8 noatom   Members   -  Reputation: 782

Like
0Likes
Like

Posted 01 July 2014 - 05:40 PM

But how do you pass a pointer by value, since a pointer's value is an address?! If a and b are pointers, and b points to something, doing a = b, doesn't create another object, is just makes a point to the very same object that b points to.


Edited by noatom, 01 July 2014 - 05:44 PM.


#9 Brother Bob   Moderators   -  Reputation: 7923

Like
1Likes
Like

Posted 01 July 2014 - 05:49 PM

Step away from your nested max functions a bit and examine this program.

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

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

int main ()
{
    int ivalue = 0;
    char const *svalue = "foo";

    set(ivalue);
    set(svalue);

    std::cout << ivalue << " " << svalue << std::endl;
}

Now answer these two questions, and understand why the answer is what it is.

  1. Is the value of ivalue 0 or 42?
  2. Does the pointer svalue point to the string "foo" or "bar"?

This is what is the core of the problem with your max functions.


Edited by Brother Bob, 01 July 2014 - 05:50 PM.


#10 TheChubu   Crossbones+   -  Reputation: 3991

Like
0Likes
Like

Posted 01 July 2014 - 05:58 PM


But how do you pass a pointer by value, since a pointer's value is an address?
Copy the value of that address. The value of the pointer (an address as you said) is stored somewhere, when you call that function, its value gets copied and handed over to the function. The function uses that value to see where the pointer pointed to. Passing the pointer by its value.

"I AM ZE EMPRAH OPENGL 3.3 THE CORE, I DEMAND FROM THEE ZE SHADERZ AND MATRIXEZ"

 

My journals: dustArtemis ECS framework and Making a Terrain Generator


#11 noatom   Members   -  Reputation: 782

Like
0Likes
Like

Posted 01 July 2014 - 05:58 PM

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?


Edited by noatom, 01 July 2014 - 06:05 PM.


#12 Brother Bob   Moderators   -  Reputation: 7923

Like
1Likes
Like

Posted 01 July 2014 - 06:04 PM

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.


Edited by Brother Bob, 01 July 2014 - 06:07 PM.


#13 noatom   Members   -  Reputation: 782

Like
1Likes
Like

Posted 01 July 2014 - 06:20 PM

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


Edited by noatom, 01 July 2014 - 06:29 PM.





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS