yes, indeed there is a difference! when you use malloc
, the block of memory that you are trying to allocate gets allocated in a region of memory called the heap. freeing memory allocated on the heap has to be done manually (by calling free
).
when you declare an array of a certain size, depending on where you declare your array, the memory for it may get allocated on the stack (e.g., if it's declared as a local variable — inside of a function) or on a data segment (e.g., if it's declared as a global variable).
memory allocated on the stack gets freed automatically as your array (or more generally, local variable) gets out of scope. so you don't have to worry about manually freeing your memory.