Hallo,
ich habe dazu doch noch eine Frage:
Wie kann ich mir das erklären....
#include <stdio.h>
int main(void) {
char* string = "Test";
char* p_test = string;
printf("%c\n", string[2]);
printf("%c\n", p_test[2]);
//Weder das...
string[2]='x';
//noch das funktioniert...
p_test[2]='x';
return 0;
}
...es führt beides zu einem Segmentation Fault. Wie kann ich mir das erklären? Ich dachte es wäre gleich mal ein guter Test für Valgrind:
Valgrind spuckt folgendes aus:
markus@archy ~/C-Programme $ valgrind --leak-check=full --track-origins=yes ./valgrindtest
==3453== Memcheck, a memory error detector
==3453== Copyright (C) 2002-2009, and GNU GPL'd, by Julian Seward et al.
==3453== Using Valgrind-3.5.0 and LibVEX; rerun with -h for copyright info
==3453== Command: ./valgrindtest
==3453==
s
s
==3453==
==3453== Process terminating with default action of signal 11 (SIGSEGV)
==3453== Bad permissions for mapped region at address 0x8048502
==3453== at 0x8048420: main (valgrindtest.c:12)
==3453==
==3453== HEAP SUMMARY:
==3453== in use at exit: 0 bytes in 0 blocks
==3453== total heap usage: 0 allocs, 0 frees, 0 bytes allocated
==3453==
==3453== All heap blocks were freed -- no leaks are possible
==3453==
==3453== For counts of detected and suppressed errors, rerun with: -v
==3453== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 13 from 8)
Speicherzugriffsfehler
Seit wann ist es nicht erlaubt, Zeichen in einem Array zu überschreiben?
Viele Grüße,
Markus