Sunteți pe pagina 1din 11

Dynamic Memory Allocation

Dynamic Memory Allocation


void* malloc(size_t size): Because the block pointer
Request a contiguous returned by malloc() is a
block of memory of the void * (i.e., it makes no
given size in the heap. claim about the type of
malloc() returns a pointer to its pointee), a cast will
the heap block or NULL if probably be required
the request is not when storing the void
satisfied. pointer into a regular
The type size_t is essentially typed pointer.
an unsigned long that
indicates how large a
block the caller would like
measured in bytes.
Dynamic Memory Allocation
• calloc(): works like
malloc, but initializes the
memory to zero if
possible. The prototype is
• This function allocates a
block long enough to
contain an array of count
elements, each of size
eltsize. Its contents are
cleared to zero before
calloc returns.
Dynamic Memory Allocation
void free(void* block): free()
takes a pointer to a heap
block earlier allocated by
malloc() and returns
that block to the heap for
reuse.
After the free(), the client
should not access any part
of the block or assume that
the block is valid memory.
The block should not be
freed a second time
Dynamic Memory Allocation
• void* realloc(void* block, • Remember to catch and
size_t size): Take an existing examine the return value of
heap block and try to realloc().
reallocate it to a heap block of • It is a common error to
the given size which may be continue to use the old block
larger or smaller than the pointer. realloc() takes care of
original size of the block. moving the bytes from the old
• It returns a pointer to the new block to the new block.
block, or NULL if the • realloc() exists because it can
reallocation was unsuccessful. be implemented using low-
level features that make it
more efficient than the C code
a programmer could write
Dynamic Memory Allocation
#include <stdlib.h> fp2 = (float *) malloc(sizeof(float));
#include <stdio.h> if(fp2 == NULL)
int main() { printf(“out of memory\n”);
{ int * ip;
double * dp; exit(-1);
float * fp1; fl }
oat * fp2; *ip = 42;
ip = (int *) malloc(sizeof(int)); *dp = 3.1415926;
if(ip == NULL) *fp1 = -1.2;
{ printf(“out of memory\n”); *fp2 = 0.34;
exit(-1); printf(“ip: address %d; contents %d\n”,
} (int)ip, *ip);
dp =(double *) malloc(sizeof(double)); printf(“dp: address %d; contents %f\n”,
if(dp == NULL) (int)dp, *dp);
{ printf(“out of memory\n”); printf(“fp1: address %d; contents %f\n”,
exit(-1); (int)fp1, *fp1);
} printf(“fp2: address %d; contents %f\n”,
fp1 =(float *) malloc(sizeof(float)); (int fp2, *fp2);
if(fp1 == NULL) return 0;
{ printf(“out of memory\n”); }
exit(-1);
}
Dynamic Memory Allocation
Output:
ip: address 133792;
contents 42
dp: address 133808;
contents 3.141593
fp1: address 133824;
contents -1.200000
fp2: address 133840;
contents 0.340000
Dynamic Memory Allocation
#include <stdio.h>
#include <stdlib.h>
int main()
{ int N,*a,i,s=0;
printf(“\n enter no. of elements of
the array:”);
scanf(“%d”,&N);
a=(int *)malloc(N*sizeof(int));
if(a==NULL)
{ printf(“\n memory allocation
unsuccessful...”);
exit(0);
}
printf(“\n enter the array elements
one by one”);
for(i=0; i<N;++i)
{ scanf(“%d”,&a[i])); /* equivalent
statement
scanf(“%d”,(a+i));*/
s+=a[i];
}
printf(“\n sum is %d ”,s);
return 0;
}
Dynamic Memory Allocation
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int *array;
int size = 1;
int i;
printf(“Enter the number of values:”);
scanf(“%d”, &size);
array = (int *)calloc(size, sizeof(int));
for(i=0; i<size; i++) {
printf(“Please enter value #%d: ”, i+1);
scanf(“%d”, array+i);
}
for(i=0; i<size; i++)
printf(“Value #%d is: %d\n”, i+1,
array[i]);
free(array);
return 0;
}
Dynamic Memory Allocation
#include <stdio.h>
#include <alloc.h>
#include <string.h>
int main(void)
{
char *str = NULL;

/* allocate memory for string */


str = (char *)calloc(10, sizeof(char));

/* copy “Hello” into string */


strcpy(str, “Hello”);

/* display string */
printf(“String is %s\n”, str);

/* free memory */
free(str);
str=NULL;
return 0;
}
Dynamic Memory Allocation
#include <stdio.h> if(np == NULL)
#include <stdlib.h> {
int main() printf(“out of memory\n”);
{ int N,*a,*np,i,s=0; exit(1);
char ans=‘Y’; }
printf(“\n Enter no. of elements of the a = np;
array:”); }
scanf(“%d”,&N); printf(“\n Enter the number ...”);
a=(int *)malloc(N*sizeof(int)); scanf(“%d”,&a[i]);
if(a==NULL) s+=a[i];
{ printf(“\n memory allocation i++;
unsuccessful”); printf(“\n Do U 12
exit(0); Continue(y/n)?...”);
} fflush(stdin);
i=0; scanf(“%c”, &ans)
while(toupper(ans)==‘Y’) }
{ if(i >= N) N=i;
{ /* increase allocation */ printf(“\n THE NUMBERS ARE:...\n”);
N *=2; for(i=0;i<N;++i)
np =(int *)realloc(a,N*sizeof(int)); printf(“\n%d”,a[i]);
printf(“\n Sum is %d”,s);
return 0;
}

S-ar putea să vă placă și