DSA Unit 1
DSA Unit 1
1. malloc()
2. calloc()
3. realloc()
4. free()
Static Testing and dynamic
Testing
Syntax:
pointer=(type*) malloc (number *sizeof(type));
Ex: (int*)malloc(20*sizeof(int));
char *k;
k = malloc( 20 * sizeof(char) );
Example of Malloc
#include <stdlib.h>
int main() {
int n, i, *ptr, sum = 0;
printf("Enter number of elements: ");
scanf("%d", &n);
ptr = (int*) malloc(n * sizeof(int));
if(ptr == NULL) {
printf("Error! memory not allocated.");
exit(0);
}
printf("Enter elements: ");
for(i = 0; i < n; ++i) {
scanf("%d", ptr + i);
sum += *(ptr + i);
}
printf("Sum = %d", sum);
free(ptr);
return 0;
}
2] calloc () :
• function is similar to malloc () function. calloc ()
initializes to default value.
• This is a function in C that allocates a block of memory
for an array of elements. It stands for "contiguous
allocation". When you use calloc, you tell the computer
how many elements you need and how big each element
is. Then, it gives you a chunk of memory big enough to
hold all those elements, and it sets all the bits to zero.
• So, if you want, say, 10 integers worth of memory using
calloc, it will give you a block of memory where you can
store 10 integers, and it will make sure all those integers
start with a value of 0.
• In simpler terms, calloc is like renting a bunch of boxes in
a warehouse, and the nice thing is, when you get those
boxes, they're all empty and ready for you to use!
Syntax: pointer=(type*)calloc(arraysize,datatype);
Ex: char *k;
k = calloc( 20, sizeof(char) );
3] realloc ()
• function modifies the allocated memory size by malloc ()
and calloc () functions to new size. If enough space
doesn’t exist in memory of current block to extend, new
block is allocated for the full size of reallocation, then
copies the existing data to new block and then frees the
old block.
• This function in C helps you change the size of memory
that you've already asked for using functions like malloc
or calloc. Let's say you initially asked for space to store 10
items, but then you realize you need space for 15 items.
Instead of asking for a completely new block of memory,
you can use realloc to expand the existing block to
accommodate the extra items.
• Similarly, if you find you have too much space allocated,
you can use realloc to shrink the block of memory. It's like
saying, "Hey, I rented this storage unit for 10 boxes, but
now I only need space for 8 boxes. Can I give back some
of this space?" And realloc handles that for you.
• In essence, realloc helps you manage your memory more
efficiently by adjusting the size of the memory block
you've previously allocated.
Example of Realloc
#include <stdlib.h>
int main() {
int *ptr, i , n1, n2;
printf("Enter size: ");
scanf("%d", &n1);
ptr = (int*) malloc(n1 * sizeof(int));
printf("Addresses of previously allocated memory:\n");
for(i = 0; i < n1; ++i)
printf("%pc\n",ptr + i);
printf("\nEnter the new size: ");
scanf("%d", &n2);
ptr = realloc(ptr, n2 * sizeof(int));
printf("Addresses of newly allocated memory:\n");
for(i = 0; i < n2; ++i)
printf("%pc\n", ptr + i);
free(ptr);
return 0;
}
Syntax:pointer= realloc(pointername,newsize);
Ex:k= realloc(k,40);
Pointers:
• Are one of the core components of the C programming
language. A pointer can be used to store the memory
address of other variables, functions, or even other
pointers. The use of pointers allows low-level memory
access, dynamic memory allocation, and many other
functionality in C.
int **pp=&p;
• Pointers in C are like signposts or arrows that show where
something is stored in the computer's memory. Instead of
holding the actual data, a pointer holds the memory
address of where the data is stored.
• Declaration and Initialization: When you declare a pointer,
you're telling the computer that this variable will hold the
memory address of a specific type of data. For example,
int *ptr; declares a pointer that can point to an integer. To
make a pointer point to something, you assign it the
memory address of another variable using the & operator.
For example, ptr = &x;, where x is an integer variable.
• Dereferencing: To access the actual value stored at the
memory address pointed to by a pointer, you dereference
it using the * operator. For example, *ptr gives you the
value stored at the memory address stored in ptr.
• Pointer Arithmetic: You can do arithmetic with pointers to
move them around in memory. For example, adding 1 to a
pointer moves it to the next memory location of the type it
points to
Types of pointers:
There are many types of pointers:
Null Pointer: A pointer which is pointing to nothing is known
as a null pointer. Null pointer points to the base address of the
segment.
Array of Pointer
A pointer array is a homogeneous collection of indexed pointer
variables that are references to a memory location. It is
generally used in C Programming when we want to point at
multiple memory locations of a similar data type in our C
program. We can access the data by dereferencing the pointer
pointing to it.
Syntax:
pointer_type *array_name [array_size];
Here,
• pointer_type: Type of data the pointer is pointing to.
• array_name: Name of the array of pointers.
• array_size: Size of the array of pointers.
Example:
#include <stdio.h>
int main()
{
int var1 = 10;
int var2 = 20;
int var3 = 30;
int* ptr_arr[3] = { &var1, &var2, &var3 };
for (int i = 0; i < 3; i++) {
printf("Value of var%d: %d\tAddress: %p\n", i + 1,
*ptr_arr[i], ptr_arr[i]);
}
return 0;
}
Array of Pointers to Character
#include <stdio.h>
int main()
{ char str[3][10] = { "kgf", "veer", "jaiho" };
printf("String array Elements are:\n");
for (int i = 0; i < 3; i++) {
printf("%s\n", str[i]);
}
return 0;
}