The main selection of the string line c-style

I am working on a project with existing code that uses mostly C ++, but with c-style strings. Take the following:

#include <iostream> int main(int argc, char *argv[]) { char* myString = "this is a test"; myString = "this is a very very very very very very very very very very very long string"; cout << myString << endl; return 0; } 

This compiles and works fine, with the output being a long line.

However, I do not understand WHY this works. I understand that

 char* myString 

- A pointer to a region of memory large enough to hold the string literal "this is a test." If so, how can I store a longer string in the same place? I expected this to work when you do this, due to an attempt to cut the long string into the space allocated for the shorter one.

Obviously, there is a basic misunderstanding of what is happening here, so I appreciate any help in understanding this.

+4
source share
6 answers

You do not change the contents of the memory, you change the value of the pointer to another area of ​​memory that contains "this is a very very very very very very very very very very very long string" .

Note that char* myString allocates enough bytes for the pointer (usually 4 or 8 bytes). When you do char* myString = "this is a test"; , what actually happened was that before your program even started, the compiler allocated space in the executable image and placed "this is a test" in this memory. Then when you do char* myString = "this is a test"; , what it actually does is simply allocate enough bytes for the pointer and make the pointer a point for the memory that it already allocated at compile time in the executable file.

So, if you like charts:

 char* myString = "this is a test"; (allocate memory for myString) ---> "this is a test" / myString--- "this is a very very very very very very very very very very very long string" 

Then

 myString = "this is a very very very very very very very very very very very long string"; "this is a test" myString--- \ ---> "this is a very very very very very very very very very very very long string" 
+14
source

In memory two lines. First, "this is a test" and say that it starts at address 0x1000. The second is "this is a very very ... test" , and it starts at address 0x1200.

Across

 char* myString = "this is a test"; 

you create a variable called myString and assign it the address 0x1000. Then, <

 myString = "this is a very very ... test"; 

you assign 0x1200. By

 cout << myString << endl; 

you just print a line starting with 0x1200.

+5
source

You have two string literals of type const char[n] . They can be assigned to a variable of type char* , which is nothing more than a pointer to char . Whenever you declare a variable of type pointer-to-T, you only declare a pointer, not the memory that it points to.

The compiler reserves memory for both literals, and you just take the pointer variable and point it to these literals one by one. String literals are read-only, and their distribution will take care of the compiler. They are usually stored in an executable image in read-only protected memory. A string literal usually has a lifetime equal to the lifetime of the program itself.

Now, if you tried to change the contents of the literal, it would be UB, but you won’t. To prevent attempts to modify errors, you would be wise to declare your variable as const char* .

+2
source

During program execution, a memory block containing “this is a test” is allocated, and the address of the first character in this memory block is assigned to the myString variable. The next line contains a separate memory block containing "this is very ...", and the address of the first character in this memory block is now assigned to the variable myString, replacing the address that he used to save the new address in the line "very very long".

just to illustrate, say, the first block of memory looks like this:

[t] [h] [i] [s] [] [i] [s] [] [a] [] [t] [e] [s] [t] and just say that the address of this first character is' t 'in this sequence / array of characters is 0x100. therefore, after the first assignment of the variable myString, the variable myString contains the address 0x100, which points to the first letter "this is a test."

then in a completely different memory block there is:

[t] [h] [i] [s] [] [i] [s] [] [a] [] [v] [e] [r] [r] [y] ... and just say that the address of this first character 't' is 0x200. therefore, after the second assignment to the myString variable, the myString NOW variable contains the address 0x200, which points to the first letter "this is very very ...".

Since myString is just a pointer to a character (hence: "char *" is a type), it only stores the address of the character; he does not care about how large the array is, he does not even know that it points to the "array", only that it stores the address of the character ...

for example, you can legally do this:

  char myChar = 'C'; /* assign the address of the location in memory in which 'C' is stored to the myString variable. */ myString = &myChar; 

Hope this was clear enough. If yes, answer / accept the answer. If not, please comment so I can clarify.

+2
source
String literals

do not require allocation - they are saved as is and can be used directly. Essentially, myString was a pointer to one string literal and was changed to point to another string literal.

+1
source

char* means a pointer to a block of memory that contains a character.

C-style string functions get a pointer to the beginning of a string. They assume there is a sequence of characters that end with a null character (\ n).

So, what does it mean <the operator actually makes a loop from this first character position until it finds a null character.

0
source

Source: https://habr.com/ru/post/1381373/


All Articles