Thread overview
Segfault, why ?
Jan 27, 2006
Yves Jacoby
Jan 27, 2006
Tom S
Jan 27, 2006
Yves Jacoby
Jan 27, 2006
xs0
January 27, 2006
Hi,

Could someone tell me why this program segfaults ? None of the asserts fail, the print statements were because I didn't trust the asserts.

System information: gdc (GCC) 3.4.5 (gdc 0.17, using dmd 0.140, Gentoo
0.17-r1)

------------

int main(char[][] args){

        int i;

        assert(args!=null);
        printf("Argument list %d %d\n",args.sizeof,args.length);

        for(i=0;i<args.length;i++){

                printf("B");
                assert(i<args.length);
                assert(args[i]!=null);
                printf("A");
                printf("Index: %d  length: %d value: %s\n", i,
args[i].length, args[i]);
        }

        return 0;
}

January 27, 2006
Yves Jacoby wrote:
> Hi, 
> 
> Could someone tell me why this program segfaults ? None of the asserts fail,
> the print statements were because I didn't trust the asserts. 

It fails because D strings aren't ended by zeros. printf is trying to use strlen on the string arguments and thus fails, looking for the zero. Replace printf with writef or writefln and you'll be fine



-- 
-----BEGIN GEEK CODE BLOCK-----
Version: 3.1
GCS/M d-pu s+: a-->----- C+++$>++++ UL P+ L+ E--- W++ N++ o? K? w++ !O !M V? PS- PE- Y PGP t 5 X? R tv-- b DI- D+ G e>+++ h>++ !r !y
------END GEEK CODE BLOCK------

Tomasz Stachowiak  /+ a.k.a. h3r3tic +/
January 27, 2006
On Fri, 27 Jan 2006 17:43:49 +0100, Tom S wrote:

> Yves Jacoby wrote:
>> Hi,
>> 
>> Could someone tell me why this program segfaults ? None of the asserts fail, the print statements were because I didn't trust the asserts.
> 
> It fails because D strings aren't ended by zeros. printf is trying to use strlen on the string arguments and thus fails, looking for the zero. Replace printf with writef or writefln and you'll be fine

Thank you, first D try. :-)

January 27, 2006
Yves Jacoby wrote:
> Hi, 
> 
> Could someone tell me why this program segfaults ? None of the asserts fail,
> the print statements were because I didn't trust the asserts. 
> 
> System information: gdc (GCC) 3.4.5 (gdc 0.17, using dmd 0.140, Gentoo
> 0.17-r1)
> 
> ------------
> 
> int main(char[][] args){
> 
>         int i;
> 
>         assert(args!=null);
>         printf("Argument list %d %d\n",args.sizeof,args.length);
> 
>         for(i=0;i<args.length;i++){
> 
>                 printf("B");
>                 assert(i<args.length);
>                 assert(args[i]!=null);
>                 printf("A");
>                 printf("Index: %d  length: %d value: %s\n", i,
> args[i].length, args[i]);
>         }
> 
>         return 0;
> }
> 

try

writefln("Index: %d length: %d value: %s", i, args[i].length, args[i]);

The reason would be that printf is from C and expects a char*, but gets a char[], which is length first, char* second. Dereferencing length is almost guaranteed to segfault :)

BTW, this should go to digitalmars.D.learn


xs0