Pointer increment operator errors

Okay, so that really bothers me. I am working on an HW issue and discovered what was really strange to me. here is the function and call in question

int find_oldest_frame(int **a, int size) { int min = clock(); int **ptr; int *ptr2; int frame = 0; int i; // get address of pointer so we can modify it ptr = a; // store off original pointer location. ptr2 = *a; for (i=0; i<size; i++) { // Who is the oldest time if (**ptr < min) { min = **ptr; frame = i; } printf("Current_Pointer %d\n", *ptr); *ptr++; // For some reason ++ doesn't work. } // now store the oldest frame with the current system time, so it no longer the oldest. *ptr = ptr2; *ptr += frame; **ptr = clock(); *ptr = ptr2; // Return the array index so that we can change the right page! return frame; 

}

So, to make the long story short, I get a pop-up (in windows) which says that the problem was and should be closed.

When I try to replace * PTR ++; with * PTR + = 1;

The program starts. This interested me, so I used -S in gcc With the * ptr ++ version, I get this instruction:

 addl $4, -20(%ebp) 

with * p + = 1 I get

 movl -20(%ebp), %eax movl (%eax), %eax leal 4(%eax), %edx movl -20(%ebp), %eax movl %edx, (%eax) 

Which seems to me a very similar operation, therefore, assuming that I am reading it correctly,

case 1

An increment of -20 (% ebp) by 4 (assuming $ 4 means that)

case 2

we save what is in ebp for eax,

(not sure what () does), but do it again?

then grab and load the address from eax offset by 4 in edx,

now copy ebp back to eax again

now copy edx to eax,

I mean, it looks like they are doing the same thing, but for some reason * ptr ++! = * Ptr + = 1

Why? What am I missing from what I see?

EDIT: THANKS Everything now I feel special, I canโ€™t believe that I did not realize this!

+4
source share
4 answers

Actually, this is a problem with operator precedence . The postfix increment operator is applied before the pointer is dereferenced, which means that you increment the value of the pointer (points to memory) and then try to dereference it. This causes undefined behavior because it points to a single int .

In the latter case, += is applied after dereferencing, so you get the value stored in the pointer's memory, then add it to it. You must make sure that dereferencing happens first if you want to use the postfix increment, which you can do using:

 (*ptr)++; 

instead of what you have.

+6
source

it

 *ptr++; // For some reason ++ doesn't work. 

it should be

  (*ptr)++; 

Here the operator takes precedence - *ptr++ works as follows: *(ptr++) - just like

  while( ( *t++ = *s++ ) != 0 ); 

copies the line with zero completion.

+2
source

Operator priority does not match. They are respectively equivalent:

 *(ptr++); 

and

 (*ptr) += 1; 

You can do *(ptr)++; to increase the specified value and leave the pointer itself.

+2
source

The ++ operator has a higher precedence than * , which has a higher priority than += . ++ binds to ptr , and += binds to (*ptr) .

+1
source

All Articles