Вопрос

Again here i am because the course material for C++ was not taught very well at all. The question gives use several function definitions and calls to these functions and expects us to state the output and what exactly is going on. I have executed these functions and tried to come up with some rationale to what is happening but if someone could help me out that i would be very grateful.

function definitions (The names are all as given. I know the names are not very good):

void MyIncrementFirst(int * i) {
(*i)++;
}

void MyIncrementSecond(int i) {
i++;
}

void MyIncrementThird(int & i) {
i++;
}
void MyIncrementFourth(int ** i) {
*i = new int(0);
}

void MyIncrementFifth(int *& i) {
i = new int(69);
}

The calls to these functions have been defined as:

int * a = new int(42);
cout << "Result" << endl;

MyIncrementFirst(a);
cout << "first " << *a << endl;

MyIncrementSecond(*a);
cout << "second " <<*a << endl;

MyIncrementThird(*a);
cout << "third " <<*a << endl;

MyIncrementFourth(&a);
cout << "fourth " <<*a << endl;

MyIncrementFifth(a);
cout << "fifth " <<*a << endl;

Which produces the following:

first 43
second 43
third 44
fourth 0
fifth 69 

Some of this syntax i have not seen before (e.g. *&) so i maybe completely wrong here.

First:

This seems simple enough. I am passing in a pointer to an integer, and then de-referencing the pointer and incrementing its value, so instead of incrementing the pointer i am incrementing the actual value it is pointing to. No memory allocation is used, and this function uses pass-by-reference in order to modify the value.

Second:

This is similar to the above, as the pointer is de-referenced to get the integer value from the pointer rather than the memory address. Now this function uses pass-by-value so a copy of the variable is used in the functions execution. This means that incrementing a here, will have no impact on the variable you passed in, the value of a will remain as 42, but the copy of the passed in variable will have its value changed to 43 during the functions execution.

Third:

My understanding is that the '&' operator is basically saying 'the memory address of ...' so this function will take the memory address of the passed in pointer, locate the value it points to an increment this by one. That is why 43 is printed. This is using pass-by-reference.

Fourth:

If i remember correctly the '**' syntax means a pointer to a pointer. This means that when passing &a to the function you are passing the memory address of the pointer so when you de-reference this pointer and set its value to a new int(0); you are actually overwriting the exiting data at this address (42 in this case) so the value 0 is printed. This uses pass-by-reference.

fifth:

I have not seen this syntax before (*&). I have looked up this syntax and i think it is basically saying 'pass me by reference not value' so it seems to be the same as de-referencing the pointer as mentioned earlier. Again, because this is pass-by-refence not value when the value is set to a new int(69); the current data at that location is overwritten thus 69 is outputted.

Edit: Forgot to include my question! I want to know if my thinking is correct, this is all exam prep so i want to be sure i am doing it correctly.I just want to make sure my logic is correct and if i have done anything wrong or missed anything would somebody be able to steer my in the right direction

Это было полезно?

Решение

Edit: (based on some commenter feedback)

Everything you have is mostly correct, save some confusion on your output values. However, what you have in #4 and #5 is actually not overwritten data, it's actually new data at a new address, which is assigned to the variable i. This is part of what an earlier commenter said about a potential for a memory leak. Since you're not deleting the old address before assigning a new one, that's where it could occur.

The &* syntax in the last one is a reference to a pointer. It's essentially the same as int** except you don't pass &a as the argument, it's just a

What you have there for number 5 is basically correct, but the reasoning behind it is missing the meaning of &*

You might want to clarify if you've run each of these function calls sequentially or one at a time, since your output is incorrect if you'd indeed run them sequentially as written.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top