#715 SDCC should throw a warning on sizeof (void).

closed-fixed
Maarten Brock
5
2013-05-25
2004-02-25
Josef Pavlik
No

hello
I found, that is impossible to cast the type of pointer when I want to increment it.
When I try something like this:
void *voidptr;
((unsigned int *)voidptr)++;
I got the error: 'lvalue' required for '++' operation.
If I omit the parenthesis, no increment is done because no type casting is done (because the priority of operands) and the size of void is 0.

If I try to add the sizeof of unsigned int:
ptr+=sizeof(unsigned int);
nothing happens, because in the sdcc the size of void is 0.
I think that this is not right. In this case is impossible to modify an void pointer. The gcc thinks that size of void is 1, so voidptr+=sizeof(something) makes sense.

I thing, that sizeof anything may not be 0. sizeof(void), increment void* etc may produce an error or something, but does not silently ignore it. My opinion is that the right way is make it 1, like makes gcc.

Discussion

  • Bernhard Held
    Bernhard Held
    2004-02-25

    Logged In: YES
    user_id=203539

    The only problem is that SDCC should throw a warning on sizeof (void).

    From Marc Nijweide <nijweide AT xs4all.nl> 2001-09-10, sdcc-devel,
    discussion of bug #460185:

    sizeof(void) is an invalid expression. sizeof is not
    allowed on void expressions, it is neither 0 nor any
    other value. That gcc decides that sizeof(void)=1
    is a gcc extension to the ANSI C language, they know
    it and supply a flag to turn of this feature.... Also pointer
    arithmetic on void pointers does have its conceptual flaws
    as void pointers are intended to be used when the function
    is not allowed or does not need to make assumptions about
    the type the pointer is pointing too.

    See also:
    http://www.lysator.liu.se/c/rat/c3.html#sizeof-3-3-3-4

     
  • Bernhard Held
    Bernhard Held
    2004-02-25

    • summary: pointer cast problem and sizeof(void) should'nt be 0 --> SDCC should throw a warning on sizeof (void).
     
  • Josef Pavlik
    Josef Pavlik
    2004-02-26

    Logged In: YES
    user_id=756222

    The main problem is, that is impossible to cast another type of pointer on void pointer when I try to increment it. See the first paragraph of my original message. The sizeof(void) is only secondary problem.

     
  • Bernhard Held
    Bernhard Held
    2004-02-26

    • labels: 101550 --> C-Front End
     
  • Bernhard Held
    Bernhard Held
    2004-02-26

    Logged In: YES
    user_id=203539

    Oh, sorry, yes you're right. I filed a 2nd bug report #905167,
    because these are 2 different problems.

     
  • Erik Petrich
    Erik Petrich
    2004-02-26

    Logged In: YES
    user_id=635249

    Incrementing a casted object is not legal C (since casts are
    not lvalues); both of your examples are depending on gcc
    extensions. Instead of:

    ((unsigned int *)voidptr)++;

    use:

    voidptr = (unsigned int *)voidptr + 1;

     
  • Josef Pavlik
    Josef Pavlik
    2004-02-26

    Logged In: YES
    user_id=756222

    Thanx for the idea. But ((unsigned int *)voidptr)++; is so beautiful :-) And all
    C compilers that I've seen allows it.

     
  • Maarten Brock
    Maarten Brock
    2004-09-09

    Logged In: YES
    user_id=888171

    Fixed: SDCC 2.4.4 #833 generates a warning

     
  • Maarten Brock
    Maarten Brock
    2004-09-09

    • milestone: --> fixed
    • assigned_to: nobody --> maartenbrock
    • status: open --> closed-fixed