GNU compiler bug?

Mike Harrold mharrold@cas.org
Thu Sep 30 19:57:00 GMT 1999


> 
> Hello VxWorkers!
> 
> I’m  extremely puzzled by a bug I've discovered in the GNU compiler. It
> can be reproduced by this simple example of code:
> 
> /** begin of the example (gnu_bug.c) **/
> #include <stdlib.h>
> #include <string.h>
> void myfunc(char*s, int *x)
> {  char *loc;
> 
>    loc=malloc(246);
>    strcpy(loc,"11111");
>    free(s);		/*    pushl %ebx           */
> 			/*    call 0x11bf70 <free> */
>     s= loc;		/**** NO CODE GENERATED ! ***/

This is saying 'assign "loc" to "s"'. Since s is a local variable
and is not used again in the function, the compiler has optimised
it out. At a glance it appears you are making a fundamental mistake
regarding pointers.

>    *x= 246;		/*    movl $0xf6, (%esi)   */
> 
> }

Replace the function with the following:

void myfunc(char** s, int* x)
{
  char* loc;

  loc = malloc(246);
  strcpy(loc, "11111");
  free(*s);
  *s = loc;
  *x = 246;
}

or if you're using C++

void myfunc(char*& s, int* x)
{
  char* loc;

  loc = malloc(246);
  strcpy(loc, "11111");
  free(s);
  s = loc;
  *x = 246;
}

Regards,

/Mike


> void main(void)
> {
> char *str; int y;
> str=malloc(246);
> strcpy(str,"sdfsd");
> myfunc(str,&y);
> }
> /** end of the example  (gnu_bug.c) **/
> 
> The problem is that no code is generated for the source line “s=loc;”
> in the myfunc() !
> This is also what I saw in the disassembled code.
> Here I brought the compilation options on my x86 BSP:
> 
> cc386 -O -fvolatile -nostdlib -fno-builtin -fno-defer-pop -m486 -Wall -
> I/h   -I. -IC:\Tornado\target\config\all -IC:\Tornado\target/h -
> IC:\Tornado\target/src/config -IC:\Tornado\target/src/drv -ansi -
> nostdinc -DCPU=I80486   -Wp,-lang-c++ -o gnu_bug.o -g -Winline -c gnu_
> bug.c
> 
> Any thoughts guys?
> 
> Thanks,
> Alex
> 
> - VxWorks -
> 
> 
> Sent via Deja.com http://www.deja.com/
> Share what you know. Learn what you don't.
> 



More information about the Gcc-bugs mailing list