S
Stephan Beal
Hi, all!
Today i decided to turn on gcc's -pedantic flag on some of my code,
and i was rewarded with this:
whio_dev_mem.c:315: error: pointer of type ‘void *’ used in arithmetic
The offending code is part of an i/o device interface where the only
option for this particular bit (and several others like it) is to add
offsets to (void*):
memset( b + mb->alloced, 0, alen - mb->alloced );
In this particular case, i've realloc()ed memory to a larger size and
i'm zeroing out the new bytes. In some cases i have loops where i'm
having to iterate over a large input set (provided by a (void*) +
length info) and send the data via a smaller buffer. In those cases i
need to add to the input (void*) to get my current read/write offset.
i remember getting the same warning (but not an error) when compiling
this code on the SunStudio compiler (but without any special flags).
Since two compilers are bitching at me about this...
My question is: is it strictly illegal to add to a (void*) like this?
If it is illegal, strictly speaking, is there an alternative? Would it
instead be portable/legal to cast the (void*) to (unsigned char*) and
do the addition on that result?
My current workaround is to define a macro:
#if 0 /* enable this if you want to build with -pedantic. */
# define WHIO_VOID_PTR_ADD(VP,PLUS) ((void*)((intptr_t)VP+PLUS))
# define WHIO_VOID_CPTR_ADD(VP,PLUS) ((void const*)((intptr_t)VP
+PLUS))
#else
# define WHIO_VOID_PTR_ADD(VP,PLUS) (VP+PLUS)
# define WHIO_VOID_CPTR_ADD(VP,PLUS) (VP+PLUS)
#endif
and change the offending calls to use this instead of directly doing
(void*) addition.
Your insights and suggestions for improvements would be greatly
appreciated!
:-?
Today i decided to turn on gcc's -pedantic flag on some of my code,
and i was rewarded with this:
whio_dev_mem.c:315: error: pointer of type ‘void *’ used in arithmetic
The offending code is part of an i/o device interface where the only
option for this particular bit (and several others like it) is to add
offsets to (void*):
memset( b + mb->alloced, 0, alen - mb->alloced );
In this particular case, i've realloc()ed memory to a larger size and
i'm zeroing out the new bytes. In some cases i have loops where i'm
having to iterate over a large input set (provided by a (void*) +
length info) and send the data via a smaller buffer. In those cases i
need to add to the input (void*) to get my current read/write offset.
i remember getting the same warning (but not an error) when compiling
this code on the SunStudio compiler (but without any special flags).
Since two compilers are bitching at me about this...
My question is: is it strictly illegal to add to a (void*) like this?
If it is illegal, strictly speaking, is there an alternative? Would it
instead be portable/legal to cast the (void*) to (unsigned char*) and
do the addition on that result?
My current workaround is to define a macro:
#if 0 /* enable this if you want to build with -pedantic. */
# define WHIO_VOID_PTR_ADD(VP,PLUS) ((void*)((intptr_t)VP+PLUS))
# define WHIO_VOID_CPTR_ADD(VP,PLUS) ((void const*)((intptr_t)VP
+PLUS))
#else
# define WHIO_VOID_PTR_ADD(VP,PLUS) (VP+PLUS)
# define WHIO_VOID_CPTR_ADD(VP,PLUS) (VP+PLUS)
#endif
and change the offending calls to use this instead of directly doing
(void*) addition.
Your insights and suggestions for improvements would be greatly
appreciated!
:-?