Dynamic Storage Allocation Techniques Final PDF
Dynamic Storage Allocation Techniques Final PDF
Dynamic Storage Allocation Techniques Final PDF
Techniques
Submitted To:
Cherry Khosla
Date of Submission: 08 April 2015
ACKNOWLEDGEMENT:
First of all I would like to thank the Lovely Professional University, and take
the opportunity to do this as a part of the B-TECH
Many people have influenced the shape and content of this term paper, and
many supported me through it. I express my sincere gratitude to Cherry
Khosla for assigning me a term paper on Dynamic Storage Allocation
Techniques .
She has been an inspiration and role model for this topic. Her guidance and
active support has made it possible to complete the assignment.
I also would like to thank my Friends who have helped and encouraged me
throughout the working of the project.
Finally, I take this opportunity to acknowledge the services of the total team of
publisher and everyone who collaborated in producing this work.
1. Introduction
Memory management is the act of managing computer memory at the
system level. The essential requirement of memory management is to
provide ways to dynamically allocate portions of memory to programs at
their request, and free it for reuse when no longer needed. This is critical
to any advanced computer system where more than a single
process might be underway at any time. Several methods have been
devised that increase the effectiveness of memory management. Virtual
memory systems separate the memory addresses used by a process from
actual physical addresses, allowing separation of processes and increasing
the effectively available amount of RAM using Paging or swapping
to secondary storage. The quality of the virtual memory manager can
have an extensive effect on overall system performance.
The task of fulfilling an allocation request consists of locating a block of
unused memory of sufficient size. Memory requests are satisfied by
allocating portions from a large pool of memory called the heap or free
store. At any given time, some parts of the heap are in use, while some
are "free" (unused) and thus available for future allocations.
Several issues complicate the implementation, such as external
fragmentation, which arises when there are many small gaps between
allocated memory blocks, which invalidates their use for an allocation
request. The allocator's metadata can also inflate the size of (individually)
small allocations. This is often managed by chunking. The memory
management system must track outstanding allocations to ensure that
they do not overlap and that no memory is ever "lost" as a memory leak.
Sometimes we do not release the user to create different variables
according to their interest. It's a bit difficult to explain well, so give an
example: imagine you want to do a text encoder, you do not know how
much space you will need in memory until the user gets the text, which
can be a word or an entire book. The solution is to put a rookie char
[10000], but even so we can be short, and if you have to decode a word,
it goes up too much space. We need to change the variables and create
them according to what happens, that is what I am discussing in this term
paper.
2. Methodology used:
Static Data Storage Allocation
Compiler allocates space for all variables (local and global) of all
procedures at compile time.
No stack/heap allocation.
No overheads.
No recursion.
very difficult time. If too much waste of memory, and the less accessible
outside of the array, may result in an error.
The functions malloc (), realloc (), calloc () and free (),
the Library functions stdlib.h, malloc.h or are responsible for this task. All
data are stored in the free store (heap), which is limited to 64k a stack in
the beginning, this varies from machine. All variables were declared so far
on the stack, now you tell me where to go, in an indirect way.
In C we use malloc for allocating memory
The return type is of type void *, also receive the address of any type.
The fact is used as follows.
calloc() function
The calloc function is used to allocate storage to a variable while the
program is running. This library function is invoked by writing
calloc(num,size).This function takes two arguments that specify the
number of elements to be reserved, and the size of each element in bytes
and it allocates memory block equivalent to num * size . The function
returns a pointer to the beginning of the allocated storage area in
memory. The important difference between malloc and calloc function is
that calloc initializes all bytes in the allocation block to zero and the
allocated memory may/may not be contiguous.
calloc function is used to reserve space for dynamic arrays. Has the
following form.
void * calloc (size_t n, size_t size);
Number of elements in the first argument specifies the size in bytes of
one element to the second argument. A successful partitioning, that
address is returned, NULL is returned on failure.
For example, an int array of 10 elements can be allocated as follows.
int * array = (int *) calloc (10, sizeof (int));
Realloc()
With the function realloc, you can change the size of the allocated area
once. Has the following form.
void * realloc (void * ptr, size_t size);
The first argument specifies the address of an area that is currently
allocated to the size in bytes of the modified second argument. Change
the size, the return value is returned in re-allocated address space.
Otherwise it returns NULL.
Free() function:
how to release the reserved area, which will use the function free. Has
also been declared in stdlib.h, which has the following form.
void free (void * ptr);
The argument specifies the address of a dynamically allocated area. You
can then free up the space. Specify an address outside the area should
not be dynamically allocated. Also, if you specify a NULL (or NULL if the
same holds for a given pointer variable), the free function is guaranteed
By using new:
In the C++ programming language, as well as in many C++-based
languages, new is a language construct that dynamically
allocates memory from free store and initialises the memory using
the constructor. Except for a form called the "placement
new", new attempts to allocate enough memory in free store for the new
data. If successful, it initialises the memory and returns the address to
the newly allocated and initialised memory. However if new cannot
allocate memory in free store it will throw an exception of type
std::bad_alloc . This removes the need to explicitly check the result of an
allocation. A call to delete, which calls the destructor and returns the
memory allocated by new back to free store, must be made for every call
to new to avoid a memory leak.
Syntax:
p_var = new type_name;
3. Results:
As a general rule, dynamic behaviour is troublesome in real time
embedded systems. The two key areas of concern are determination of
the action to be taken on resource exhaustion and nondeterministic
execution performance.
There are a number of problems with dynamic memory allocation in a real
time system. The standard library functions (malloc() and free()) are not
normally re-entrant(it is a function), which would be problematic in a
multithreaded application. If the source code is available, this should be
straightforward to rectify by locking resources using RTOS facilities (like a
4. Conclusions:
C and C++ use memory in various ways, both static and
dynamic. Dynamic memory includes stack and heap. Dynamic
behaviour in embedded real time systems is generally a source
of concern, as it tends to be non-deterministic and failure is
hard to contain. Using the facilities provided by most real time
operating systems, a dynamic memory facility may be
implemented which is deterministic, immune from
fragmentation and with good error handling.
5. References
http://en.wikipedia.org/wiki
http://www.cprogrammingexpert.com
BELADY, L.A. A study of replacement algorithms for a virtual-storage
computer
http://www.cs.fsu.edu