No, there's no a memory leak here.
The key thing is that you've declared and initialized an array of chars. You could have done that two ways, which are effectively the same:
First way:
char str[] = {'h', 'e', 'l', 'l', 'o', '\0'};
Here compiler knows you want an array of chars, and it figures the array length and contents via by-element initialization. So, you have an array of 6 chars.
Second way:
char str[] = "hello";
Here compiler once again knows you want an array of chars. Array of chars, this is a kind of special case, can be initialized with a string literal. It's length is 5 printed characters plus one for the string terminating NUL
. So you have an array of 6 chars.
Changing any certain element of this array doesn't lead to a memory leak, since you can still access any array element with str[i]
. You just have to make sure, you don't get out of array bounds with i
.
Try to compare to this:
int arr_int[] = {10, 20, 30, 40, 50};
arr_int[2] = 0;
Is there a memory leak in this snippet? -- No, there isn't.