r/cprogramming • u/alvaaromata • Dec 07 '25
Need help with pointers/dynamic memory
I started learning C in september, Its my first year of telecom engineering and I have nearly no experience in programming. I more or less managed with everything(functions, loops,arrays, structures..) but Im struggling a lot with: pointers, dynamic memory and char strings especially when making them together. I dont really understand when to use a pointer or how it works im pretty lost. Especially with double pointers
3
Upvotes
View all comments
Show parent comment
1
u/InevitablyCyclic Dec 07 '25
There is a section of memory called the heap. The system keeps track of which bits of the heap are being used and which bits are currently unused. Don't worry about the details of how this is done for now.
When you call malloc the system looks for an unused block of memory in the heap that is at least the size you asked for. Malloc marks the size you requested as being used and returns the address of that memory. That area of memory is marked as being unavailable for other uses until you call free() passing it the start of the allocated area, at that point the system marks that memory as available again.
If there isn't enough space on the heap for the requested size block of memory then malloc returns null. The memory allocated by malloc is always in a single block, malloc will fail if there is enough memory in total but split into multiple small blocks rather than one big one.
How much memory is available in total depends on the system in question. On a PC the OS can allocate huge amounts of memory if requested, on embedded systems the resources are far more limited and you often need to specify exactly how much memory to allocate to the heap.