9953330565 Low Rate Call Girls In Rohini Delhi NCR
Dynamic memory allocation
1.
2. • The blocks of information in a memory system is called
memory allocation.
• To allocate memory it is necessary to keep in information of
available memory in the system. If memory management
system finds sufficient free memory, it allocates only as much
memory as needed, keeping the rest available to satisfy future
request.
• Memory allocation has two types.
• They are static and dynamic memory allocation.
3. STATIC MEMORYALLOCATION
• In static memory allocation, size of the memory may be required and that
must be defined before loading and executing the program.
• If memory is allocated at compile time, it cannot be changed during
execution. There will be a problem of either insufficiency or else
wastage of memory
DYNAMIC MEMORYALLOCATION
• In the dynamic memory allocation, the memory is allocated to a
variable or program at the run time.
• The only way to access this dynamically allocated memory is
through pointer.
4.
5.
6. malloc() function is used for allocating block of memory at
runtime. This function reserves a block of memory of given size
and returns a pointer of type void.
A block of memory is allocated by malloc
Allocates single block of requested memory.
Allocates specified number of bytes
Returns NULLif memory is not sufficient. It doesn't initialize
memory at execution time, so it has garbage value initially.
ptr=(cast-type*) malloc (byte-size)
7. • calloc() is another memory allocation function that is used for
allocating memory at runtime. calloc function is normally used for
allocating memory to derived data types such as arrays and
structures.
• Allocates multiple block of requested memory.
• Allocates specified numbers of bytes.
• Initially initialize all bytes to zero.
Ptr=(cast-type*)calloc(n,elem-size);
8. • realloc() changes memory size that is already allocated
dynamically to a variable.
• It can either reduce (or) extend the allocated memory .
• It returns a void pointer that points to the base address
of reallocated memory
ptr=realloc(ptr,new_size);
9. • Free() function should be called on a pointer that was used either with
”calloc()” or “malloc()”, otherwise the function will destroy the memory
management making a system to crash.
• This function frees ( or) de-allocates previously allocated memory
space.
• With dynamic runtime allocation, it is our responsibility to release
the space when it is not required for effective usage of memory.
free (ptr);
10. Malloc() Calloc()
The name malloc stands
for memory allocation.
Takes only one argument
Amount of memory is in
bytes
Does not initialize the
memory allocated
The name calloc stands
for contiguous allocation
Takes two argument
Amount of memory of single
variable in numbers of
variable
Guarantees that all the bytes
of allocated memory block
have been initialized to 0