What happens if I try to access memory beyond a malloc()'d region?

后端 未结 5 1119
死守一世寂寞
死守一世寂寞 2020-12-01 22:15

I\'ve allocated a chuck of memory with char* memoryChunk = malloc ( 80* sizeof(char) + 1); What is keeping me from writing into the memory location beyond 81 un

5条回答
  •  不知归路
    2020-12-01 23:18

    Carl suggests strncpy(), which is a start in the right direction. The main idea is to develop the habit of avoiding buffer overflows by adopting specific practices. A more deliberate library for this is covered in strlcpy and strlcat--Consistent, Safe, String Copy and Concatenation.

提交回复
热议问题