473,387 Members | 1,517 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,387 software developers and data experts.

The Heap?

Markus
6,050 Expert 4TB
Simple question.

In the book I previously read on C, it didn't go into what the heap was. In some lectures I'm watching currently, the guy talks about the heap but doesn't explain what it is (maybe he's gone over this in a previous lecture).

Do you know of any resources I could use (online, preferably) to understand the memory management, heap, etc. in C?

Thanks.
Oct 25 '09 #1
7 2904
Markus
6,050 Expert 4TB
Furthermore, what happens with the following 2 examples (are both stored on the heap, or..)?

Expand|Select|Wrap|Line Numbers
  1. int *arr = (int *)malloc(4 * sizeof(int)); // On the heap?
  2. int arr[4]; // Or is this on the heap?
  3. // Or both?
  4.  
Mark.

P.S. I apologise for the newbie questions. Maybe I should take back my previous pseudonym of markusn00b?

P.P.S. I apologise for having to apologise.

P.P.P.S Ad infinitum..
Oct 25 '09 #2
Banfa
9,065 Expert Mod 8TB
Most C and C++ implementations rely on both a heap and a stack. However you may not find it in a book on the subject because neither are mandated by the standard, how an implementation organises its own memory is implementation defined.

malloc, realloc, calloc and new all allocate from the local heap. The local heap is the memory in the heap available to the program as opposed to the system heap, the memory available to the entire system. In an embedded program the program heap and system heap are often the same but for Windows and Linux they are very different, the program rarely as access to the entire memory of the system under Windows or Linux. In the days of 16 bit Windows the size of the program heap had to be set in the linker.

Variables with automatic scope (look up the auto keyword) are allocated from the stack (if the implementation has one). The stack is another area of memory the size of the used stack grows and shrinks as function calls are made. Each time a function call is made, the current state of the processor and the return address are stored to the stack space is also reserved on the stack for any auto variables in the function just called, that is variables local to the function.

Data declared at file scope or data declared at function scope with the static keyword are stored in the data segment. This is a section of data that the C start-up code allocates (if required) and initialises. These variables are normally split into 2 sections, those with a non-zero initial value and those with a zero initial value or non initial value (which automatically get assign zero). The later section needs to have their initial values stored in the program code so that c start-up can initialise them properly, those in the latter section can just be zeroed programmatically.

As to your code, truely I should ask if that code is inside or outside a function, however since you have called malloc it must be inside a function.

Therefore the first declaration allocates an int pointer on the stack, then calls malloc to allocate data for 4 ints on the heap and stores a pointer to it in the int pointer.

The second declaration allocates an array of 4 ints on the stack.

It would normally be considered bad practice to allocate large objects (more than a few hundred bytes) on the stack. For a recursive function allocating more than a few bytes on the stack is likely to be a bad idea. However it does rather depend on how your implementation organises its memory.
Oct 25 '09 #3
Markus
6,050 Expert 4TB
Thanks. That's a great explanation.

Mark.
Oct 26 '09 #4
myusernotyours
188 100+
Banfa, what about if his code was under main? Is main treated just like any other function (the first) and the automatic variables allocated on the stack?

Incidentally just today I was trying to find out the entire memory available on a programs heap on my windows (with g++ on mingw) and I could allocate an array of char[125000] 17029 times and failing on the 17030th array. That's almost 2gb. My laptop (on which I was doing this) has 3gb of RAM. So it seems like the heap is much more than you would expect. This apparently has something to do with how windows allocates memory that is 'really not there' physically. I have no idea whether it does this for the stack also.
Finally, for my 2gb allocation, the taskman showed memory usage of under 150000KB I wonder why.

Regards,

Alex
Oct 26 '09 #5
Banfa
9,065 Expert Mod 8TB
Yes main is like any other function in this respect.

I have worked with embedded micro-controllers whose compilers had a switch to make all function local variables default to static allocation rather than auto allocation (effectively taking them off the stack an putting the onto the heap.

On (32bit) WIndows any program can have up to 2Gbyte of allocated memory with another 2Gbyte reserved for the system to use on behalf of the program.

That 2Gbyte can be made up using heap, stack or statically allocated data. On Windows both the heap and stack of a program is expandable to accommodate what the program allocates (up to the 2Gbyte limit).

Windows (and Linux) can supplement physical memory with a swap file on the hard drive giving the computer more available memory that its actually physical memory.

A can not explain why taskman has the number wrong, it may in fact be displaying the physical ram being used by your program rather than the amount of memory it has allocated. Try Process Explorer from SysInternals (google it).
Oct 26 '09 #6
myusernotyours
188 100+
You are right. Taskman only shows the 'Physical memory' procexp calls it 'Working memory'. Actually I had tried procexp too and found this 'Working memory' to be the same as what taskman reported, but when you mentioned it, I checked it again and realised I had to enable a column that shows the virtual memory size (called just 'virtual size' by the program) that shows the entire 2gb allocated to the process. I had read about this 2gb-program and 2gb-OS thing but it wasn't very clear to me. Now I think it is! I suspect on machines that support 64GB virtual memory I could allocate almost half of that right?

Regards,

Alex.
Oct 26 '09 #7
Banfa
9,065 Expert Mod 8TB
In 64 bit windows 32 bit applications are allocated 4Gbyte of address space by the OS, however they need to be compiled with the /LARGEADDRESSAWARE to be able to access >2Gbyte of address space.

64bit applications get 8Tbyte of address space, with 8Tbyte reserved for the OS.

Having the address space does not mean you can allocate the memory, you can only allocate the memory if you have the address space and the RAM (or swap file ram).

I am not quite sure where you got the 64Gbyte limit from.

http://support.microsoft.com/kb/294418
Oct 26 '09 #8

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

Similar topics

0
by: ANt | last post by:
Hi, we have some major GC issues at present with a system we're trying to put live. It's a live calculation engine that's distributed across about 30 Java server processes. A set of processes...
14
by: Kevin Grigorenko | last post by:
Hello, I couldn't find an obvious answer to this in the FAQ. My basic question, is: Is there any difference in allocating on the heap versus the stack? If heap or stack implementation is not...
17
by: Jonas Rundberg | last post by:
Hi I just started with c++ and I'm a little bit confused where stuff go... Assume we have a class: class test { private: int arr; };
2
by: mordac | last post by:
Hello, I was wondering if I could get some opinions on how best to handle printing in a max heap data structure. Right now my heap struct looks as thus: typedef struct heapStruct { int*...
9
by: shine | last post by:
what is the difference between a heap and a stack?
16
by: sarathy | last post by:
Hi all, I need a few clarifications regarding memory allocaion in C++. I apologize for the lengthy explanation. 1. In C++, Objects are allocated in heap. What does heap refer to? Is it an area...
10
by: Woody Ling | last post by:
In 32 bits DB2 environment, is it meaningful to set sheapthres larger than 256MB for the following case.. 1. Intra-parallel is ON 2. Intra-parallel is OFF
0
by: JosAH | last post by:
Greetings, I was asked to write a Tip Of the Week; so here goes: a lot of topics are started here in this forum (and a lot of other forums too) mentioning a problem about sorting data. ...
5
by: kumarmdb2 | last post by:
Hi guys, For last few days we are getting out of private memory error. We have a development environment. We tried to figure out the problem but we believe that it might be related to the OS...
4
by: ggoubb | last post by:
The purpose of the Insert function is to add a new integer in the Heap assuming that it is not already full. If Heap capacity has been reached, it attempts to double the current capacity. If capacity...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.