+ 1
How and when do i use malloc?
I understand the concept behind malloc but i dont understand when to use it
1 Respuesta
0
Using malloc in C or C++ is essential when you need to dynamically allocate memory during runtime. You should use malloc when you want to allocate memory based on user input or when the memory needed is not known at compile time. This allows you to manage memory efficiently and avoid fixed-size memory allocations that may lead to wastage or insufficient memory. For example, you can use `malloc` when creating arrays of unknown size, storing user-inputted strings, or constructing complex data structures like linked lists or binary trees.
Here is an example:
https://sololearn.com/compiler-playground/c2CyOvOU0Ul1/?ref=app