Menu

#842 Address of a void ptr doesn't change

closed-rejected
nobody
None
5
2013-05-25
2004-12-05
Hubert Sack
No

I read the RFE 905167 and wrote a short senseless
program, it results in detecting a bug:
sdcc -v
SDCC : mcs51/ds390/pic16/TININative/xa51/ds400 2.4.7
#897

int *intptr;
void *voidptr;

char main (void)
{
int x[2] = {0,4711};
char res;

voidptr = x;
intptr = x;

/*
< voidptr++; // No warning, but o.k. since sizeof(void)
== 0
*/
voidptr += sizeof(int); // but here it's *NOT*
incremented, too
intptr++;

res = (*(int *)voidptr == *intptr); // should be true

return(res);
}

Related

Bugs: #4056

Discussion

  • Hubert Sack

    Hubert Sack - 2004-12-05

    All files (test.c, test.asm, ...) as an windows zip archive

     
  • Maarten Brock

    Maarten Brock - 2004-12-05
    • milestone: --> non_bugs
    • status: open --> closed-rejected
     
  • Maarten Brock

    Maarten Brock - 2004-12-05

    Logged In: YES
    user_id=888171

    This is no bug. If you agree voidptr++ doesn't increment is
    ok, than you should know that voidptr+=sizeof(int) doesn't
    increment either. Sizeof(int) only returns a number, just like
    ++ means +=1. In this case 1 is the number. This number
    must be multiplied by the size of the object the pointer points
    to. In this case 0.

    If you want to use sizeof() to increment the pointer to the
    next object, it must be added to a char* casted version:

    voidptr = (char*)voidptr + sizeof(int);

    Hope this clarifies things

     

Log in to post a comment.