Help me understand this Strange C ++ code

This was a question in our old C ++ exam. This code is driving me crazy, can someone explain what it is doing, and - especially - why?

int arr[3]={10,20,30}; int *arrp = new int; (*(arr+1)+=3)+=5; (arrp=&arr[0])++; std::cout<<*arrp; 
+6
c ++
source share
6 answers

This operator writes an *(arr+1) object twice without an intermediate point in the sequence, therefore it has undefined behavior.

 (*(arr+1)+=3)+=5; 

This statement writes an arrp object arrp without an intermediate point in the sequence, so it has undefined behavior.

 (arrp=&arr[0])++; 

The code may lead to something.

Ref: ISO / IEC 14882: 2003 5 [expr] / 4: "Between the previous and next point in a sequence, a scalar object must have a value that its stored value has changed no more than once by evaluating the expression."

+13
source share
 (*(arr+1)+=3)+=5; 

arr + 1 - element with index 1
* (arr + 1) - the value of this element
(arr + 1) + = 3 - increase by 3
((arr + 1) + = 3) + = 5 - increase by 5;

so arr [1] == 28

 (arrp=&arr[0])++; 

arr [0] - element value 0
& arr [0] - address of element 0
arrp = & arr [0] - set arrp to point to elem 0
(arrp = & arr [0]) ++ - set arr to point to elem 1

result: 28

+9
source share

This line:

 (*(arr+1)+=3)+=5; 

gives the same result as this one (see footnote):

 arr[1] += 3; arr[1] += 5; 

This line:

 (arrp=&arr[0])++; 

gives the same result as this one (see footnote):

 int* arrp = arr+1; 

So this line:

 std::cout<<*arrp 

prints 28 .

But this code is losing memory because int *arrp = new int; allocates a new int on the heap that will be lost when assigned to (arrp=&arr[0])++;

Footnote: Of course, I suppose the lack of strangeness.

Edit: Apparently, some of the lines actually lead to undefined behavior due to C ++ Standard 5/4. So this is really a crappy exam question.

+6
source share
 int arr[3]={10,20,30}; // obvious? int *arrp = new int; // allocated memory for an int (*(arr+1)+=3)+=5; // (1) (arrp=&arr[0])++; // (2) std::cout<<*arrp; // (3) 

(one)

*(arr+1) same as arr[1] , which means that *(arr+1)+=3 will increase arr[1] by 3, so arr[1] == 23 now.

(*(arr+1)+=3)+=5 means that arr[1] incremented by another 5, so now it will be 28 .

(2)

arrp will pont at the address of the first element arr ( arr[0] ). Then the arrp pointer will be incremented, so it will point to the second element after the execution of the entire statement.

(3)

Prints what arrp points arrp : the second element is arr , which means 28 .

+1
source share

Well, remember that arrays can be interpreted as pointers

 int arr[3]={10,20,30}; int *arrp = new int; 

creates an arr array of three integers and an int pointer , which is assigned a fresh highlighted value.

Because assignment operators return a reference to the value that was assigned to enable multitasking,

 (*(arr+1)+=3)+=5; 

equivalently

 *(arr+1)+=3; *(arr+1)+=5; 

*(arr + 1) refers to the first element of the arr array, so arr[1] effectively increased by eight.

(arrp=&arr[0])++; assigns the address of the first element of the arrp array and then increments this pointer, which now points to the second element ( arr[1] again).

By dereferencing it in std::cout<<*arrp , you output arr[1] , which now contains the value 20 + 3 + 5 = 28 .

Thus, the code prints 28 (and also creates a memory leak, since the new int originally assigned to arrp never gets delete d)

0
source share

I will try to answer you by rewriting the code in a simpler way.

 int arr[3]={10,20,30}; int *arrp = new int; (*(arr+1)+=3)+=5; (arrp=&arr[0])++; std::cout<<*arrp; 

=== equals ===

 int arr[3]={10,20,30};//create array of 3 elements and assign them int *arrp = new int;//create an integer pointer and allocate an int to it(useless) //(*(arr+1)+=3)+=5; arr[1] = arr[1] + 3;//arr[1] == arr+1 because it is incrementing the arr[0] pointer arr[1] = arr[1] + 5; //(arrp=&arr[0])++; arrp = &arr[0];//point the integer pointer to the first element in arr[] arrp++;//increment the array pointer, so this really is now pointing to arr[1] std::cout<<*arrp;//just print out the value, which is arr[1] 

I assume you understand pointers and basic c.

0
source share

All Articles