Click here to Skip to main content
15,892,005 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: compiler error Pin
ali kanju9-Jun-09 20:16
ali kanju9-Jun-09 20:16 
Questionfree and delete[] Pin
hawk23reddy9-Jun-09 17:28
hawk23reddy9-Jun-09 17:28 
AnswerRe: free and delete[] Pin
«_Superman_»9-Jun-09 17:55
professional«_Superman_»9-Jun-09 17:55 
GeneralRe: free and delete[] Pin
N a v a n e e t h9-Jun-09 18:02
N a v a n e e t h9-Jun-09 18:02 
GeneralRe: free and delete[] Pin
«_Superman_»9-Jun-09 18:05
professional«_Superman_»9-Jun-09 18:05 
GeneralRe: free and delete[] Pin
Stuart Dootson9-Jun-09 22:37
professionalStuart Dootson9-Jun-09 22:37 
GeneralRe: free and delete[] Pin
N a v a n e e t h10-Jun-09 0:01
N a v a n e e t h10-Jun-09 0:01 
GeneralRe: free and delete[] Pin
Stuart Dootson10-Jun-09 0:13
professionalStuart Dootson10-Jun-09 0:13 
N a v a n e e t h wrote:
Are you saying it is implemented as pointers behind the scenes


Yep - consider this C++ code - two equivalent functions, one using a pointer, the other a reference.

void Add1(int* pInt, int x)
{
   *pInt += x;
}

void Add2(int& rInt, int x)
{
   rInt += x;
}


I compiled this with cl -FAcs -c -O2 and got this assembly language (irrelevant bits snipped):

PUBLIC	?Add1@@YAXPAHH@Z				; Add1
_TEXT	SEGMENT
_pInt$ = 8						; size = 4
_x$ = 12						; size = 4
?Add1@@YAXPAHH@Z PROC					; Add1, COMDAT

; 3    :    *pInt += x;

  00000	8b 44 24 04	 mov	 eax, DWORD PTR _pInt$[esp-4]
  00004	8b 4c 24 08	 mov	 ecx, DWORD PTR _x$[esp-4]
  00008	01 08		 add	 DWORD PTR [eax], ecx

; 4    : }

  0000a	c3		 ret	 0
?Add1@@YAXPAHH@Z ENDP					; Add1
_TEXT	ENDS
PUBLIC	?Add2@@YAXAAHH@Z				; Add2
_TEXT	SEGMENT
_rInt$ = 8						; size = 4
_x$ = 12						; size = 4
?Add2@@YAXAAHH@Z PROC					; Add2, COMDAT

; 8    :    rInt += x;

  00000	8b 44 24 04	 mov	 eax, DWORD PTR _rInt$[esp-4]
  00004	8b 4c 24 08	 mov	 ecx, DWORD PTR _x$[esp-4]
  00008	01 08		 add	 DWORD PTR [eax], ecx

; 9    : }

  0000a	c3		 ret	 0
?Add2@@YAXAAHH@Z ENDP					; Add2
_TEXT	ENDS
END


Exactly the same code for each.

The Wikipedia page on C++ references[^] says it quite nicely - ...the typical implementation...effectively compiles references into pointers which are implicitly dereferenced at each use. Basically, references are (from most C++ programmers PoV) pretty much just syntactic sugar[^] for pointers.

PS - here's the gcc assembly language for that C++ code - again, the two functions are implemented exactly the same.

.globl __Z4Add1Pii
	.def	__Z4Add1Pii;	.scl	2;	.type	32;	.endef
__Z4Add1Pii:
LFB2:
	pushl	%ebp
LCFI0:
	movl	%esp, %ebp
LCFI1:
	movl	8(%ebp), %edx
	movl	12(%ebp), %eax
	addl	%eax, (%edx)
	popl	%ebp
	ret
LFE2:
	.align 2
	.p2align 4,,15
.globl __Z4Add2Rii
	.def	__Z4Add2Rii;	.scl	2;	.type	32;	.endef
__Z4Add2Rii:
LFB3:
	pushl	%ebp
LCFI2:
	movl	%esp, %ebp
LCFI3:
	movl	8(%ebp), %edx
	movl	12(%ebp), %eax
	addl	%eax, (%edx)
	popl	%ebp
	ret
LFE3:


Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

GeneralRe: free and delete[] Pin
N a v a n e e t h10-Jun-09 0:21
N a v a n e e t h10-Jun-09 0:21 
GeneralRe: free and delete[] Pin
Randor 11-Jun-09 14:12
professional Randor 11-Jun-09 14:12 
AnswerRe: free and delete[] Pin
N a v a n e e t h9-Jun-09 17:58
N a v a n e e t h9-Jun-09 17:58 
GeneralRe: free and delete[] Pin
chirag_chauhan9-Jun-09 19:33
chirag_chauhan9-Jun-09 19:33 
QuestionDecoding(decompressing) an image Pin
Bhavani_mnnit9-Jun-09 15:49
Bhavani_mnnit9-Jun-09 15:49 
AnswerRe: Decoding(decompressing) an image Pin
«_Superman_»9-Jun-09 16:08
professional«_Superman_»9-Jun-09 16:08 
GeneralRe: Decoding(decompressing) an image Pin
Bhavani_mnnit9-Jun-09 20:39
Bhavani_mnnit9-Jun-09 20:39 
QuestionSTRRET Structure Question Pin
nm_1149-Jun-09 14:02
nm_1149-Jun-09 14:02 
GeneralRe: STRRET Structure Question Pin
Michael Dunn9-Jun-09 16:09
sitebuilderMichael Dunn9-Jun-09 16:09 
AnswerRe: STRRET Structure Question Pin
«_Superman_»9-Jun-09 16:16
professional«_Superman_»9-Jun-09 16:16 
GeneralRe: STRRET Structure Question Pin
nm_1149-Jun-09 17:22
nm_1149-Jun-09 17:22 
GeneralRe: STRRET Structure Question Pin
«_Superman_»9-Jun-09 17:24
professional«_Superman_»9-Jun-09 17:24 
GeneralRe: STRRET Structure Question Pin
nm_1149-Jun-09 17:37
nm_1149-Jun-09 17:37 
QuestionDeployment Error. Pin
FISH7869-Jun-09 13:51
FISH7869-Jun-09 13:51 
AnswerRe: Deployment Error. Pin
chirag_chauhan9-Jun-09 19:35
chirag_chauhan9-Jun-09 19:35 
AnswerRe: Deployment Error. Pin
Cedric Moonen9-Jun-09 20:04
Cedric Moonen9-Jun-09 20:04 
GeneralRe: Deployment Error. Pin
FISH78610-Jun-09 1:39
FISH78610-Jun-09 1:39 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.