The above code will cause problems.
The first instance is known as static row allocation and definition. For ordinary type variables int, etc. And non-string data types such an announcement will allocate data on the stack. When strings are initialized using string literals (i.e.:), "stack"it is allocated in the read-only part of the memory.
The string itself should not be changed, as it will be stored in the read-only part of the memory. The pointer itself can be changed to indicate a new location.
t
char strGlobal[10] = "Global";
int main(void) {
char* str = "Stack";
char* st2 = "NewStack";
str = str2;
strcpy(str, str2);
}
, const, :
const char* str = "Stack";
, , . . - free().
, . , , .
char str[] = "Stack";
:
Example: Allocation Type: Read/Write: Storage Location:
================================================================================
const char* str = "Stack"; Static Read-only Code segment
char* str = "Stack"; Static Read-only Code segment
char* str = malloc(...); Dynamic Read-write Heap
char str[] = "Stack"; Static Read-write Stack
char strGlobal[10] = "Global"; Static Read-write Data Segment (R/W)
, . , .