How and when do i use malloc? | Sololearn: Learn to code for FREE!
+ 1

How and when do i use malloc?

I understand the concept behind malloc but i dont understand when to use it

15th May 2020, 6:17 PM
Guido Parlatore
Guido Parlatore - avatar
1 Antwort
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:
17th Apr 2024, 7:21 PM
`đŻĐéĐéßőĘÔü┤Ôü░Ôü - avatar