Materials of VTU CBCS 7th sem Machine Learning(15CS73), Machine Learning Lab(15CSL76), 6th sem Python Application Programming(156CS664), 3rd sem Data Structures (15CS33), Data Structure in C Lab (15CSL38)
Showing posts with label Dynamic Memory management. Show all posts
Showing posts with label Dynamic Memory management. Show all posts
Monday, 1 August 2016
realloc() Dynamic Memory Allocation
Dynamic Memory Allocation: realloc()
Download the Program
Syntax:
ptr = (cast_type *)realloc(ptr, newsize_in_bytes);
#include<stdio.h>
void main()
{
char *str;
str = (char *)malloc(10);
if(str == NULL)
{
printf("\nInsufficient memory allocation");
exit(0);
}
strcpy(str, "hello");
printf("\nString is = %s", str);
str = (char *)realloc(str, 20);
if(str == NULL)
{
printf("\nInsufficient memory allocation");
exit(0);
}
strcpy(str, "how are you");
printf("\nString is = %s", str);
free(str);
}
Output:
String is = hello
String is = how are you
Also Check,
https://tejaswinihbhat.blogspot.in/2016/08/dynamic-memory-allocation.html
https://tejaswinihbhat.blogspot.in/2016/08/malloc-dynamic-memory-allocation.html
https://tejaswinihbhat.blogspot.in/2016/08/calloc-dynamic-memory-allocation.html
https://tejaswinihbhat.blogspot.in/2016/08/realloc-dynamic-memory-allocation.html
calloc() Dynamic Memory Allocation
Dynamic Memory Allocation: calloc()
ptr = (cast_type *) calloc (number_of_blocks, size_in_bytes);
#include<stdio.h>
void main()
{
int i,n;
int *arr;
printf("\nEnter the number of elements: ");
scanf("%d", &n);
arr = (int *)calloc(n, sizeof(int));
if(arr == NULL)
{
printf("\nInsufficient memory allocation");
exit(0);
}
printf("\nEnter the array elements: ");
for(i=0; i<n; i++)
scanf("%d", (arr+i));
printf("\nThe array elements are: ");
for(i=0; i<n; i++)
printf("%d ", *(arr+i));
free(arr);
}
Output:
Enter the number of elements: 5
Enter the array elements: 11 22 33 44 55
The array elements are: 11 22 33 44 55
Also Check,
https://tejaswinihbhat.blogspot.in/2016/08/dynamic-memory-allocation.html
https://tejaswinihbhat.blogspot.in/2016/08/malloc-dynamic-memory-allocation.html
https://tejaswinihbhat.blogspot.in/2016/08/calloc-dynamic-memory-allocation.html
https://tejaswinihbhat.blogspot.in/2016/08/realloc-dynamic-memory-allocation.html
Also Check,
https://tejaswinihbhat.blogspot.in/2016/08/dynamic-memory-allocation.html
https://tejaswinihbhat.blogspot.in/2016/08/malloc-dynamic-memory-allocation.html
https://tejaswinihbhat.blogspot.in/2016/08/calloc-dynamic-memory-allocation.html
https://tejaswinihbhat.blogspot.in/2016/08/realloc-dynamic-memory-allocation.html
malloc() Dynamic Memory Allocation
Dynamic Memory Allocation: malloc()
Download the ProgramSyntax:
ptr = (cast_type *) malloc(size_in_bytes);
#include<stdio.h>
void main()
{
int i, n;
int *arr;
printf("\nEnter the number of elements: ");
scanf("%d", &n);
arr = (int *)malloc(n*sizeof(int));
if(arr == NULL)
{
printf("\nInsufficient memory allocation");
exit(0);
}
printf("\nEnter the array elements: ");
for(i=0; i<n; i++)
scanf("%d", (arr+i));
printf("\nThe array elements are: ");
for(i=0; i<n; i++)
printf("%d ", *(arr+i));
free(arr);
}
Output:
Enter the number of elements: 5
Enter the array elements: 11 22 33 44 55
The array elements are: 11 22 33 44 55
Also Check,
https://tejaswinihbhat.blogspot.in/2016/08/dynamic-memory-allocation.html
https://tejaswinihbhat.blogspot.in/2016/08/malloc-dynamic-memory-allocation.html
https://tejaswinihbhat.blogspot.in/2016/08/calloc-dynamic-memory-allocation.html
https://tejaswinihbhat.blogspot.in/2016/08/realloc-dynamic-memory-allocation.html
Wednesday, 20 July 2016
Data Structures and Applications(15CS33): Module 1
Data Structures and Applications (15CS33):
Module 1
Click on the respective topics:
Subscribe to:
Posts (Atom)