r/cprogramming • u/SubstantialCase3062 • Jan 07 '26
Here is my super simple use of the malloc() func, question why can't just let the compiler do the work?
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(void){
int *pArarry;
pArarry = malloc(5 * sizeof(*pArarry));
*pArarry = 5;
pArarry[1] = 10;
pArarry[2] = 15;
pArarry[3] = 20;
pArarry[4] = 25;
for(int i = 0; i < 5; ++i){
printf("%d\n", *(pArarry + i));
}
return 0;
}
0
Upvotes
8
u/[deleted] Jan 07 '26
> why can't just let the compiler do the work?
Do what work?
2
u/raxuti333 Jan 07 '26
You can stack allocate the array by typing "int pArray[5]". Then you don't need to call malloc. Stack allocations are also "freed" automatically when the function returns.
Also in the for loop you should use array indexing instead of adding i to pArray.
2
u/asgaardson Jan 07 '26
What are you trying to solve here?
2
u/Leverkaas2516 Jan 07 '26 edited Jan 07 '26
Since you use malloc for pArarray and never free it before it goes out of scope, this code has a memory leak.
In C, you only use malloc when you specifically do NOT want the compiler to do the memory management work for you using the stack. It's not so much who's doing the work, as it is the work you want done. Want to use the heap? You have to do it yourself.
Why do you use *pArarray instead of pArarray[0] ? You could also use a for loop, with pArarray[i] = (i+1) * 5.
2
u/zhivago Jan 07 '26
Perhaps you are complaing about the lack of garbage collection?
1
u/Diligent-Union-8814 Jan 07 '26
This may help https://www.geeksforgeeks.org/c/memory-layout-of-c-program/
1
u/pixel293 Jan 07 '26
Stack vs Heap
The stack is limited, when you run out of stack the program crashes. If you let C "allocate" the array then it allocates it on the stack. So if your array is too big, your program crashes. If your array is big but not to big, your program may crash depending on how many functions you call.
When you call malloc the memory is allocated in the heap, this space is really limited by the physical (and virtual ram) in the machine. So you can allocate huge amounts of memory.
Additionally if you have multiple threads, each thread has it's own stack. When you want both threads to use the same memory it's better if that memory is allocated on the heap where YOU control when it's freed. The compiler only considers the current thread when freeing the memory it allocated for you.
3
u/EddieBreeg33 Jan 07 '26
What are you asking specifically, here?
2
u/Both_Love_438 Jan 07 '26
Say you're coding a web browser with the usual tab functionality. At compile time, you don't know how many tabs the user will open, and you also can't assume some max value and reserve that much at compile time, because it would be limiting for users who may need more and memory inefficient for users that won't use that much, so every new tab they open you call malloc to allocate more memory dynamically.
1
u/[deleted] Jan 07 '26
Malloc is for dynamic allocation. If you have a variable N and want to allocate N ints, you would need malloc. If you know ahead of time that N is 5, you can declare a static array of 5 ints and the compiler will allocate this memory statically, meaning before runtime. Malloc takes from the global memory area at runtime. Static memory is allocated separately from this.
1
u/v_maria Jan 07 '26
lookup undefined behavior. just because your program compiles and runs doesn't mean it's right
1
u/Severe-Reality5546 Jan 07 '26
As others have said, you use malloc() when you don't know how much data you will need ahead of time.
Here is a real-world example: I've written software that processes raw image data from different infrared cameras. Different cameras have different resolutions and possibly a different number of bytes/pixel. So the program uses malloc() to allocate the correct amount of memory to hold the camera data.
1
u/Hot-Camp780 Jan 10 '26
Memory is stored in 2 types static and dynamic (stack and heap respectively).Malloc is used to allocate memory to the heap which is dynamic unlike stack (not the data structure) which is static. This is just useful when you can't actually determine tje size you want during compilation.
-1
u/SubstantialCase3062 Jan 07 '26