pointer

Pages: 1234
is there any diff b/t pointer variable and refrence variable if yes then i want to know the program by which we can diffrentiate it

is it related to hardware and memory?
In machine level, there is no difference.
In c++ level, it's mostly syntax. Plus, reference can't be null and can't be made to point to a different object.
as i know there is no such thing like pointer because Denis Ritchi named it as refrence variable.....but there are too many rumours pointer and refrence variable are two diffrent things....
I have had to say this so many times, and I expect to be saying it for decades to come.

A pointer is a complete object. It takes up memory. You can give it a value. You can change that value. You can carry out arithmetic operations on it. You can carry out the dereference operation on it.

A reference is another name for an object. The implementation is left up to the compiler writer, but there is no reason at all for it to take up memory. It's just another name for an object.

They are very different. A pointer is not a reference. A pointer is not a reference. A pointer is not a reference.
Last edited on
closed account (1vRz3TCk)
many rumours pointer and refrence variable are two diffrent things....

They are, different things that is.

A C++ reference type is an alias (an alternate name) for an object.
Then, I assume, there is a way to take
1
2
3
void foo(int& i) {
   i++;
}
and compile it to something that does not resemble a pointer? Apart from inlining. That would be silly.

Also, is a pointer really forced to take up space? Wouldn't
1
2
3
int i;
int* p = &i;
*p = 5;

be optimized into
int i = 5;
?

Reference is a hidden pointer. Is there some kind of reason to treat it in another way?
closed account (zb0S216C)
A reference is a variable but the rule is that it must constantly refer to the object given to it. Once it's been given an object, it cannot, by any means, refer to any other object. With the latter in mind, the reference can be viewed as an alias, but I find that misleading; typedef creates aliases.

Note that a reference, like a pointer, does in fact require memory but the amount of memory is allocates reflects the type it refers to.

A pointer, though, refers to the starting address of the variable it points to. Pointers can be used to allocate heap memory; references cannot. The size of a pointer is usually 4 bytes, but maybe larger or smaller - depending on the implementation.

Wazzak
Last edited on
... but the amount of memory is allocates reflects the type it refers to.
Why would you say that?
Also, http://ideone.com/79GUa
Last edited on
Note that a reference, like a pointer, does in fact require memory but the amount of memory is allocates reflects the type it refers to.


I disagree that is has to, but I agree that it's very common to. The original object takes up memory. The reference does not have to - it is deliberately unspecified and left to the compiler implementor to come up with a way to create code that does what the standard says it should. I do agree, though, that it is very very common to do it that way.

Reference is a hidden pointer. Is there some kind of reason to treat it in another way?

Because pointers can do all sorts of extra things that a reference cannot. Treating them the same way will be fine until you try to {dereference/carry out arithmetic on/change the object it refers to} on a reference.
Last edited on
closed account (zb0S216C)
Create a class with an int and a int& and see for yourself. sizeof() should yield 8. Why would it not allocate memory? It's a variable, isn't it?

Wazzak
http://www.cplusplus.com/forum/beginner/3958/ there are probably several more threads like that.. This one is a bit silly, but it contains disassembly, so that makes it okay.

dear gentlemen.....
as i know
pointer is a variable that can hold the address of a memory location..

class A{};
in any oops language

A ob=new A();

here ob will hold the address of memory location created by new then how
pointer and refrence are two diffrent things
how pointer and refrence are two diffrent things


If two things are the same, then any place you can put one, you can put the other.

There are places you can use a reference that a pointer will not go, and vice-versa; since they cannot be used interchangably, they cannot be the same thing. There are ways to take the important information from one and craft the other, using that important information, much in the same way one could take an int and use it to make a float; this does not make a float and an int the same thing. I could even create some seriously mental compiler that actually used floats to store all int values, and a whole bunch of tricky internal calculations to make sure that the behaviour of these pseudo-int values met the requirements of the standards. That would still not mean that a float is the same thing as an int.

This is true of many other things that are not the same. For example, a cow and a dog are not the same thing. Blue and yellow are not the same thing. If two things cannot be interchanged identically, they are not the same.
Last edited on
@Moschops,
Firstly, you should be comparing T& to T*const rather than just T*.
Secondly, we get different results when comparing them in C++ level and in machine level. My first post seems to state that clearly..
Lastly, I'd love to hear a situation where a reference can be used, but a pointer cannot.

@OP, if we accept that reference is a pointer, it must be added that this pointer is hidden from you. You have no control of the address it holds (after initialization). I suppose it's hidden enough that you may not consider it a pointer at all. After all, the mechanics are not that important. Recognise what a reference is by what you can do with it and call it as you like.
Lastly, I'd love to hear a situation where a reference can be used, but a pointer cannot.


Given a function that returns a reference to an int,

someFunction() = 7;, for example.

I expect now you'll say that you could change the function so that it returns a pointer, and then take that pointer, and then dereference it and set the value it points to. This is true, but it doesn't mean a pointer is the same thing as a reference. By that logic, an int is identical to a float, as I could take an int and make a float store the same value.
Last edited on
int/float thing is entirely different. ints and floats have very different properties, store things in different ways and using them produces different asm code.

The analogy you're looking for is function/method. The difference is mostly syntactic. A method is a special type of function designed for a special purpose. That does not make it anything other than a function.

Though the pointer/reference difference is even more shallow, as writing C++ code in C is quite a bit of pain.. Your example only needs to be changes syntactically, not semantically, to make it work with pointers.
As an aside, I do not give credence to arguments based on asm here. asm is not C++, and saying that different aspects of C++ come out to similar/identical asm is analagous to reducing code to formal logic to prove it's identical - it may well have identical effect, but that doesn't make it the same thing.

I can understand giving credence to argument based on comparison of asm; I just don't.
Last edited on
You're persistent. Surely you understand that there is syntax and there is semantics. I say that asm is the easiest way to analyse semantics. I really hope we're talking about semantics here..

Let me paraphrase it the last time: functionality of a reference is a subset of functionality of a pointer.
Functionality is another word for semantics. If we say that references are a subset of pointers (semantically), we can say that all references are pointers (again semantically). Syntactic equivalence was never an objective.

Hopefully this is more clear. I really can't put it any more clearly. And there isn't any point to. It's not like you don't know what references are. We're both just being persistent.
Let me paraphrase it the last time: functionality of a reference is a subset of functionality of a pointer.


I'm fine with that. My point is that a reference is not the same thing as a pointer. If the functionality of a reference is not the same as that of a pointer, it seems to me that they are different things.

we can say that all references are pointers (again semantically)


There are things I can do with a pointer that I cannot do with a reference. Accordingly, I see them as different things.
Last edited on
closed account (1vRz3TCk)
Semantically, a reference is just an alternative name for an object. If you want to think of it as a constant pointer that automatically is dereferenced when it is used, then it probably isn't too harmful...but as a reference isn't an object that can be manipulated in the same way as a pointer and that a compiler doesn't have to provide storage for a reference if it can optimised away (i.e. no object representing the reference at run-time), then I don't consider a reference to be a pointer.
Pages: 1234