char * p_one = "this is my first char pointer";
char * p_two= "this is second";
strcpy(p_one ,p_two);
consider the above code. This is giving access violation error.
So please help to understand
- where is the current
"this is my first char pointer"string stored in memory? heap or stack - why I need to allocate memory for p_one before call
strcpy, even it’s already storing the first string. why"this is second"string cannot copy to same location? - If I allocate memory for p_one before call
strcpythen what happen to"this is my first char pointer"string that was pointed by p_one ? is it keep in memory? - How
strcpyknows specific pointer have allocated memory or not?
First off your compiler should be warning that the p_one and p_two are actually
const char *because the compiler allocates the storage of this string at compile time.The reason you cannot modify them is because in theory you could overwrite memory after them, this is what causes hack attack with a stackoverflow.
Also the compiler could be smart and realize that you you use this string in 10 places but notices it is the same, so modifying from one place changes it – but that destroys the logic of the other 9 places that uses it