Jump to content

  • Log In with Google      Sign In   
  • Create Account

Brother Bob

Member Since 26 Nov 2001
Online Last Active Today, 09:52 AM

Posts I've Made

In Topic: how to use or ||

09 July 2014 - 04:27 AM


It should be noted that performing your comparisons using multiple Boolean variables means that you cannot take advantage of short-circuit evaluation.


VS2012, compiled with default release mode settings:


Resulting disassembly:


Compilers are very, very smart.


They are, hopefully, also very smart not to do that when the evaluations of the temporary variables have side effects because that would change the meaning of the program. Your example works because the individual conditions does not have any observable side effects. If the expressions have side effects, or otherwise relies on short circuiting to prevent the evaluation of an expression in the condition (for example checking for null-pointer before accessing a member of an object), expanding the expressions to temporary variables before the if-statement won't work.

In Topic: How is this a pass by value?!

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.

In Topic: How is this a pass by value?!

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";


    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.

In Topic: How is this a pass by value?!

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.

In Topic: How is this a pass by value?!

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.