Path: ...!3.eu.feeder.erje.net!feeder.erje.net!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: James Kuyper Newsgroups: comp.lang.c Subject: Re: relearning C: why does an in-place change to a char* segfault? Date: Thu, 1 Aug 2024 12:02:30 -0400 Organization: A noiseless patient Spider Lines: 44 Message-ID: References: MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit Injection-Date: Thu, 01 Aug 2024 18:02:31 +0200 (CEST) Injection-Info: dont-email.me; posting-host="083f7e27dab0955cdb6680690189a52d"; logging-data="2362899"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+uRT6BZpjffwDMb/yA9dVoERv7ZN7NA84=" User-Agent: Mozilla Thunderbird Cancel-Lock: sha1:HQ3RVbzzuyJuR+6ntcWJNzlea3c= In-Reply-To: Content-Language: en-US Bytes: 2734 On 8/1/24 04:06, Mark Summerfield wrote: > This program segfaults at the commented line: > > #include > #include > > void uppercase_ascii(char *s) { > while (*s) { > *s = toupper(*s); // SEGFAULT > s++; > } > } > > int main() { > char* text = "this is a test"; "In translation phase 7, a byte or code of value zero is appended to each multibyte character sequence that results from a string literal or literals. 89) The multibyte character sequence is then used to initialize an array of static storage duration and length just sufficient to contain the sequence. ..." (6.4.5p6) "... If the program attempts to modify such an array, the behavior is undefined." (6.4.5p7). This gives implementation the freedom,for instance, to store that array in read-only memory, though they don't have to do so. The segfault you got suggests that the implementation you're using did so. On other platforms, writes to read-only memory might be silently ignored. On a platform where it is possible to write to such memory, the implementation is still free to optimize the code on the assumption that you won't. That could produce bizarrely unexpected behavior if you actually do modify it. What you want to do is initialize an array with the static literal: char text[] = "this is a test"; Nominally, such an array is initialized by copying from the string literal's array. However, there's no way for strictly conforming code to determine whether or not there are two such arrays. If the "text" array has static storage duration, the string literal's array is likely to be optimized away.