Dynamic memory allocation Flashcards

1
Q

types of memory in c

A

static memory
dynamic memory

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Dynamic memory allocation

A

it is a way to allocate memory to a datastructure during the runtime

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Functions used for DMA

A

-malloc()-memory allocation
-calloc()-continous allocation
-free()
-realloc()-re allocation

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

malloc()

A

takes a number of bytes to be allocated and returns a pointer of type void

ptr=(int) malloc(5sizeof(int));-here void pointer is typecasted into int pointer

have to include <stdlib.h> library</stdlib.h>

if the give memory size is out of your systems capability then the malloc returns null to the pointer

ptr[0]=1;
ptr[1]=2;
ptr[2]=3;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

calloc()

A

continuous allocation

-initializes with 0
ptr=(int*) calloc(5,sizeof(int));

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

free()

A

-we use it to free memory that is allocated using malloc and calloc

when a variable(int) is statically allocated then after its use then the memory is freed up automatically which is not in dynamic memory

free(ptr);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

realloc()

A

reallocate the (increase of decrease ) the memory using the same pointer and size

ptr=realloc(ptr,newsize);

we cannot do this with array only with dynamically allocated memory

How well did you know this?
1
Not at all
2
3
4
5
Perfectly