www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Segfault, why ?

reply Yves Jacoby <yves kloune.net> writes:
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;
}
Jan 27 2006
next sibling parent reply Tom S <h3r3tic remove.mat.uni.torun.pl> writes:
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 +/
Jan 27 2006
parent Yves Jacoby <kloune gmail.com> writes:
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. :-)
Jan 27 2006
prev sibling parent xs0 <xs0 xs0.com> writes:
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
Jan 27 2006