From: pvcf Subject: Re: strcpy() bug? To: djgpp AT sun DOT soe DOT clarkson DOT edu Date: Tue, 8 Mar 1994 13:53:17 -0500 (EST) > The following program: [example deleted] > produces the output: > > C:\STU>gcc -Wall test.c > > C:\STU>go32 a.out > go32 version 1.11.maint3 Copyright (C) 1993 DJ Delorie > text text > text > > Whereas if *element1[] is initialised as: > > char *element1[]={"length","short"}; > > the outputs from the two printf() statements are identical. i.e. when > the two strings are the same, strcpy() appears to copy `text' to > &element1[0][0] AND &element1[0][1]. This does not occur with MS and > Borland C. > > Any ideas? I think what is happening is the compiler is merging the duplicate strings to save storage in the initialized data segment. I thought that there used to be an explicit option to do this in gcc. Perhaps this was made a default action some time ago. At least, it seems to be in gcc 2.5.7+. I think you may be able to fix this by using the -fwritable-strings option. I tried your example code on a Unix machine using gcc 2.5.7 and when compiled without -fwritable-strings, it seg faults and dumps core. Of course, there is no memory protection on PCs, so you can write anywhere you want, but most Unixes have memory protection and would not allow writes to the initialized data segment. Adding -fwritable-strings fixed this problem and produced the correct results on Unix. (Sorry, not at home to test). I have a feeling that -fwritable-strings may force the compiler to create 2 copies of "length" since you are telling it you intend, or at least would like the ability to write over top of those initialized strings. Duh! Why don't I just check the manpage.... :) From gcc 2.5.7 man page: -fwritable-strings Store string constants in the writable data segment and don't uniquize them. This is for compatibility with ----------^^^^^^^^^^^^^^^ old programs which assume they can write into string constants. `-traditional' also has this effect. Writing into string constants is a very bad idea; ``constants'' should be constant. Hope this helps. ....Paul -- |=========================|\-/\-\/-/\-/| CYGNUS Computer Associates Ltd. | | Paul V C Frattaroli |/_\_/\/\_/_\| Computer Consultants | | |\ / \/\/ \ /| Minto Plaza, 38 Elm St, Suite 2109 | |=========================|/-\/-/\-\/-\| Toronto, ON. M5G-2K5 (416) 977-6996 |