I started my adventure with C++ one week back. I have read a lot about C++.
I was experimenting with the following:
char * String1 = "abcdefgh";
I, then, tried to modify its value in the following way:
String1[2] = 'f';
This resulted in an UNHANDLED EXCEPTION.
But the following results in proper execution:
char String2[9]="abcdefgh";
String2[7]='s';
I tried to extract information about the binary generated using above code using DUMPBIN.
DUMPBIN is a Visual Studio Tool. I used the /ALL option to extract every information contained in the binary.
I could see two instances of “abcdefgh” in the RAWDATA section. And I understand why.
My questions are as follows:
1) Although both String1 and String2 are essentially pointers to two different instances of the same character sequence, why is the String1 manipulation not a legal one?
2) I know the compiler generates a SYMBOL TABLE for mapping variable names and their values. IS there any tool to visualize the SYMBOL TABLE in Windows OS?
3) If I have an array of integers instead of the character sequence, can it be found in the RAWDATA?
I could also see the following in RAWDATA:
Unknown Runtime Check Error.........
Stack memory around _alloca was corrupted.......
....A local variable was used before it was initialized.........
....Stack memory was corrupted..
........A cast to a smaller data type has caused a loss of data.
If this was intentional, you should mask the source of the cast with the appropriate bitmask.
How do these things get into the binary executable? What is the purpose of having these messages in the binary(which obviously is not readable)?
EDIT:
My question 1) has a word INSTANCES, which is used to mean the following:
The character sequence “abcdefgh” is derived from a set of non-capitalized ENGLISH ALPHABETS, i.e., {a,b,…,y,z}. This sequence is INSTANCIATED twice and stored at two memory locations, say A and B. String1, points to A(assumption) and String2 points to B. There is no conceptual mix-up in the question.
What I wanted to comprehend was the difference in the attributes of the memory locations A and B, i.e., why one of them was immutable.
This allocates a char array, and initializes it with the values {‘f’, ‘o’, ‘o’, ‘\0’}. You get “your own” storage for the chars, and you can modify the array.
This allocates a pointer, and sets the value of that pointer to the address of a char array which contains {‘f’, ‘o’, ‘o’, ‘\0’}. The pointer is yours to do with as you wish, but the char array is not. In fact, the type of the array is not
char[], butconst char[], andstrptrreally ought to be declared asconst char*so that you do not mistakenly attempt to modify the const array.In the first case,
"foo"is an array initializer. In the second,"foo"is a string literal.More specific details about exactly where the memory for each situation is located tend to be unspecified by the standard. However, generally speaking,
char string[] = "foo"allocates achararray on the stack,char strptr* = "foo"allocates acharpointer on the stack and (statically) allocates aconst chararray in the data section of the executable.