digitalmars.D - Segfault, why ?
- Yves Jacoby (20/20) Jan 27 2006 Hi,
- Tom S (11/15) Jan 27 2006 It fails because D strings aren't ended by zeros. printf is trying to
- Yves Jacoby (2/11) Jan 27 2006 Thank you, first D try. :-)
- xs0 (8/38) Jan 27 2006 try
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
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
On Fri, 27 Jan 2006 17:43:49 +0100, Tom S wrote:Yves Jacoby wrote:Thank you, first D try. :-)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
Jan 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
Jan 27 2006