answersLogoWhite

0


Best Answer

malloc

User Avatar

Wiki User

11y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What function allocate n bytes in heap memory?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

What is heap -short?

The microprocessor architecture divides the memory into distinct areas. Heap is one of them. This is where you can statically/dynamically allocate memory.


What function is used to release the memory on heap?

The free() function releases memory obtained with the malloc() family of functions. The delete and delete [] operators release memory obtained with the new operator. Note: realloc() can allocate and free memories as well as reallocate.


What is it called when a program allocates more and more memory but doesn't use the memory it allocates?

You are probably referring to a resource leak, however a resource leak specifically relates to programs that allocate memory on the heap (the free store) but fail to keep track of it. If we lose track of a heap allocation, the programmer cannot release that memory back to the system until the program terminates. The following is an example of a resource leak: void f (unsigned x) { void* p = malloc (x); // allocate x bytes // ... use memory } Each time we call this function we allocate another s bytes on the heap. But the function never releases that memory and because p is local to the function, we have no way of knowing where that memory was allocated (p will fall from scope when we return from the function). If we call this function often enough, we will eventually run out of memory. To fix the problem, we must release the memory as soon as we are finished with it: void f (unsigned x) { void* p = malloc (x); // allocate x bytes // ... use memory free (p); // release the memory p = NULL; // zero the pointer } It is not necessary to zero a pointer when the pointer will fall from scope anyway, but it is considered good programming practice. What is important is that we release the memory it refers to before it falls from scope. Keeping our scopes as narrow as possible helps us to manage our resources more easily; the larger the scope, the more easily we can inadvertently introduce resource leaks, particularly when our code is maintained by several different programmers. The problem you describe isn't necessarily a resource leak but it is clearly wasteful to allocate memory that is left unused even if we don't create a resource leak. Good programming practice dictates that we release memory as soon as we no longer require it, however allocating memory on an as-required basis can impact upon performance so it is not unusual for a programmer to allocate more memory than he needs, thus creating a reserve that can be used as and when required. Not using a reserve isn't in itself a problem, however we should endeavour to keep our reserves to a minimum and return all reserved memory to the system in a timely manner -- as soon as we no longer require it.


Static and dynamic memory allocation in c plus plus?

dynamic memory allocation is that type of memory which create to allocate the memory on running time or at compile time by the function of malloc , calloc , realloc and free. dynamic memory allocation is give the best utilization of memory which gives the sufficient use of memory.


What is a malloc function in C programming with example?

The malloc() function is part of a class of functions that deal with the allocation of memory on the heap. int *a = malloc (sizeof (int) * 100); /* allocate 100 int's */ if (a == NULL) {...} /* deal with possible malloc failure */ /* use a, either as pointer or as array of 100 ints */ free (a); /* release memory back to the library */

Related questions

What is the difference between allocating memory through heap and malloc?

Nothing, malloc does allocate memory from the heap.


What is heap -short?

The microprocessor architecture divides the memory into distinct areas. Heap is one of them. This is where you can statically/dynamically allocate memory.


What function is used to release the memory on heap?

The free() function releases memory obtained with the malloc() family of functions. The delete and delete [] operators release memory obtained with the new operator. Note: realloc() can allocate and free memories as well as reallocate.


What is the difference between the stack and the heap?

The stack is a local memory reserve where the program stores variable and function data. The heap is a special memory reserve where the programmer can dynamically allocate memory from. The heap is useful when the programmer doesn't know how large to make certain variables, he just constructs one of the right size during run-time.


What is it called when a program allocates more and more memory but doesn't use the memory it allocates?

You are probably referring to a resource leak, however a resource leak specifically relates to programs that allocate memory on the heap (the free store) but fail to keep track of it. If we lose track of a heap allocation, the programmer cannot release that memory back to the system until the program terminates. The following is an example of a resource leak: void f (unsigned x) { void* p = malloc (x); // allocate x bytes // ... use memory } Each time we call this function we allocate another s bytes on the heap. But the function never releases that memory and because p is local to the function, we have no way of knowing where that memory was allocated (p will fall from scope when we return from the function). If we call this function often enough, we will eventually run out of memory. To fix the problem, we must release the memory as soon as we are finished with it: void f (unsigned x) { void* p = malloc (x); // allocate x bytes // ... use memory free (p); // release the memory p = NULL; // zero the pointer } It is not necessary to zero a pointer when the pointer will fall from scope anyway, but it is considered good programming practice. What is important is that we release the memory it refers to before it falls from scope. Keeping our scopes as narrow as possible helps us to manage our resources more easily; the larger the scope, the more easily we can inadvertently introduce resource leaks, particularly when our code is maintained by several different programmers. The problem you describe isn't necessarily a resource leak but it is clearly wasteful to allocate memory that is left unused even if we don't create a resource leak. Good programming practice dictates that we release memory as soon as we no longer require it, however allocating memory on an as-required basis can impact upon performance so it is not unusual for a programmer to allocate more memory than he needs, thus creating a reserve that can be used as and when required. Not using a reserve isn't in itself a problem, however we should endeavour to keep our reserves to a minimum and return all reserved memory to the system in a timely manner -- as soon as we no longer require it.


Static and dynamic memory allocation in c plus plus?

dynamic memory allocation is that type of memory which create to allocate the memory on running time or at compile time by the function of malloc , calloc , realloc and free. dynamic memory allocation is give the best utilization of memory which gives the sufficient use of memory.


What is a malloc function in C programming with example?

The malloc() function is part of a class of functions that deal with the allocation of memory on the heap. int *a = malloc (sizeof (int) * 100); /* allocate 100 int's */ if (a == NULL) {...} /* deal with possible malloc failure */ /* use a, either as pointer or as array of 100 ints */ free (a); /* release memory back to the library */


What is malloc function in C?

The malloc function is a function used in C/C++ and various other high-level programming languges to add memory to the heap, it is essentially a pointer int* X = malloc(sizeof(int)); Where the parameter to malloc is the number of bytes you want to make room for. Because we are using an integral variable we want to make as much room as an integer takes.


What is meant by heap in c or cpp?

If you see the word "heap" in the context of C/C++ programming, it is probably referring to one of two ideas. First, if it is written as "the heap", it is probably referring to dynamically allocated memory. We conceptualize memory as either being on "the stack" or "the heap" in main memory. Memory allocation from the heap happens when a call to malloc (or similar functions) are called in C, or when the "new" operator is used in C++. This is in contrast to statically allocated memory, which comes from the load module and is known at compile-time, or from the "stack" which is used at run-time to allocate local scope, or automatic, memory. Another usage of the word heap is a certain data structure called a heap. It is a very common data structure for priority queues and is crucial to the famous HeapSort algorithm. You can easily find more information on this data structure e.g. by searching for HeapSort.


Why it is required to create mainframe object on the heap?

You need to create the mainframe object on the heap so that the object does not go out of scope and get automatically deleted when your function exits. You could create it on the stack if the function doing that does not exit until everything is deleted, such as in the main loop of CWinApp. There are also issues with available memory in stack versus heap, as the stack size is preallocated while the heap can grow to the size of available memory.


What are stored in heap memory?

Objects are stored in heap.


Where is Heap located?

A Heap data structure can be located in the computer's memory, either on the stack or heap memory. The Heap memory is a region of a computer's memory space where dynamic memory allocation happens during a program's execution. It is used to store global variables, function pointers, and objects created using dynamic memory allocation.