Question

In: Computer Science

What are the different stages of memory in a heap?

What are the different stages of memory in a heap?

Solutions

Expert Solution

The heap is a region of your computer's memory that is not managed automatically for you, and is not as tightly managed by the CPU. It is a more free-floating region of memory (and is larger). To allocate memory on the heap, you must use malloc() or calloc(), which are built-in C functions. Once you have allocated memory on the heap, you are responsible for using free() to deallocate that memory once you don't need it any more. If you fail to do this, your program will have what is known as a memory leak. That is, memory on the heap will still be set aside (and won't be available to other processes). As we will see in the debugging section, there is a tool called valgrind that can help you detect memory leaks.

Unlike the stack, the heap does not have size restrictions on variable size (apart from the obvious physical limitations of your computer). Heap memory is slightly slower to be read from and written to, because one has to use pointers to access memory on the heap. We will talk about pointers shortly.

Unlike the stack, variables created on the heap are accessible by any function, anywhere in your program. Heap variables are essentially global in scope.

When should you use the heap, and when should you use the stack? If you need to allocate a large block of memory (e.g. a large array, or a big struct), and you need to keep that variable around a long time (like a global), then you should allocate it on the heap. If you are dealing with relatively small variables that only need to persist as long as the function using them is alive, then you should use the stack, it's easier and faster. If you need variables like arrays and structs that can change size dynamically (e.g. arrays that can grow or shrink as needed) then you will likely need to allocate them on the heap, and use dynamic memory allocation functions like malloc(), calloc(), realloc() and free() to manage that memory "by hand". We will talk about dynamically allocated data structures after we talk about pointers.

ge memory (you're in charge of allocating and freeing variables)

  • variables can be resized using realloc()


Related Solutions

C++ Memory Allocation: 1) Write a C++ program that allocates static, stack, & heap memory. Your...
C++ Memory Allocation: 1) Write a C++ program that allocates static, stack, & heap memory. Your program does not need to do anything else.  Indicate via comments where memory for at least one variable in each memory area is allocated. a) Write code that allocates static memory (only include one declaration): b) Write code that allocates stack memory (only include one declaration): c) Write code that allocates heap memory (only include one declaration): 2) Edit the C++ program below to include...
In CP/M why are the stack and the heap on opposite ends of memory? Why is...
In CP/M why are the stack and the heap on opposite ends of memory? Why is the OS at the top of upper memory?
what are the three main stages of memory, as well as the main processes that occur...
what are the three main stages of memory, as well as the main processes that occur within each stage. There are a number of different types of memory. how are they define and what are examples of them. What types of retrieval exist? What factors/strategies might improve the odds of successful retrieval? How can memory go wrong? What research has been done that supports the fact that these memory errors exist?
What is a heap and what are they used for
What is a heap and what are they used for
What brain areas are associated with memory consolidation and different types of memory (declarative, procedural, working,...
What brain areas are associated with memory consolidation and different types of memory (declarative, procedural, working, etc)? in detail please What changes in the brain occur with learning? in detail
What are the different stages of the Lewis two-sector model?
What are the different stages of the Lewis two-sector model?
13. Briefly describe the three-stage model of memory, and explain how the memory stages interact. Also,...
13. Briefly describe the three-stage model of memory, and explain how the memory stages interact. Also, explain how flashbulb memories demonstrate that emotions and memories are interrelated. In your opinion, how does this overlap between emotions and memories contribute to the accuracy of one’s memories?
what are the different stages of follicle development in the ovary and what happens in each
what are the different stages of follicle development in the ovary and what happens in each
Illustrate the stack and the heap allocation. Specify what each variable value holds and where different...
Illustrate the stack and the heap allocation. Specify what each variable value holds and where different references are pointing to. char[] class = {'C','O','M','P','1','2','2'"}; #define int n = 4; long long fibb(long long a, long long b, int n) { return (--n>0)?(fibb(b, a+b, n)):(a); } int main() { fib(3); //illustrate what memory looks like at this point return 0; }
What are the challenges related to the migration of women at different stages of migration?
What are the challenges related to the migration of women at different stages of migration?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT