To dup or not to dup?
Jürgen Herz
juergen at jherz.redirectme.net
Tue Feb 27 03:02:56 PST 2007
Frits van Bommel wrote:
> Jürgen Herz wrote:
>> It also isn't enforced for C on Linux (gcc) though it also doesn't
>> crash. Anyways, that's C and D (resp. dmd) could do better.
>
> With my Linux gcc it *does* crash:
> ---
> urxae at urxae:~/tmp$ cat test.c
> int main() {
> char* str = "test";
> str[0] = 'b';
> }
> urxae at urxae:~/tmp$ gcc test.c -o test
> urxae at urxae:~/tmp$ ./test
> Segmentation fault (core dumped)
> ---
Uh, sorry. I mixed up C and D and just wrote
char a[] = "Test";
in the C test. That's different from []a on D of course.
> There are plans to add a better concept of non-modifiability to D,
Having a 1.0 I was under the impression such basics would have been
done. But if it's not I'm glad to hear it's worked on.
>> > On windows the result of that code is very interesting. Since it doesn't
>> > segfault, one can printf a and s after manipulation. And it's "Txst" in
>> > test() and "Test" outside.
>
> Yes, it allows it to be modified, which is why it doesn't crash.
Yes, but what I'm surprised is that s and a seem to point to different
memory (seem because I didn't look at the actual addresses - have to
make up for that when on Win again) since the string a was unchanged
even after s was changed in test().
Jürgen
More information about the Digitalmars-d
mailing list