Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

What is 'Heap' in microCpro for PIC?

Status
Not open for further replies.

Mithun_K_Das

Advanced Member level 3
Advanced Member level 3
Joined
Apr 24, 2010
Messages
899
Helped
24
Reputation
48
Reaction score
26
Trophy points
1,318
Location
Dhaka, Bangladesh, Bangladesh
Visit site
Activity points
8,254
In microCpro for PIC, there is an option in the edit window,
see the image below:
heap.PNG

So my question is,

a. What is this Heap for?
b. What is the benefit of this thing?
c. How to use it?
 

I guess it is related to memory. Maybe it is the option to reserve heap memory like you do using malloc() or calloc(). Maybe the Compiler can do it for you if you specify the amount of heap memory you need in the textbox.
 

a. Heap is memory put aside for use by the library routines and for dynamically allocated storage. Normally, it is the remainder of memory not used by the program variables.
b. It allows you (and library functions) to temporarily use memory then release it for other uses later. It makes better use of available memory because it can be allocated as required instead of for a specific purpose.
c. "alloc" and "malloc" let you claim a block of memory for yourself, "free" releases it for other uses. The heap is also used for storing local variables which by default are destroyed at the end of the function declaring them.

For example:
MyFunction()
{
int MyLocalVariable; // MyLocalVariable is destroyed at the end of MyFunction so use Heap memory rather than allocating it a fixed permanent address
<more code>
}

Now the memory used inside the function to hold "MyLocalVariable" is available again and can be re-used. If "MyLocalVariable" had been declared globally it would be given a permanent reserved address that could not be used for other purposes.

Brian.
 
The parameter in the Edit Project menu is to set the amount of memory which can be used for the purposes described in #3 above. This is important in a microcontroller project since the amount of memory is usually small and must be carefully controlled. Frankly, the concept of a heap in a microcontroller project may be a bit of overkill. I just think of it as memory which can be used for variables which are not static or global.
 

Thanks Brian,

I'm trying to use this option now. I'll post if there is any further difficulties happen...

- - - Updated - - -

Can I use this option for text?

Say, I've One text to use in the program; text = "TEST FOR HEAP";

I've a complete program and its working well. But I want to add this text in that program. Which is normally making an error like 'Not Enough RAM'. So, can I use this heap to solve the error? I mean using heap, can I save some RAM?
 

It might be possible, you would have to use malloc to allocate space for a variable then hold the text in that variable. The problem is that the malloc function would try to use free memeory and that's exactly what the error says you haven't got enough of!

If the text is fixed, for example messages to be displayed on an LCD, the better method is to declare them as "const" instead of a normal variable. This tells the compiler that the text will never change so it can be stored in program memory instead of RAM. For example:

char text[14] = {"TEST FOR HEAP"}; will use 14 RAM locations, one for each character and one for the terminator.

const char text[14] = {"TEST FOR HEAP"}; will use 14 ROM (program space) memory instead, leaving the RAM free.

Be careful though, when declared 'const' and stored in program memory, the text can be used as normal but you can't write to it so it can't be altered from within the program.

Brian.
 

Still I'm trying to use this function. But not clear to me yet. I think, it will be clear to me soon. Thanks all. I'll post if I fetch any problems again.

specially thank to Brian for your nice explanation.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top