-1

I'm just beginning to learn C++ through my university course.

I'm revising for a C++ exam and I was hoping I could just ask some simple questions about things I'm not so sure on.

First of all, if I have this:

int i = 4;

What is the difference between these two lines:

int* p = &i;
int& r = i;

By my understanding, at this point both p and r are like conduits to i; changing their value will change the value of i...


Secondly, about assembly, I have a question in a past exam that is fooling me:

mov eax, dword ptr[b]
push eax
mov eax, dword ptr[a]
push eax
call printCode (411186h)
add esp,8 

Q: Which of the following prototypes when dissassembled, best match the assembly code?

A: int printCode(int a, int b)

My question is: If the assembly is moving ptr[a] and ptr[b]... Why is the prototype not int* a, int* b?


Thank you.

Community
  • 1
  • 1
Xenoprimate
  • 7,691
  • 15
  • 58
  • 95

5 Answers5

2

The difference is that changing p's value won't change i, it will change what p points to:

p = &j;

With p, you can access, and change, i via indirection:

*p = 5;

Whereas with r, the indirection is not required, since r is an alias for i. On the flip side, this means that there is no way to change r to reference a different variable. i.e., there is no equivalent to p = &j;.


You should split the second question into a second question (if you get my drift).

Marcelo Cantos
  • 181,030
  • 38
  • 327
  • 365
1
  • int* p = &i;

    p is a pointer pointing to the address of i. p will be a variable stored in your memory that contains the (virtual) memory address of i.

  • int& r = i;

    r is a reference to i. You should think of references as aliases: you could think that r is an alias for the name i

    I'd suggest you to read C++Faq chapter about references.

peoro
  • 25,562
  • 20
  • 98
  • 150
1
int* p = &i;
int& r = i;

The first statement means assigning the address of variable i to a pointer p. Here later you can change the value of the pointer p.

The second statement is "create a reference or alias r for the variable i". It is just another name for the same variable. You cannot change the reference later to point to a different variable in c++.

For the assembly snippet. I sense the snippet points to calling code. the call might have been like

printCode(*a,*b); // Here a and b are pointer to integers 

So the prototype is

int printCode(int a,int b) 

is right.

Ferose Khan J

ferosekhanj
  • 1,086
  • 6
  • 11
0

References are more or less only syntactic sugars, but there are few differences between them and pointers which are described very well here.


For disassembly question.
When you say:

mov eax, dword ptr[b]

You are asking CPU to move the value from a specified address (b). So it depends what is b. If b is a int* then the answer is correct.

Community
  • 1
  • 1
Klark
  • 8,162
  • 3
  • 37
  • 61
0

What is the difference between these two lines:

int* p = &i;
int& r = i;

By my understanding, at this point both p and r are like conduits to i; changing their value will change the value of i...

The sentence is not true if taken literally. Changing p value will only make it point to a different int, changing the memory pointed to will change i:

int j = 0;
*p = 5; // equivalent to r = 5, or i = 5 (semantically, can be different in assembler)
p = &j; // changes p to point to j instead of i

You should think of pointers as proxy objects that refer you to a real instance, but that can be modified to lead to a different place. References are best understood as aliases to the object on the right hand side of the definition. You never operate on the reference, you operate on the referred object, they are the same.

David Rodríguez - dribeas
  • 204,818
  • 23
  • 294
  • 489