Pointer and reference similar at assembly level

Discussion in 'C' started by kaustubh, Aug 15, 2007.

  1. kaustubh

    kaustubh New Member

    Joined:
    Aug 13, 2007
    Messages:
    25
    Likes Received:
    0
    Trophy Points:
    0
    Pointer and reference maybe same at assembly level.
    Try an experiment :

    Code:
    # include<stdio.h>
    int main()
    {
      int i =90;
      int *pointer;
      int &reference = i; // Line 6
      pointer = &i;  
      return 0;  // Line 8
    }
    compile it to assembly with Microsoft c++ like this :
    Code:
    c:\ cl /Fa filename.cpp
    or with gcc like
    Code:
    c:\ gcc -S -masm=intel filename.cpp
    Open the asm file like c:\ notepad filename.asm

    Now you can the code of i, pointer, and reference;

    Code:
     Line 6
    	mov	DWORD PTR _i$[ebp], 90			; int i = 90;
    ; Line 8
    	lea	eax, DWORD PTR _i$[ebp]                  ; int &reference = i;
    	mov	DWORD PTR _reference$[ebp], eax
    ; Line 9
    	lea	ecx, DWORD PTR _i$[ebp]               ; int *pointer
    	mov	DWORD PTR _pointer$[ebp], ecx    ;  pointer = &i;
    
    you see the code for pointer is exactly same as reference .
     
  2. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    http://www.daweidesigns.com
    That's because C doesn't have references like C++. The & is strictly an address-of operator.
     
  3. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    He is using a cpp file and so the compiler is C++.
     
  4. kaustubh

    kaustubh New Member

    Joined:
    Aug 13, 2007
    Messages:
    25
    Likes Received:
    0
    Trophy Points:
    0
    Dawei i am using c++ compiler if you don't believe me see this code

    Code:
    #include<iostream>
    
    int main()
    {
    int i =90;
      int *pointer;
      int &reference = i; 
      pointer = &i;  
      return 0;  
    }
    
    
    Code:
    	
           mov     DWORD PTR _i$[ebp], 90			;  int i = 90
    
    	lea	eax, DWORD PTR _i$[ebp]                  ; int &reference = i; 
    	mov	DWORD PTR _reference$[ebp], eax
    
    	lea	ecx, DWORD PTR _i$[ebp]                  ; pointer = &i;
    	mov	DWORD PTR _pointer$[ebp], ecx
    	
    
    the code of reference is same as code of pointer.
     
  5. rajkumar_singhalmca

    rajkumar_singhalmca New Member

    Joined:
    Aug 22, 2007
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    student
    Location:
    bikaner(raj)
    the pointer is very ueful when the use in the right way otherwise it can be damaged the our memory location.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice