0% found this document useful (0 votes)
26 views4 pages

Dyna 2

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views4 pages

Dyna 2

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Malloc means memory allocation

Malloc(size of the structure)

If size is 8 then form the heap section 8 bytes of memory is allocated

One completed block is allocated and malloc returns a pointer/base address of the block which is
allocated

It returns the address of the first byte allocated

Therefore, to allocate memory dynamically we need pointers without pointers we cannot allocate
memory.

General syntax of the malloc function is

(Void*) Malloc(size)

Here void* is the return type of the pointer

Size can only take 0 or positive values it cannot take negative values memory cannot be in negative
values

Since the malloc function doesn’t know the kind of value that gets stored in the 8bytes we use void
in this case based on the requirement we can change the return type of the malloc function

If we want to store integer function we can use int* if we want to store float then we can use float*

We usually don’t mention 20 or 8 any numerical values in the brackets we use certain method for
that also

We first initialise int *ptr;

Ptr(pointer)

Ptr=int* malloc(n*sizeof(int))

Int *ptr means this ptr is gonna store the address of a variable whose data type is an integer

If we don’t initialise the size of the memory then the malloc function takes some garbage value

If the malloc memory is sufficient then it will return ptr=NULL

If it is sufficient then it will retun the base address of the the block aloocated for the memory

Ptr is stored in stack memory.

Calloc in c
Calloc full form is contagious allocation
Built in function in stdlib.h library
Used to dynamically allocate multiple blocks of memory and each block is of the same size.
Malloc allocates single block of memory calloc allocates multiple blocks of memory

Calloc accepts 2 arguments while malloc accepts only one argument


Syntax for calloc function:
Void* Calloc(size_t n,size_t size)
Malloc only takes size,
Size_t only takes 0 or positive values.
n means the number of blocks or the number of elements you want the calloc to allocate memory
from the heap, size gives the size of each blocks.
Ex: Calloc(5,size of(int));
This means we need 5 blocks of integer type
If calloc run successfully then calloc returns the address of the first block.
It returns the address in the form of void pointer.
Ex:
If u want to store integer values in the block then we type cast the syntax to integer
(Int*) calloc(n,size of(size));
We store the address in a ptr, hence we initialise the pointer initially
Int *ptr; //static memory allocation//
Ptr=(int*)calloc(5,size of(size));//dynamic memory allocation//
Ptr now stores the first base address of it allocates the memory successfully,

If the size of each block is 2 bytes say,


Calloc allocates 5 blocks of each 2 bytes
Malloc allocates 1 block of 10 bytes

In calloc the memory value is initialised with 0 automatically


In malloc the memory value is initialised with an garbage value
If we want the initial memory value to be zero then only we should use calloc to allocate memory
dynamically.

Realloc:

Reallocation,

Resize the memory (increase or decrease the size of the memory)

Can be used only when malloc or calloc function is used to allocate memory in the programme.

If used in a static memory, then it shows undefined behaviour.

Built in function declared in stdlib.h.

Accepts 2 arguments, previously allocated pointer and the new size we want to resize it with.

Syntax:

(Void*)Realloc(void*ptr,size_t size);

The most important feature of realloc is that it resizes the memory block without losing the previous
content.
Ex; From heap section suppose we store
Int *ptr; five integers and let the first byte
address be 1000, then ptr returns
Ptr=(Int*) malloc(5*sizeof(int)); 1000, suppose now we want to add
2 more blocks of memory then we
Ptr1=(int*)realloc(ptr,7*sizeof(int));
do the following. The previous
content will be as it is and the first
byte address also remains the same
if the same block is expanded.

Sometimes when the block is not


expandable then the realloc will
create a new block to store the
memory. It copies the memory from
the previous block to the new block
(resized block) and then the first
byte address changes and the
address is stored in ptr1.

The content will remain the same


the address may change but the
content will always remain the
same.

If on failure of the reallocation it


returns null.

If the size is reduced during


reallocation, then data would be
lost.
Free()

Releases the dynamically allocated memory.

Built in function defined in stdlib.h

Syntax:

Void Free(pointer)

Ex:

Int *ptr;

Ptr=(int*)malloc(2*sizeof(int));

Free(ptr);

You might also like