void pointer:
Pointer a variable used to store the address of another
variable.
The type of pointer must be equal to the type of variable.
Void pointer is a generic pointer capable to store the
address of any type of variable.
But void pointer can't access the value of original variable
using indirection because the scale factor of void pointer is zero.
Void pointer need to be changed into original variable type
of pointer.
Example:
#include<stdio.h>
void main()
{
int x=10;
float y=12.50;
char ch='a';
void *p=&x,*q=&y,*r=&ch;
clrscr();
printf("x=%d",*p);
printf("\ny=%f",*q);
printf("\nch=%c",*r);
getch();
}
Output:
Error: Not an allowed
type in function main()
Example explained:
Though void pointer is capable to store the addresses of
different variable of different types, can't access the values through
indirection.
Void pointer needs to be converted into variable type of
pointers.
Example rectified:
#include<stdio.h>
#include<stdio.h>
void main()
{
int x=10;
float y=12.50;
char ch='a';
void
*p=&x,*q=&y,*r=&ch;
clrscr();
printf("x=%d",*(int*)p);
printf("\ny=%f",*(float*)q);
printf("\nch=%c",*(char*)r);
getch();
}
Output:
x=10
y=12.500000
ch=a
Example explained:
void pointer is converted into variable type of pointers
that is int pointer, float pointer and char pointers.
Dynamic memory allocation:
The memory allocation of a variable or an array allocates
during the compilation of program, called compile time memory allocation.
This memory is used while executing the program.
It results wastage of memory.
For example if we declare an array called "int a[50]".
Only few elements would be used during the execution of program results wastage
of memory.
Dynamic or runtime memory allocation is the allocating of
memory during execution of program.
It results effective memory management.
The following functions are used for dynamic memory
allocation. All these functions are defined in a header file called
"stdio.h"
- sizeof()
- malloc()
- realloc()
- calloc()
- free()
sizeof():
It is the function also considered as an operator used to
get the size of a variable, type, an array or a pointer.
Example1:
#include<stdio.h>
void main()
{
int a=255;
float b=12.25;
char ch='x';
clrscr();
printf("size of a %d",sizeof(a));
printf("\nsize of b %d",sizeof(b));
printf("\nsize of ch %d",sizeof(ch));
getch();
}
Output:
size of a 2
size of b 4
size of ch 1
Example explained:
The size of int type is 2
The size of float is 4
The size of char is 1
Example2:
#include<stdio.h>
void main()
{
int *p;
float *q;
char *r;
clrscr();
printf("size of int pointer %d",sizeof(p));
printf("\nsize of float pointer %d",sizeof(q));
printf("\nsize of char pointer %d",sizeof(r));
getch();
}
Output:
size of int pointer 2
size of float pointer 4
size of char pointer 1
Example explained:
size of any pointer is 2
Example3:
#include<stdio.h>
void
main()
{
char *a="
char b[]="
clrscr();
printf("%s",a);
printf("\n%s",b);
printf("\nsize of a %d",sizeof(a));
printf("\nsize of b %d",sizeof(b));
getch();
}
Output:
size of a 2
size of b 6
Example explained:
Both "a" and "b" are strings
"a" is a pointer assigned with a string, shows its
size as 2 because the size of any pointer is 2
"b" is an array assigned with a string, shows its
size as 6 that is the size of array
malloc():
It is defined within the header file "stdio.h"
It is the function used to allocate the memory allocation of
specified number of bytes during the execution of program (runtime memory
allocation).
It returns the address of first byte of memory allocation as
void pointer.
The void pointer can be converted into the required pointer type.
Function definition:
void* malloc(int size)
{
----------------------
----------------------
}
Example1:
Example2:
free();
It is the function defined within the header file
"stdio.h"
It accepts the address of memory allocation allocated by
malloc() and de-allocates the memory.
It helps to effectively use the memory while developing
complex applications.
Example:
Specification1:
Accept two integers and print the sum of them using dynamic
memory allocation.
Program:
#include<stdio.h>
void main()
{
int *p,*q,*r;
clrscr();
p=(int*)malloc(2);
q=(int*)malloc(2);
r=(int*)malloc(2);
printf("Enter
two numbers:\n");
scanf("%d%d",p,q); /*
addresses */
*r=*p+*q;
printf("Sum %d",*r);
free(p);free(q);free(r);
getch();
}
Output:
Enter two numbers:
12
34
Sum 46
Example explained:
malloc() is used to allocate 2 bytes of memory as
"int" type of memory.
0 comments:
Post a Comment