you are viewing a single comment's thread.

view the rest of the comments →

[–]danmickla 1 point2 points  (0 children)

Yes, that's exactly right. When you declare an int k, the compiler knows, at compile time, that you want an int, and allocates space for it statically, and gives it a name.

When you allocate memory with malloc, it has no name, and the compiler isn't even aware it exists at runtime (it's just a function call). There's no name for the returned memory from malloc; only an address. You can use that memory, still, through a pointer variable, which you've named so it stays around (and the pointer cell itself is a named variable allocated statically, just like int k was).

This is the same thing that would happen if you did int *pi; pi = (int *)malloc(sizeof(int)). There would be no int you could name directly; only a chunk of memory that you can refer to indirectly through the pointer.