Pointers vs References

Pages: 12
IMO references are trashy things! :<
have they any really useful advantage that can cover their lack of intuitiveness?
@jsmith,

I am not sure what standard compiler allows you to reseat a reference, in any C++ reference you would find that a reference can NOT be reseated, once initialized. Most of the compilers will complain if a reference is attempted to be reseated (meaning, reset or reinitialized).

And your example shows as it can be reseated. Though I agree that many compilers vary from their implementations and standards (or non-standard), but I dont agree that a reference is same as a pointer and can be reseated.

And I would strongly recommend my programmers to avoid such reseating assignments for their use of references in their code, as it would not be portable or be problematic in future.

It is upto you, if you dont agree. We better stop it here for this topic.

Have a great day guys :)






Last edited on
@kinder

I would not say they so trashy, but they are very helpful in C++ when it comes to copy constructors and overloading operators. In such case, they work better than pointers ;)


Good luck :)
thanks; maybe!
can you probably put a simple sample code here?
indeed I got very confused by this strange thing when I came to learn c++.
I am familiar with C very good.
their syntax somehow contradicts with each other. and in C++ we have both together.
this increases obscurity capacity of code. particularly if we use both in one program.
pointers are essential and very intuitive, while references arent.
Last edited on
It would be more easier if you could post the code you have problem with and questions, myself or some one here could help you to clear it out.

Good luck :)

[EDIT - removed wrong stuff above.]

I refer anyone who claims references should be avoided to C++ Coding Standards, Sutter and Alexandrescu, (ISBN 0-321-11358-6) pg. 46 section "Take parameters appropriately by value, (smart) pointer, or reference."
Last edited on
OK, after our this long discussion, I am turned curious if there are any changes in the recent standards/compilers etc.
Just before I did a quick search on google search on "reference" and "reseating". The following showed up as result.

All of them show a reference can not be reseated. Though there are other links for forums on disucssing the same, I read them that all suggesting they can not be 'reseated' once initalized.

Now I am not sure what kind/standard of compiler are you using, and say, your compiler is wrong/obsolete or others (including myself) wrong. Check it out.


Check the link http://www.stat.harvard.edu/ccr2005/C++/refs-ptrs.pdf
and in the Reference V (Page 5) it states:

1
2
3
4
5
6
7
8
9
10
• difference with C: in C++, pass by reference has a distinct meaning
• although there are big debates on this issue, here is some general advice on
when to use what while calling/writing a function:
• when to use references?
– use references when you can, and pointers when you have to
– references are usually preferred over pointers whenever you don’t need
re-initialization or ”reseating”
• when to use pointers?
– use a pointer when a function’s return value needs to be NULL to indicate
“failure” because you cannot return a NULL reference



"Once a reference is created, it cannot be later made to reference another object; we say it cannot be reseated. This is often done with pointers. " noted under "Relation to pointers" at
http://en.wikipedia.org/wiki/Reference_(C%2B%2B)

Check the point "reference type cannot be reseated in C++" at
http://meditation-art.blogspot.com/2007/04/reference-type-cannot-be-reseated-in-c.html

Check the point [8.5] named "How can you reseat a reference to make it refer to a different object?" at http://yosefk.com/c++fqa/ref.html


And I never said, it should not be avoided. They can be avoided when they are not for the purpose. They should be used carefully.

Check it out. Good luck :)
Last edited on
Further to above note, there is more to read for clear difference between the reference and pointers, at the same link
http://www.stat.harvard.edu/ccr2005/C++/refs-ptrs.pdf

This link is useful for anyone what to read the differences.

Under "Reference III" (page 3) you will see:

1
2
3
4
5
6
7
8
9
10
11
12
• what won’t work:
/*
* The following two lines won’t work because reference has to
* be initialized at the time of declaration
*/
int &debug_level_ref;
debug_level_ref = level;
• so always initialize references
• because references always need to be initialized there could be no NULL
references
• references cannot be re-initialized, i.e. throughout its lifetime it will serve as
an alias to the object it was first initialized with


Not to forget this documents is from Harvard University.

Good luck :)
I stand corrected on reseating references.

I stand pat on usage of references being preferred to pointers.
@jsmith

Good discussion though.

At least it gave me an opportunity to refresh my memory checking if forgot some thing ;)

Good luck :)


I'll just leave this here.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <iostream>
#include <cstring>

void pmemloc(void *a,long size=1){
	if (!size)
		return;
	unsigned char *casted=(unsigned char *)a;
	for (long mod=1;size>0;size--,mod++){
		printf("%02x ",*(casted++));
		if (!(mod%16))
			printf("\n");
		else if (!(mod%8))
			printf(" ");
	}
	for (long mod=1;size<0;size++,mod++){
		printf("%02x ",*(casted--));
		if (!(mod%16))
			printf("\n");
		else if (!(mod%8))
			printf(" ");
	}
	printf("\n");
}

void printstack(void *a,bool stackGrowsDownwards,long sizeOfStackEntry){
	for (long i=0;i<6;i++){
		char *f=((char *)a)+(stackGrowsDownwards?-i:i)*(sizeOfStackEntry+4);
		pmemloc(f,sizeof(char *));
	}
}

int main(){
	int startofstack=0xCDDCCDDC;
	int z=12;
	int a=42;
	int &b=a;
	int *c=&a;
	int *d=&z;
	int endofstack=0xCDDCCDDC;
	bool stackGrowsDownwards=(void *)&c<(void *)&a;
	long sizeOfStackEntry=(((char *)&a)-((char *)&z))*(stackGrowsDownwards?-1:1)-sizeof(int);
	//pmemloc(&endofstack,1000);
	std::cout <<stackGrowsDownwards<<" ("<<&c<<";"<<&a<<")"<<std::endl<<sizeOfStackEntry<<std::endl;
	printstack(&z,stackGrowsDownwards,sizeOfStackEntry);
	std::cout <<"reference 'b' before: "<<b<<std::endl;
	if (stackGrowsDownwards)
		memcpy(((char *)&a)-(sizeOfStackEntry+sizeof(int)),&d,sizeof(int));
	else
		memcpy(((char *)&a)+sizeOfStackEntry+sizeof(int),&d,sizeof(int));
	std::cout <<std::endl;
	printstack(&z,stackGrowsDownwards,sizeOfStackEntry);
	std::cout <<"reference 'b' after: "<<b<<std::endl;
	return 0;
}

Still working on something that works with MinGW. It'd be easier if the damn debugger stopped at the breakpoints. Grrr.
Last edited on
i think references are just precompile notations.
that is somewhat clear from their common traits with such language parts (such as templates).
c++ has several of such features that are famous and are parts of what makes C++ so different from C (apart from classes).
references are indeed pointers/translated to them in the compilation process.
no other difference exist in the generated code; not any runtime control.
am I correct?
I am heard that C++ was implemented as a preprocessor for the C compiler sometime.

oh, I have another question!
can you show how we can make a pointer parameter that act exactly (or to any degree possible) like references?
should it be a const pointer?
oh, and just out of curiosity, can we write a macro that write that &s automatically before arguments that we pass to a function ?!!
if yes, we probably can add references to C too!!
Last edited on
The nature of references is implementation-dependent. If you compile and run the hack (one that I'm not very proud of, by the way) I posted above you'll see that VC++ compiles references as pointers.

Oh, yes. Underpowered pointers: just what C needs. While we're at it, let's forcefully change everything to upper case. I'm sure nobody would mind.
Topic archived. No new replies allowed.
Pages: 12