What’s the malloc for?

Asked

Viewed 81 times

0

I’m new to C++ so I have some questions. One of them I’ve seen in several codes around is the malloc function. What’s the use of malloc?

1 answer

3


First of all malloc is a function introduced in the language C, it serves for dynamic memory allocation in the region known as memory heap, a continuous region of spaces in the main memory that the operating system makes available for your program to use. Unlike static allocation and automatic allocation, in which the management of memory allocated by variables is done by the compiler along with Linker, more specifically these utilities control when the variable should be created - allocated from memory, and destroyed - out of focus memory; dynamic allocation, made by malloc in C, it is the responsibility of the programmer, being this space provided by the OS, but managed by the programmer, managing when this space should be displaced or changed of state.

In C++ the operator new, as in other languages, is the equivalent of malloc of the C.

PS: Some languages have a "garbage collector", where the programmer does not need to worry about dynamic allocation management.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.