Liste des Groupes | Revenir à cl c |
On 2024-08-01, Mark Summerfield <mark@qtrac.eu> wrote:So is the text here:This program segfaults at the commented line:The "this is a test" object is a literal. It is part of the program's image.
>
#include <ctype.h>
#include <stdio.h>
>
void uppercase_ascii(char *s) {
while (*s) {
*s = toupper(*s); // SEGFAULT
s++;
}
}
>
int main() {
char* text = "this is a test";
When you try to change it, you're making your program self-modifying.
Does it really do that? That's the method I've used for read-only strings, to put them into the code-segment (since I neglected to support a dedicated read-only data section, and it's too much work now).Program received signal SIGSEGV, Segmentation fault.On Linux, the string literals of a C executable are located together
0x000055555555516e in uppercase_ascii (s=0x555555556004 "this is a test")
at inplace.c:6
6 *s = toupper(*s);
with the program text. They are interspersed among the machine
instructions which reference them. The program text is mapped
read-only, so an attempted modification is an access violation trapped
by the OS, turned into a SIGSEGV signal.
Les messages affichés proviennent d'usenet.