The address of a literal

Pages: 12
I don't know any assembly language or another low-level programming language, so - simply put - a compiler can simpler create a variable away a literal when we deal with a reference than when we deal with a pointer.

Is that right ?
well... you shouldn't really think of it in terms of what the compiler would do. Pointers and References are two different concepts. And while they are somewhat similar, you shouldn't think of a reference as a "kind of" pointer, because they're not really (even if they're implemented that way).

These are high level concepts, not low level implementation.

Pointers "point to" the other variable.
References are the other variable.
Last edited on
Yes, a reference can be optimized away, whereas it is much more difficult with a pointer. But where it cannot, the underlying data is a pointer.
Well my point is they're still two different things and should be treated as such.
Duoas wrote:
But where it cannot, the underlying data is a pointer.


Sorry - I don't get it ^.^

Underlying data of what ? Of a reference ? If so, then I have to ask: how is that possible ?

I understand concept from my previous post in this way:

Reference to a literal -----> reference to a temporary variable created away a literal

And you said that if a compiler cannot does something like this above, the underlying data is a pointer. And I understand it in this way:

Reference to a literal -----> pointer to a temporary variable created away a literal

But - again - this construction:

const int* ptr = &10;

it's incorrect!..............
Last edited on
const /*numerical type*/ &a=/*numerical literal*/;
has the same effect as just declaring a symbolic constant. If a is parameter and the literal is an argument, then indeed a temporary variable is pushed onto the stack.
Computer languages are an abstraction over underlying data.

What is the difference between a float and an int? To the computer, it is just a collection of 32 bits (typically).

The difference is how they are treated.


How would you implement a reference? A reference is a name that dynamically refers to another object. Hey, we've already got something similar: a pointer. A pointer references another object.

Remember, there is the high-level, abstract way of looking at things.
And there is the low-level, machine way of looking at things.

You are confusing the two.


A constant literal, like "10", is not guaranteed to be stored anywhere useful to you other than where it is used [edit] and in the context where it is used [/edit]. As I already indicated:
The problem with literals is that they don't necessarily exist. The compiler is not only free to, but may need to, put them in the code in such a way that they cannot be accessed directly [as an object by itself].
For example, a MIPS processor packs both the instruction code and the values in the same machine word. You cannot reference a bitpacked field transparently (not without some significant overhead).
All this is low-level thinking.


From the high-level perspective, just remember that unless your data is in a const or a variable, it doesn't exist as a first-class object (one you can do stuff to). Hence, something like
const int* ptr =  &10 ;
is nonsense, because you are trying to get the address of something that is not guaranteed to exist: the literal value 10.

Hope this helps.
Last edited on
Duoas wrote:
From the high-level perspective, just remember that unless your data is in a const or a variable, it doesn't exist as a first-class object (one you can do stuff to). Hence, something like
const int* ptr = &10;
is nonsense, because you are trying to get the address of something that is not guaranteed to exist: the literal value 10.


OK, I get it :) I'll just remember that what I said before:

Quentin wrote:
so - simply put - a compiler can simpler create a variable away a literal when we deal with a reference than when we deal with a pointer.


(And you agreed with me):

Duoas wrote:
Yes, a reference can be optimized away, whereas it is much more difficult with a pointer.


-------------------------------------------------------------------------------------------------------
Thanks all for your help ;)
Topic archived. No new replies allowed.
Pages: 12