tech-userlevel archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

Re: why cast to char* through void*



At Wed, 1 Jul 2009 22:33:16 +0000 (UTC), christos%astron.com@localhost 
(Christos Zoulas) wrote:
Subject: Re: why cast to char* through void*
> 
> In article <m1MM5sb-000kmuC%most.weird.com@localhost>,
> Greg A. Woods <tech-userlevel%NetBSD.org@localhost> wrote:
> >-=-=-=-=-=-
> >
> >It doesn't make any sense to me -- there shouldn't be any alignment
> >work-arounds necessary here (should there?), and I see no other possible
> >reason for it.
> >
> >
> >--- login_cap.c      10 Feb 2007 12:57:39 -0500      1.25
> >+++ login_cap.c      01 Jul 2009 15:14:06 -0400      
> >@@ -517,8 +517,8 @@
> >     if (!res)
> >             return -1;
> >     
> >-    ptr = (char *)(void *)&res[count];
> >-    (void)strcpy(ptr, str);
> >+    ptr = (char *) &res[count];
> >+    (void) strcpy(ptr, str);
> > 
> >     /* split string */
> >     for (i = 0; (res[i] = stresep(&ptr, stop, '\\')) != NULL; )
> >
> 
> Typically used to shut up gcc type punning warnings. Not that it is the TRTTD
> in all cases. Does anyone know if this makes gcc produce "the wanted" code
> (has the effect of -fno-strict-aliasing) or it just shuts up the warning?

As I said, the cast through void* is not necessary here on at least i386
neither for generating the correct result, nor for shutting up GCC.

See the following example, and in particular the compiler options in the
comment at the bottom:


/* tcharstarstararray.c */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(void);

int
main(void)
{
        unsigned int count = 2;
        char *ptr;
        char *res_storage[10];
        char **res = res_storage;
        char str_storage[1000];
        char *str = str_storage;

        ptr = (char *) (void *) &res[count];
        printf("ptr = %p\n", ptr);

        ptr = (char *) &res[count];
        printf("ptr = %p\n", ptr);

        ptr = &res[count];      /* only this will generate a GCC warning */
        printf("ptr = %p\n", ptr);

        (void) strcpy(ptr, str);

        exit(0);
        /* NOTREACHED */
}


/*
 * Local Variables:
 * eval: (make-local-variable 'compile-command)
 * compile-command: "make 'CFLAGS=-O2 -g -Wall -Wimplicit -Wreturn-type 
-Wswitch -Wcomment -Wtraditional -Wextra -Wcast-qual -Wpointer-arith -Wshadow 
-Wstrict-prototypes -Waggregate-return -Wcast-align -Wchar-subscripts 
-Wconversion -Wmissing-declarations -Wmissing-prototypes -Wno-long-long 
-Wformat-extra-args -Wundef -Wbad-function-cast -Wdeclaration-after-statement 
-DBIND_8_COMPAT -pipe  -Wall -Wimplicit -Wreturn-type -Wswitch -Wcomment 
-Wtraditional -Wextra -Wcast-qual -Wpointer-arith -Wshadow -Wstrict-prototypes 
-Waggregate-return -Wcast-align -Wchar-subscripts -Wconversion 
-Wmissing-declarations -Wmissing-prototypes -Wno-long-long -Wformat-extra-args 
-Wundef -Wbad-function-cast -Wdeclaration-after-statement' tstatic_assert"
 * End:
 */ 


-- 
                                                Greg A. Woods
                                                Planix, Inc.

<woods%planix.com@localhost>       +1 416 218-0099        http://www.planix.com/

Attachment: pgpE6RhYJqUMX.pgp
Description: PGP signature



Home | Main Index | Thread Index | Old Index