Re: Comments please
From: Sean Kenwrick (skenwrick_at_hotmail.com)
Date: 01/21/04
- Next message: Buck Rogers: "Help with a progam"
- Previous message: Martin Ambuhl: "Re: is there any powerful program that can....."
- In reply to: Christopher Benson-Manica: "Re: Comments please"
- Next in thread: Christopher Benson-Manica: "Re: Comments please"
- Reply: Christopher Benson-Manica: "Re: Comments please"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Wed, 21 Jan 2004 09:10:14 +0000 (UTC)
"Christopher Benson-Manica" <ataru@nospam.cyberspace.org> wrote in message
news:bukccm$ijh$1@chessie.cirr.com...
> Sean Kenwrick <skenwrick@hotmail.com> spoke thus:
>
> > void printdate(char *date)
> > {
> > printf( "New date is %s-%s-%s\n",
> > strtok(NULL,"/"),strtok(NULL,"/")-3,strtok(date,"/")+3);
> > }
>
> > try it, it works!
>
> Okay...
>
> $ cat t.c
>
> #include <stdio.h>
> #include <stdlib.h>
> #include <string.h>
>
> void printdate(char *date)
> {
> printf( "New date is %s-%s-%s\n",
> strtok(NULL,"/"),strtok(NULL,"/")-3,strtok(date,"/")+3);
> }
>
> int main(void)
> {
> char str[256];
> strcpy( str, "09/12/2003" ); /* strtok modifies its argument! */
> printdate( str );
> return 0;
> }
>
> $ gcc -O2 -Wall -ansi -pedantic t.c
> $ ./a.out
> Segmentation fault (core dumped)
>
> Uh oh. Looks like undefined behavior to me...
>
Just out of curiosity - what happens if you turn off optimisation? I
suspect that this is causing the parameters to be passed through registers,
not on the stack.
I know that the standard does not define the order of parameter passing,
but I would expect the default behaviour of compilers to usually pass
parameters in reverse oder onto the stack so that variable length parameter
lists can be more easily acheived. Obviously you shouldn't ever rely on
this behavior - its just a curious side effect of compiler architecture
which I attemted to exploit in the code above....
Let me know if gcc still fails without the optimisation....
Sean
- Next message: Buck Rogers: "Help with a progam"
- Previous message: Martin Ambuhl: "Re: is there any powerful program that can....."
- In reply to: Christopher Benson-Manica: "Re: Comments please"
- Next in thread: Christopher Benson-Manica: "Re: Comments please"
- Reply: Christopher Benson-Manica: "Re: Comments please"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|