<< Chapter < Page Chapter >> Page >

Today, even assembly programmers do not have to worry about memory allocation because the assembler will handle that. The memory allocation programs are good enough at solving the problem that it isn't worth a programmer's time to solve this problem. Instead, there are a few different ways that languages solve the problem of memory allocation. In general, it is a simple matter to provide the programmer with the memory that is known to be needed at compile time. This includes primarily global data values and the space for the code itself. The more difficult problem is how to provide flexible data memory that may or may not be needed when the program actually executes.

The approach that C takes is to ask the programmer to call special functions that manage memory allocation. These methods are called malloc(int) and free(void *) The basic idea is that whenever the program needs a specific amount of additional memory, it calls malloc (memory allocate) with the integer being the number of bytes of memory needed. The program will then search for a block of memory of the appropriate size and return a pointer to it. When the program is done with a particular allocation of memory, it calls free to let the memory management library know about the particular block of memory isn't needed anymore. If the programmer is diligent about returning (freeing) memory that isn't needed anymore, then the programmer will enjoy abundant memory without having to count individual bytes. On the other hand, if a program repeatedly requests memory but does not free the memory back to the system, the memory allocator will eventually run out of memory. The program will then crash. Thus, it is essential for passages of code that frequently request memory allocations to free these allocations as they can. Un-freed allocations are not fatal in very infrequently executed parts of code; however, the longer a program runs, the more the problem will accrue. In general, a program that allocates but does not free memory, gradually using unnecessarily more memory over time, is said to have a memory leak .

Other languages handle the problem of memory allocation automatically. Java will allocate the memory for new data on the fly using the keyword new instead of the function malloc, but the more important difference is that freeing takes place automatically. Part of the Java system called the garbage collector detects memory that can be safely freed and does so. In this fashion, Java programs do not suffer memory leaks in the way a C program might.

Memory and the msp

In the MSP430 there is no inherent difference between instruction memory, data memory, and heap memory. The only subdivisions in memory are the blocks of flash and the section of RAM. Any of these sections can hold instructions or other kinds memory. However, because it is problematic to erase and rewrite flash in the middle of program execution, the flash memory is best saved for instructions and constants. The remaining RAM must be shared between the heap, the dynamically allocated memory, and the global variables. On the MSP430F169, there is only 2KB of RAM, so no memory leaks are tolerable.

How memory is wasted or conserved

Memory leaks, the most notable way to waste memory, have already been discussed, but there are several others. While memory leaks abuse the dynamically allocated portion of data memory, many layers of function calls abuse the heap. Above, it was explained that each time a function calls another function, the caller's registers and data are moved onto the heap. If each called function calls another function in turn, then the heap portion of the memory will grow significantly. For high power computing systems, this is not usually a great threat to the overall supply of memory compared to memory leaks. Embedded systems however must avoid deep layers of function calling or risk exhausting the overall supply of memory.

There is also a programming technique called recursion wherein a recursive function calls itself repeatedly on progressively smaller or simpler versions of the data until the answer is trivial. While this technique leads to some very clever solutions to some complex problems, it is uses large amounts of memory to achieve this end. Therefore, recursion is generally a poor choice when memory must be conserved.

Finally, another important way to waste memory is to create too many global variables. Specifically, variables whose scope could be local or who could be allocated dynamically waste memory because they take up space even when they aren't being used. Use malloc and free to avoid using as many global variables.

Questions & Answers

how does Neisseria cause meningitis
Nyibol Reply
what is microbiologist
Muhammad Reply
what is errata
Muhammad
is the branch of biology that deals with the study of microorganisms.
Ntefuni Reply
What is microbiology
Mercy Reply
studies of microbes
Louisiaste
when we takee the specimen which lumbar,spin,
Ziyad Reply
How bacteria create energy to survive?
Muhamad Reply
Bacteria doesn't produce energy they are dependent upon their substrate in case of lack of nutrients they are able to make spores which helps them to sustain in harsh environments
_Adnan
But not all bacteria make spores, l mean Eukaryotic cells have Mitochondria which acts as powerhouse for them, since bacteria don't have it, what is the substitution for it?
Muhamad
they make spores
Louisiaste
what is sporadic nd endemic, epidemic
Aminu Reply
the significance of food webs for disease transmission
Abreham
food webs brings about an infection as an individual depends on number of diseased foods or carriers dully.
Mark
explain assimilatory nitrate reduction
Esinniobiwa Reply
Assimilatory nitrate reduction is a process that occurs in some microorganisms, such as bacteria and archaea, in which nitrate (NO3-) is reduced to nitrite (NO2-), and then further reduced to ammonia (NH3).
Elkana
This process is called assimilatory nitrate reduction because the nitrogen that is produced is incorporated in the cells of microorganisms where it can be used in the synthesis of amino acids and other nitrogen products
Elkana
Examples of thermophilic organisms
Shu Reply
Give Examples of thermophilic organisms
Shu
advantages of normal Flora to the host
Micheal Reply
Prevent foreign microbes to the host
Abubakar
they provide healthier benefits to their hosts
ayesha
They are friends to host only when Host immune system is strong and become enemies when the host immune system is weakened . very bad relationship!
Mark
what is cell
faisal Reply
cell is the smallest unit of life
Fauziya
cell is the smallest unit of life
Akanni
ok
Innocent
cell is the structural and functional unit of life
Hasan
is the fundamental units of Life
Musa
what are emergency diseases
Micheal Reply
There are nothing like emergency disease but there are some common medical emergency which can occur simultaneously like Bleeding,heart attack,Breathing difficulties,severe pain heart stock.Hope you will get my point .Have a nice day ❣️
_Adnan
define infection ,prevention and control
Innocent
I think infection prevention and control is the avoidance of all things we do that gives out break of infections and promotion of health practices that promote life
Lubega
Heyy Lubega hussein where are u from?
_Adnan
en français
Adama
which site have a normal flora
ESTHER Reply
Many sites of the body have it Skin Nasal cavity Oral cavity Gastro intestinal tract
Safaa
skin
Asiina
skin,Oral,Nasal,GIt
Sadik
How can Commensal can Bacteria change into pathogen?
Sadik
How can Commensal Bacteria change into pathogen?
Sadik
all
Tesfaye
by fussion
Asiina
what are the advantages of normal Flora to the host
Micheal
what are the ways of control and prevention of nosocomial infection in the hospital
Micheal
what is inflammation
Shelly Reply
part of a tissue or an organ being wounded or bruised.
Wilfred
what term is used to name and classify microorganisms?
Micheal Reply
Binomial nomenclature
adeolu
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Microcontroller and embedded systems laboratory. OpenStax CNX. Feb 11, 2006 Download for free at http://cnx.org/content/col10215/1.29
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Microcontroller and embedded systems laboratory' conversation and receive update notifications?

Ask