473,385 Members | 1,907 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Memory is added to the heap or taken from the heap?

We say that in a C process' memory layout,the heap segment deals with the dynamic memory allocation i.e. malloc,calloc.

When we do
Expand|Select|Wrap|Line Numbers
  1. int *p=malloc(100000);
100000 bytes are :
1.taken from the system memory and whole 100000 bytes added into the process' heap memory segment

2.taken from the process' heap memory(process' heap already had this memory,it has turned accessible now )

3.taken from the system memory and address to that memory is added to the heap segment of the process.So p actually points to 100000 bytes in the system memory.
????
Apr 5 '11 #1
4 1518
weaknessforcats
9,208 Expert Mod 8TB
C does not require a heap or a stack. Heaps and stacks are implementation defined so they vary by operating system.

Usually a process is allowed a ration of memory while it's running. This is the process heap and it is allocated from system memory by OS when the process is created. From this the CRT (C-run-time) heap may be allocated (depends).

Windows programmers often create a private heap and use Windows allocators to allocate from that heap and then delete the heap when they are finished. This avoids malloc/free and all the leaks.

Memory management is a big deal and you need to read specfically for your situation.

Fortunately, you can view the heap as memory you control so that you can store stuff and be sure it doesn't get away from you until you are done with it
Apr 5 '11 #2
Ohk,let me put it this way,we say that the "heap grows". What exactly is meant by that?
**If, on doing malloc(), memory is taken from the heap then why do we say that the heap grows?
Apr 6 '11 #3
donbock
2,426 Expert 2GB
Personally, I would not say "the heap grows".
Where are you seeing that phrase? In what context?
Apr 6 '11 #4
Banfa
9,065 Expert Mod 8TB
The process heap can grow.

While a process is running it memory gets allocated from its heap. That doesn't mean that the processes heap gets smaller when the memory is allocated just that there is less free space in the heap.

On a number of modern operating systems (Windows and Linux to name 2) if a process runs out of heap space then rather than fail at the next allocation it can increase the amount of memory available on its heap by requesting additional memory from the system to assign to its heap.

In this sense a programs heap can grow, that is the amount of memory allocated to the processes heap can increase during the lifetime of the process.

Note that this is a function of the operating system as mentioned C does not even require that there be a heap and in, for example, an embedded environment the processes heap is fixed at link time because normally in an embedded environment RAM is a scarce resource.
Apr 6 '11 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

10
by: Amit | last post by:
char *str1="amit"; char str2="mehta" strcat(str1,str2); It will crash, I feel str1 will be stored in code section. Once memory is allocated, you cannot change or append into this. Please...
16
by: JCauble | last post by:
We have a large Asp.net application that is currently crashing our production servers. What we are seeing is the aspnet_wp eat up a bunch of memory and then stop unexpectedly. Does not recycle. ...
6
by: One Handed Man [ OHM# ] | last post by:
I want to declare the following Dim BoolGrid(511,511) as Boolean and Dim Bitgrid As New BitArray(511*511) How can I find out the size of memory allocated on the heap for each one ?
37
by: yogpjosh | last post by:
Hello All, I was asked a question in an interview.. Its related to dynamically allocated and deallocated memory. eg. //start char * p = new char; ...
1
by: rajesh6695 | last post by:
Hai I want to know wheather the user defined function local variables are stored in the stack or in heap.... For example i have a function add() int add(int i, int j) { int m ,n;
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.