Can you return a class member object as a pointer?

Started by
18 comments, last by iMalc 11 years, 3 months ago
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.

[size="1"]And a Unix user said rm -rf *.* and all was null and void...|There's no place like 127.0.0.1|The Application "Programmer" has unexpectedly quit. An error of type A.M. has occurred.
[size="2"]

Advertisement
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.

Don't confuse language concepts and implementation details. The fact that it is a pointer "under the hood" is, as you say, an implementation detail, but it is not a pointer being passed by value as far as the language is concerned; it is passed by reference.

If you bring implementation details and "under the hood" into the picture, then nothing exists in any language, because it all boils down to machine code being executed in one way or another, and machine code typically has no templates, no structures, no functions, no scope, no name spaces, no closures, no typing... nothing. A language just abstract such things so that the programmer can write code what the program is supposed to do, and the compiler/virtual machine/whatever ensures that the program is actually doing just that on the particular computer running it.

Don't confuse language concepts and implementation details. The fact that it is a pointer "under the hood" is, as you say, an implementation detail, but it is not a pointer being passed by value as far as the language is concerned; it is passed by reference.

Actually, my bringing in of implementation details was to cement that references may in fact be pointers--for the sake of showing that my argument holds in actual architectural-level practice.

As far as your distinction, I think (in this case) it's a distinction without a difference. I will agree that references and pointers have a few semantic differences (for example, you can do arithmetic on pointers, and (so) references can do more checking), but really there's no difference between them. They act the same way, and you can use one instead of the other with almost equal facility.

For purposes of this discussion, the key point is that passing references into functions works in the same way as passing pointers. In the above example, the answer is "the reference int&example is passed by value, since its value is copied"--nevermind that that copying might be optimized away, just as inlining might similarly be able to optimize away other copies for pointers. Semantically thinking of the reference being copied doesn't break anything, and is actually closer to truth in practice.

If you bring implementation details and "under the hood" into the picture, then nothing exists in any language, because it all boils down to machine code being executed in one way or another, and machine code typically has no templates, no structures, no functions, no scope, no name spaces, no closures, no typing... nothing. A language just abstract such things so that the programmer can write code what the program is supposed to do, and the compiler/virtual machine/whatever ensures that the program is actually doing just that on the particular computer running it.

Reducto ad absurdum, but still somewhat relevant. Machine code has comparably few features, I'll agree, but in this case, the idea of "pointer"s actually exists, albeit not with static checks.

And I completely agree that whether "references" are implemented at the machine level through pointers or at the compiler level through optimizations, the point is that they exist in C++, which abstracts it. However, the converse of that was never my point; as above, I brought in implementation details to show how it works in practice. I had intended the first main paragraph of my last post to address that.

The point with all this is that the (key) properties of "references" exactly mirror those of pointers: they are datatypes held in variables, and they get copied by value when they are passed into functions, just as much as pointers. That's the functionality that C++ exposes and abstracts for us. C++ programmers might not think about it that way, since in the ideal case, they can be optimized just like any other code. But as we've agreed, that last is an implementation detail.

[size="1"]And a Unix user said rm -rf *.* and all was null and void...|There's no place like 127.0.0.1|The Application "Programmer" has unexpectedly quit. An error of type A.M. has occurred.
[size="2"]

I knew assembly language (Z80 and x86) before I learned C, and it was trivial to think of a natural assembly implementation of most things in C. When I was learning C++, I wouldn't consider that I understood a feature unless I had some idea of how that would be implemented in assembly. In particular, thinking of references as memory addresses with slightly different syntax than pointers was very helpful to me.

On a side note, I still don't think I truly understand exceptions, in that sense: I should probably spend a couple of hours figuring it out.

You can sort of do exceptions in C with setjmp/longjmp. That's how SSE is done in Windows.
"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

[quote name='Paradigm Shifter' timestamp='1357693013' post='5019295']You can sort of do exceptions in C with setjmp/longjmp. That's how SSE is done in Windows.[/quote]*SEH wink.png

[quote name='Álvaro' timestamp='1357692811' post='5019293']On a side note, I still don't think I truly understand exceptions, in that sense: I should probably spend a couple of hours figuring it out.[/quote]They key part of C++ exceptions (as opposed to SEH/setjmp/longjmp type ones) is that stack-unwinding takes place. I learnt a lot about this by writing a stack-allocator that supports unwinding (calling destructors of things in the stack in the opposite order to creation).

Yeah, I meant SEH, but I have had several cans of Lidl's finest cheapest ales ;)<br /><br />Stack unwinding is the big difference, yeah.
"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley
The point with all this is that the (key) properties of "references" exactly mirror those of pointers: they are datatypes held in variables, and they get copied by value when they are passed into functions, just as much as pointers. That's the functionality that C++ exposes and abstracts for us. C++ programmers might not think about it that way, since in the ideal case, they can be optimized just like any other code. But as we've agreed, that last is an implementation detail.


No, C++ programmers don't think about it that way because in terms of the semantics of C++ (not the implementation details), references are not values. We are discussing the semantics of C++ here. The implementation details are interesting and a C++ programmer will often find it useful to keep them in mind, but as far as I can tell, they are not relevant to the discussion of the semantics of C++, the programming language.
If you want to claim that implementation details are what counts, many C++ compilers don't have pass by value for objects. When you call a function that uses pass by value they create a new object in the caller's frame and pass by address to the function. In those compilers only primitives and pointers have pass by value. And then you have some compilers where some objects are pass by value and some others aren't, depending on size, whether or not they contain floating point variables or even more obscure criteria.
Even pointers are a high-level language concept. At the level of implementation detail, there are no such things as pointers.
There are typically registers with values in them, and instructions that operate on those registers, some of which fetch values from places in memory relating to the value a register held.

Regardless of whatever merrit your argument has on some level, it simply isn't helpful to make the point you are trying to make, because it contradicts the description of the language itself.
"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms

This topic is closed to new replies.

Advertisement