Menu

#4056 Pointer math on void * results in no change

open
nobody
None
other
5
11 hours ago
19 hours ago
Under4Mhz
No

The following code incorrectly calculates the pointer value. void * is still a pointer and should be able to add to it. gcc seems to default to a size of 1 byte for void * .
If the decision is void * is of unknown size and should not be able to add it, then I think an error would be more appropriate. I was expecting the gcc behaviour and it was unexpected to find it adding nothing, without a warning.

/// GPL 2.0 or later
#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>

#define VDU_RAM_START 0xc000

void GameSet( uint8_t game ) {

    printf( "%p\n", (void*)VDU_RAM_START + 0x100 );
    printf( "%p\n", ((void*)VDU_RAM_START) + 0x100 );
    printf( "%p\n", (void*)(VDU_RAM_START + 0x100) );
    printf( "%p\n", (uint8_t*)VDU_RAM_START + 0x100 );

    void *ptr = (void*)VDU_RAM_START;
    ptr++;

    printf( "%p\n", ptr );
}

void main(void) {

    printf( "Start\n" );

    GameSet( 1 );

    printf( "End\n" );
}

#ifdef __SDCC
__sfr __at 0xff sif;
int putchar( int c ) {

    sif = 'p';
    sif = c;

    return c;
}
#endif
$ sdcc -mz80 --fverbose-asm ./ptr_math.c  -o ptr_math.ihx && ucsim_z80 -I if=outputs[0xff] ptr_math.ihx
Simulation started, PC=0x000000
Start
0xc000
0xc000
0xc100
0xc100
0xc000
End


$ gcc ./ptr_math.c && ./a.out
Start
0xc100
0xc100
0xc100
0xc100
0xc001
End
$ sdcc -v
SDCC : z80/sm83/ez80/z80n/mos6502/mos65c02 4.6.2 #16701 (Linux)
2 Attachments

Discussion

  • Maarten Brock

    Maarten Brock - 18 hours ago

    AFAIK in general sizeof(void) is undefined behaviour.
    SDCC has chosen that the size of void is zero and thus a void* increments with 0.

     
  • Maarten Brock

    Maarten Brock - 18 hours ago

    Looking deeper in the C standard (my emphasis):

    6.2.5.24 The void type comprises an empty set of values; it is an incomplete object type that cannot be completed.

    6.5.3.4.1 The sizeof operator shall not be applied to an expression that has function type or an incomplete type, to the parenthesized name of such a type, or to an expression that designates a bit-field member. The alignof operator shall not be applied to a function type or an incomplete type.

    Thus, SDCC should probably output at least a warning and possibly an error.

     
    • Benedikt Freisen

      Benedikt Freisen - 11 hours ago

      Yes. I agree that this should probably result in a diagnostic message, regardless of the chosen behavior. I wouldn't mind sizeof(void) == 1, though.

       
  • Maarten Brock

    Maarten Brock - 13 hours ago

    See also [#842], [#715] and [#133 ].

     

    Related

    Bugs: #133
    Bugs: #715
    Bugs: #842


Log in to post a comment.

MongoDB Logo MongoDB