Why, or when, do you need to dynamically allocate memory in C?
Dynamic memory allocation is a very important topic in C programming. However, I've been unable to find a good explanation of what this enables us to do, or why it is required. Can't we just declare variables and structs and never have to use malloc()? As a side note, what is the difference between: ptr_one = (int *)malloc(sizeof(int)); and int *ptr_one = malloc(sizeof(int)); You need to use dynamic memory when: You cannot determine the maximum amount of memory to use at compile time; You want to allocate a very large object; You want to build data structures (containers) without a fixed upper