Czesc,
Mam takie pytanko odnosnie tablic przechowujących stałe dosłowne typu C-string. Adres takiej tablicy można przypisać do wskaźnika na const char*. Ale dlaczego nie można zmieniać jakiegoś elementu tej tablicy nawet za pomocą rzutowania const_cast ? O np. tak:
const char* ptr = "Jakis sobie C-string";const_castchar*(ptr+1)) = '#'; [/code]
bo nastąpi segfault (Access violation - czyli naruszenie segmentacji pamięci). A przecież takie obiekty statyczne jak np. te tablice (bo C-stringi mają automatycznie przydomek 'static') przechowuje się w obszarze zwanym Data area (jest on w Data segmencie):
[quote="Wikipedia"]
The Data area contains global and static variables used by the program that are not initialized to zero. This segment can be further classified into initialized read-only area and initialized read-write area. For instance the string defined by char s[] = "hello world"; in C and a C statement like int debug=1; outside the main would be stored in initialized read-write area. And a C statement like char *string = "hello world"; makes the string literal "hello world" to be stored in initialized read-only area and the character pointer variable string in initialized read-write area.
[/quote]
:arrow: http://en.wikipedia.org/wiki/Data_segment
I dlaczego następuje segfault ? Program niby narusza pamięć, ale ten obszar pamięci [u]należy do niego samego[/u], a nie do innego programu :!: ...