Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #3969

Re: I need help

From Keith Thompson <kst-u@mib.org>
Newsgroups comp.lang.c
Subject Re: I need help
Date 2011-05-14 11:33 -0700
Organization None to speak of
Message-ID <lnsjshgmic.fsf@nuthaus.mib.org> (permalink)
References <45f62c12-a439-42b7-be27-bab7c954e209@glegroupsg2000goo.googlegroups.com>

Show all headers | View raw


janus <emekamicro@gmail.com> writes:
> Another thing it is likely to print is "SIGSEGV" or "SIGBUS" or
> "General Protection Fault" or something along those lines.  The
> anonymous array created by the string literal is not `const', but
> that's just an accident of history.  Trying to modify such an array
> yields undefined behavior -- and indeed, plenty of compilers will
> put the arrays in read-only memory.  (This allows them to save space
> by allocating just one array to hold both of "over" and "recover".)
>
> I am a bit confused by the above...
>
> Is the correct?
>
> char* string = "NEW";
> strcat(string, "Jersey");
>
> If string is in read-only memory, could it still support things like
> concatenation?

No.

On my system, a program containing those two lines died with a
segmentation fault.

But of course that's not the only possible result.  String literals
(more accurately, the static arrays associated with them) aren't
*necessarily* stored in read-only memory.  The strcat() call will
attempt to store the 7 characters of "Jersey" (6 plus the trailing
'\0') at locations starting after the 'W' in "NEW".  This might
appear to work, if that area of memory happens to be writable
and happens not to be used for anything else.  On the other hand,
it could clobber some other variable, or write over some internal
information (such as the function's return address), with arbitrarily
bad results that might not even appear until later.

And the phrase "will attempt" above isn't strictly correct.  Since
the behavior is undefined, strcat() could conceivably detect that it
won't be able to write the characters and do something else instead.

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
Nokia
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Re: I need help janus <emekamicro@gmail.com> - 2011-05-14 11:15 -0700
  Re: I need help "Heinrich Wolf" <invalid@invalid.invalid> - 2011-05-14 20:29 +0200
    Re: I need help Phil Carmody <thefatphil_demunged@yahoo.co.uk> - 2011-05-15 13:03 +0300
      Re: I need help "Heinrich Wolf" <invalid@invalid.invalid> - 2011-05-15 13:18 +0200
        Re: I need help Phil Carmody <thefatphil_demunged@yahoo.co.uk> - 2011-05-16 04:31 +0300
    Re: I need help Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-05-15 08:13 -0400
      Re: I need help "Heinrich Wolf" <invalid@invalid.invalid> - 2011-05-15 14:30 +0200
        Re: I need help Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-05-15 08:45 -0400
          Re: I need help Phil Carmody <thefatphil_demunged@yahoo.co.uk> - 2011-05-16 04:34 +0300
    Re: I need help Ian Collins <ian-news@hotmail.com> - 2011-05-16 13:42 +1200
      Re: I need help Ben Bacarisse <ben.usenet@bsb.me.uk> - 2011-05-16 03:28 +0100
      Re: I need help pete <pfiland@mindspring.com> - 2011-05-15 22:45 -0400
        Re: I need help Ian Collins <ian-news@hotmail.com> - 2011-05-16 14:50 +1200
      Re: I need help "Heinrich Wolf" <invalid@invalid.invalid> - 2011-05-16 12:26 +0200
  Re: I need help Keith Thompson <kst-u@mib.org> - 2011-05-14 11:33 -0700
  Re: I need help Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-05-15 07:58 -0400

csiph-web