Re: a few doubts!



SM Ryan <wyrmwif@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx> writes:
> "maadhuu" <madhu_ranjan_m@xxxxxxxxx> wrote:
> > #include<stdio.h>
> > #include<conio.h>
> > #include<string.h>
> > main()
> > {
> > char * a= "bcd";
> > clrscr();
> > strcpy(a,"hello");
> > a = "fgh";
> > a[0] = 't';
> > printf("%s",a);
> > }
>
> Some compilers allow strings to be overwritten by placing them in
> read-write memory sometimes with the expectation the string can be
> modified. Writing outside an array bounds does not necessarily
> trigger a fault action.
>
> The code results are machine and compiler dependent.

[ Non-traditional '#' quoting character changed to '>'. ]
[ Excessively long lines reformatted. ]
[ Becoming seriously annoyed by gratuitously non-traditional formatting. ]

To be precise, an attempt to modify a string literal invokes undefined
behavior. An implementation is allowed to place string literals in a
read-only memory segment, causing a trap whenever the program attempts
to write to them. It's also allowed to place them in read-write
memory, and even to share the same memory for different occurrences of
the same string. For example, this:

#include <stdio.h>
#include <string.h>

int main(void)
{
char *s = "hello, world\n";
strcpy(s, "HAHA!\n");
printf("hello, world\n");
return 0;
}

could legally do any of the following:

Blow up on the strcpy() call (perhaps with a segmentation fault).
Print "hello, world".
Print "HAHA!".
Print "You have invoked undefined behavior and your mother wears army boots."
Make demons fly out your nose.

Some compilers may have an an option to control whether string
literals are stored in read-only or read-write memory. Such an option
is useful mostly to diagnose this kind of problem (or, if you're
desparate, to work around it). The only real solution is to avoid
writing to string literals in the first place.

One oddity is that, for historical reasons, a string literal is not
treated as "const". If it were, the compiler would be required to
diagnose most cases of this error.

--
Keith Thompson (The_Other_Keith) kst-u@xxxxxxx <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
.



Relevant Pages

  • Re: PROBLEM
    ... tmp.c:1: warning: initialization discards qualifiers from pointer ... The problem with C compilers is that these options are not enabled ... Compiling new C code in a mode that flags possible writes to string ... become illegal if the language were modified to make string literals ...
    (comp.std.c)
  • Re: Fast string operations
    ... Looping: I thought looping over arrays in managed code was "slow" ... array handling and such. ... The problem with TrimHelper is that it always returns a new string instance. ... The customer perceives this as a memory leak. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: understanding pointers
    ... cptr now points to the first character of the ... It's undefined behavior to modify string literals. ... and your compiler might have put in read-only memory. ...
    (comp.lang.c)
  • Re: Discovering variable types...
    ... >- but I suppose MS expect us to use wrappers ... memory allocations for your variables from disk as well. ... >They most certainly are of fixed size, changing the size of a String ... >>me to keep buffer size and current postion right in the memory block. ...
    (comp.lang.pascal.delphi.misc)
  • Re: Optimize
    ... if you use it like me to get 'the next string ptr' in addition. ... | |mov ebx eax;dw aligned strings are faster ... | Would it be enough to touch each 16th bytes in the 64K memory area ... Cache-line size is fixed, my AMD got 64 bytes per line. ...
    (alt.lang.asm)