While I was surfing for pointer tutorials,I came across an example and couldnt get it.
(This is from a question and an answer to it)
Here it is:
1 2 3 4 5 6 7 8
char *pstr = "string literal which can't be altered\n";
char astr[] = "string literal which can be altered\n";
char bstr[] = "string literal which can be altered\n";
cout << pstr << astr << bstr; // 3 strings are printed,OK
*(astr+1) = 'X'; // OK
cout << astr; // output=sXring literal which can be altered
*(pstr+1) = 'X'; // access violation
cout << pstr;
Why this is so?Is it about stack/heap topics? (which I dont know much about)
Also,I tried this:
1 2 3 4 5
char *pstr = "string literal which can't be altered\n";
char astr[] = "string literal which can be altered\n";
char bstr[] = "string literal which can be altered\n";
cout<<(void*)pstr << " " << (void*) astr << " " << (void*) bstr;
And the output is;
0X444000 0X23ff10 0X23fee0
The string that we initialized char pointer is put in a very different location on memory.(at least I think so when I see these numbers).Can you explain this?
Due to C, conversion from a string literal to a char*, while technically illegal, is allowed. However, if you attempt to modify it, you get an illegal access error.
Anyway, this is basically how it works:
1) Make a char* that points to the constant string literal in memory (no copying)
2/3) Create a new character array and *copy* the string literal in the (modifyable) memory
Well... first of all, this: char *pstr = "string literal which can't be alteredn"; is deprecated, your compiler should give you warnings about this. String literals are constant, and you are implicitly casting it to a non-constant char* here. I am not entirely sure, but I thinkchar astr[] = "string literal which can be alteredn"; actually creates a copy of the string here. Anyways: Your char* here points to a constant string here, and the result of trying to access a constant string is undefined. The more correct way to do this (I say more, because using C-strings in C++ is usually unnecessary and evil anyways), would be the following:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#include <iostream>
#include <cstring>
usingnamespace std;
int main(int argc, char** argv)
{
char* c= newchar[strlen("Can't be altered")+1];
strcpy(c,"Can be altered");
cout<<c;
*(c+1) = 'x';
cout<<c;
return 0;
}
Your compiler should give a warning here, though it might only do so if you have the Wall flag on.
I wonder if there is a read-only memory section (which is included in the part that is given to our small program), which we can NOT modify?
maybe, maybe not. Depends on your system and what code your compiler generates, but it's defined in the language standard that you aren't allowed to do that.