char *str3 = new char[SIZE]; str3 = "Amygdala"; delete[] str3; // Здесь ошибка Please explain what is wrong.
char *str3 = new char[SIZE]; str3 = "Amygdala"; delete[] str3; // Здесь ошибка Please explain what is wrong.
What's happening?
char *str3 = new char[SIZE]; You allocate memory and assign a pointer to this block to the variable str3
str3 = "Amygdala"; You assign the pointer to a constant string to the variable str3
delete[] str3; You are trying to delete a pointer to a constant string.
What do we have to do? It is necessary after the allocation of memory to copy there the necessary content. This can be a function
strcpy(str3, "Amygdala") or similar.
And, by the way, you need to compare strings also by content.
strcpy(str3, "Amygdala"); bool test1 = str3 == "Amygdala"; bool test2 = strcmp(str3, "Amygdala") == 0; test1 will be false, and test2 true
Source: https://ru.stackoverflow.com/questions/776709/
All Articles
new,delete,delete[]. - LLENNnewnordelete. 2) the assignment overwrites the pointer and you try to delete the constant string, not the allocated memory.strcpy(str3, "Amygdala")would be right - Fat-Zer