Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


Don't forget to read Tuesday's email newsletter for your chance to win a free copy of Construct 2!


#ActualGeometrian

Posted 08 January 2013 - 05:40 PM

I suppose he would've been correct if he said, "In C, everything is passed by value...."
While this is what I meant to say, I'm still not technically wrong.
 
Please identify the value is being passed in the following code:
void frobnicate(int &example) {
    example = 42;
}


When the compiler creates a function like this (and assuming it doesn't inline it), it needs to be able to change the value of that variable within the function. The C++ specification deliberately says that the actual way this happens is implementation defined, and that whether a reference actually uses storage or not is unspecified.

In some implementations, this is just a pointer, which means that yes, the value of the pointer is passed onto the stack. In this case, the "reference" semantics C++ gives you are nothing more than syntactic sugar with extra compile-time checking. Under the hood, it's nothing more than your standard raw pointer.

Sometimes you can't get around doing that unless you want to write better algorithms. Some compilers don't bother. Mine doesn't.

#2Geometrian

Posted 08 January 2013 - 05:31 PM

I suppose he would've been correct if he said, "In C, everything is passed by value...."

While this is what I meant to say, I'm still not technically wrong.

 

Please identify the value is being passed in the following code:

void frobnicate(int &example) {
    example = 42;
}
 

When the compiler creates a function like this (and assuming it doesn't inline it), it needs to be able to change the value of that variable within the function. The C++ specification deliberately says that the actual way this happens is implementation defined, and that whether a reference actually uses storage or not is unspecified.

In some implementations, this is just a pointer, which means that yes, the value of the pointer is passed onto the stack. In this case, the "reference" semantics C++ gives you are nothing more than syntactic sugar with extra compile-time checking. Under the hood, it's nothing more than your standard raw pointer.

Sometimes you can't get around doing that unless you want to write better algorithms. Some compilers don't bother. Mine doesn't.


#1Geometrian

Posted 08 January 2013 - 05:30 PM

I suppose he would've been correct if he said, "In C, everything is passed by value...."
While this is what I meant to say, I'm still not technically wrong.
Please identify the value is being passed in the following code:
void frobnicate(int &example) {
    example = 42;
}


When the compiler creates a function like this (and assuming it doesn't inline it), it needs to be able to change the value of that variable within the function. The C++ specification deliberately says that the actual way this happens is implementation defined, and that whether a reference actually uses storage or not is unspecified.

In some implementations, this is just a pointer, which means that yes, the value of the pointer is passed onto the stack. In this case, the "reference" semantics C++ gives you are nothing more than syntactic sugar with extra compile-time checking. Under the hood, it's nothing more than your standard raw pointer.

Sometimes you can't get around doing that unless you want to write better algorithms. Some compilers don't bother. Mine doesn't.

PARTNERS