Sujet : Re: relearning C: why does an in-place change to a char* segfault?
De : Keith.S.Thompson+u (at) *nospam* gmail.com (Keith Thompson)
Groupes : comp.lang.cDate : 01. Aug 2024, 22:06:03
Autres entêtes
Organisation : None to speak of
Message-ID : <87frrogdhg.fsf@nosuchdomain.example.com>
References : 1 2
User-Agent : Gnus/5.13 (Gnus v5.13)
Kaz Kylheku <
643-408-1753@kylheku.com> writes:
On 2024-08-01, Mark Summerfield <mark@qtrac.eu> wrote:
[...]
int main() {
char* text = "this is a test";
>
The "this is a test" object is a literal. It is part of the program's image.
When you try to change it, you're making your program self-modifying.
The ISO C language standard doesn't require the object to be part of the
program's image. A fully conforming implementation could store it in
read/write memory and allow it to be modified. Or, it could store it in
some kind of storage where attempts to write to it appear to succeed,
but do not actually modify it (this is implausible, but allowed by the
standard).
The ISO C language standard doesn't require implementations to support
self-modifying programs; the behavior is left undefined.
The behavior is undefined because the standard explicitly says so.
There is no reference to self-modifying programs.
[...]
-- Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.comvoid Void(void) { Void(); } /* The recursive call of the void */