Malloc -> how much memory has been allocated?

后端 未结 7 1282
梦谈多话
梦谈多话 2020-12-20 18:44
# include 
# include 
# include 
# include 

int main ()
{
  char * buffer;
  buffer = malloc (2);

          


        
7条回答
  •  不思量自难忘°
    2020-12-20 19:15

    The compiler doesn't know. This is the joy and terror of C. malloc belongs to the runtime. All the compilers knows is that you have told it that it returns a void*, it has no idea how much, or how much strcpy is going to copy.

    Tools like valgrind detect some of these errors. Other programming languages make it harder to shoot yourself in the foot. Not C.

提交回复
热议问题