How and when do i use malloc? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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 Answer
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
17th Apr 2024, 7:21 PM
`нттp⁴⁰⁶
`нттp⁴⁰⁶ - avatar