When we analyze program run step by step:
A) Initialization before program starts running:
A memory space on the stack is allocated and the string "Wurstbrot" + '\0' is laid on that memory space. Say, that memory allocated for string starts at AAAA:0000
; i.e. stack memory state is sth like below;
address value
AAAA:0000 W
AAAA:0001 u
AAAA:0002 r
...
AAAA:0007 o
AAAA:0008 t
AAAA:0009 0 // that's a zero
B) When program starts running:
Line 1: A pointer variable is created. This variable sits on stack and is uninitialized. For a 32 bits system, this variable sits on the stack (say, at address AAAA:0020
to AAAA:0023
and contain some garbage value like DBAC:5782
that points to some random memory.
address value
AAAA:0020 DB
AAAA:0021 AC
AAAA:0022 57
AAAA:0023 82
Line 2:
Operating system allocates a memory space (probably on the heap) with a length of 15 bytes and hands the starting value to the program (say it starts from DDDD:0000
). This value is assigned to your pointer variable and the pointer value on line 1 changes to DDDD:0000
.
address value
AAAA:0020 DD
AAAA:0021 DD
AAAA:0022 00
AAAA:0023 00
Line 3: test = "Wurstbrot";
line resets the value of the pointer to point to the memory that was allocated at step (A). From now on, your pointer variable test
do not point to the allocated memory space on the heap but the initialized memory space on the stack.
address value
AAAA:0020 AA
AAAA:0021 AA
AAAA:0022 00
AAAA:0023 00
Line 4: This line, of course, raise error. It tries to free the initialized memory space at step (A), not the memory space that was allocated at step (B)/1.
Solution:
Line 3 should read as: strcpy(test, "Wurstbrot");