I have a simple question which I don't seem to be able to find an answer to anywhere.
Why does this code compile and work when i allocated enough memory for just two characters, "he", "hellos" shouldnt be able to fit?
It prints out correct length, which is 6.
The free gives no error.
char* testF() {
char *arr = (char*)malloc(2*sizeof(char));
strcpy(arr, "hellos");
return arr;
}
int main() {
char *arr = testF();
printf("%c%c%c%c%c%c\n", arr[0], arr[1], arr[2], arr[3], arr[4], arr[5]);
printf("Length = %d", strlen(arr));
free(arr);
return 0;
}