Segmentation Problem

Matthew Woehlke mw_triad@users.sourceforge.net
Tue Sep 16 01:08:00 GMT 2008


John Love-Jensen wrote:
>>  char *s="",t;
> 
> Here you have allocated one byte of memory for s.
> 
>>  int i,j,l,c=0;
>>  printf("\n Enter String to be Bit Stuffed : ");
>>  scanf("%s",s);
> 
> Here you are scanning into s.  The s buffer is only one byte long.
> Depending on your compiler settings, the s buffer may be read-only (or maybe
> not... my C is rusty, and perhaps I'm referring to a C++ -ism).  You have
> not taken any precautions to insure that the scanf does not overflow the
> buffer (out of bounds error, which could result in a SEGV).

IIRC, the code:

char *s = "hello";

...has an implicit cast-from-const (the RHS has type 'const char *'), 
and trying to write to it will raise a SEGV. Before gcc4 there was 
-fwrite-strings, which would put string constants in writable memory, 
but it's been deprecated for a LONG time and was removed in gcc4.

-- 
Matthew
"NT was a marketing name that stood for New Technology, but it was still 
an amusing coincidence that WNT was VMS with each letter replaced by the 
next one."
   -- Jeremy Reimer



More information about the Gcc-help mailing list