What is heap and stack?http://www.maxi-pedia.com/what+is+heap+and+stack
What is stack?
The two sections other from the code segment in the memory are used for data. The stack is the section of memory that is allocated for automatic variables within functions.
Data is stored in stack using the Last In First Out (LIFO) method. This means that storage in the memory is allocated and deallocated at only one end of the memory called the top of the stack. Stack is a section of memory and its associated registers that is used for temporary storage of information in which the most recently stored item is the first to be retrieved.
What is heap?
On the other hand, heap is an area of memory used for dynamic memory allocation. Blocks of memory are allocated and freed in this case in an arbitrary order. The pattern of allocation and size of blocks is not known until run time. Heap is usually being used by a program for many different purposes.
The stack is much faster than the heap but also smaller and more expensive.
difference between data and program memory
Data memory = where you place your variables. You can read and write values.
Program memory = where the application is stored. Some chips allows parts of the program memory to be modified in blocks (segments), but you can't store variables in the program memory. It is normally possible to store constants - i.e. initialized variables that you do not change - in the program memory.
Your PC also has data memory and program memory. But the program memory is very small in the PC - it is just for storage of the BIOS - the boot messages you see when the PC boots, and (often, but not always) the configiguration pages where you define if you have a floppy installed, if the computer should support a USB keyboard etc.
Some chips allows parts of the program memory to be modified in blocks (segments), but you can't store variables in the program memory.
To be honest I am a bit surprised. I thought that program memory was off limits to the user for anything, constants or variables in general.
If you do not store and access constants in the program memory, then you need the startup code to copy the constants from the program memory into the RAM on program startup. But requiring two copies of the variable is obviously a waste of space.
The program memory is non-volatile and keeps its contents after power-off. But newer memory architectures, such as the flash memory in just about every new processor, can be erased and reprogrammed in the system (ISP = in-system-programming) and even by the running application (IAP = In Application Programming).
This allows field upgrades of the processor. But it may also be used to store configuration data in the program memory.
But since it is intended for program memory and for field upgrades of the application, you can normally not write individual bytes. And each write normally takes quite long time and may require a full block to be erased. Because of this, it will not work for normal variables. You can't just do a memory write like you can with the RAM memory.
A number of processors - such as the 8051 - separates program and data memory. This is called a Harward architecture. The processor has one set of instructions to access data memory and another set of instructions to access program memory. This allows the compiler to select suitable instructions when accessing a constant stored in the program memory. But there will not exist any instructions for writing into the program memory.
The solution to the above problem - how to write into the program memory for processors with separated program and data address spaces - is normally to allow special function registers indirectly access and write new data, or to create a double-mapped memory area where you can access a mapped memory block using either data operations or program operations.
Most newer processors unifies the address space for program and data, allowing the same processor instructions to access data or program memory. This is called a von Neumann architecture. It is just the address of the memory access that specifies if the access is within the range of a data memory or a program memory.
But in the end - yes, program memory is off limits. But only for "normal" write instructions. The compiler can produce code to pick up constants from the program memory. And some processors have invented methods to allow blocks of the program memory to be erased and rewritten with new information.