Download as pdf or txt
Download as pdf or txt
You are on page 1of 24

POINTERS

POINTER TO POINTERS
MEMORY ALLOCATIONS
Pointers
 Pointer is one of the powerful feature of C language
programming
 Pointers are used in C
program to access the
memory and manipulate
the address
 Figure on the side:
Pointer “a” pointing to the
memory address associated
with variable “b”
 Both pointers & non-pointers;
this need not be the case
Pointer (2/2)
 Pointer is a programming language data type
whose value "points to" another value stored
elsewhere in the computer memory using its address
 A pointer must now the data type its points to,
therefore it must be declared similar to data type
 Several languages support some type of pointer,
but may have restrictions on theirs
 Example in a book:
a page number in a book's index could be
considered a pointer to the corresponding page
Example of pointer
 Take a look at the following programs
 Example-of-pointer.c
Explanation of program and figure
•Code int *pc, p; creates a pointer pc and a variable c.
Pointer pc points to some address and that address has garbage value. Similarly,
variable c also has garbage value at this point.
•Code c=22; makes the value of c equal to 22, i.e.,22 is stored in the memory
location of variable c.
•Code pc=&c; makes pointer, point to address of c.
Note that, &c is the address of variable c (because c is normal variable) and pc is
the address of pc (because pc is the pointer variable). Since the address of pc and
address of c is same, *pc (value of pointer pc) will be equal to the value of c.
•Code c=11; makes the value of c, 11.
Since, pointer pc is pointing to address of c. Value of *pc will also be 11.
•Code *pc=2; change the address pointed by pointer pc to change to 2.
Since, address of pointer pc is same as address of c, value of c also changes to 2.
• Code int *pc, p; creates a pointer pc and a variable c. Pointer pc points to
some address and that address has garbage value. Similarly, variable c
also has garbage value at this point.
• Code c=22; makes the value of c equal to 22, i.e.,22 is stored in the
memory location of variable c.
• Code pc=&c; makes pointer, point to address of c. Note that, &c is the
address of variable c (because c is normal variable) and pc is the address
of pc (because pc is the pointer variable). Since the address of pc and
address of c is same, *pc (value of pointer pc) will be equal to the value of
c.
• Code c=11; makes the value of c, 11. Since, pointer pc is pointing to
address of c. Value of *pc will also be 11.
• Code *pc=2; change the address pointed by pointer pc to change to 2.
Since, address of pointer pc is same as address of c, value of c also
changes to 2.
Output of example-to-pointer.c
1. Address of c: 2686784
2. Value of c: 22
3. Address of pointer pc: 2686784
4. Content of pointer pc: 22
5. Address of pointer pc: 2686784
6. Content of pointer pc: 11
7. Address of c: 2686784
8. Value of c: 2
Pointers
 Since a pointer is a variable which contains the
address in memory of another variable a pointer
can be of any variable type
 C uses pointers a lot for:
 producing compact and efficient code.
 providing a very powerful tool for data manipulations

 C uses pointers explicitly for:


 Arrays
 Structures
 Functions
Pointer in C language
 Declaration of pointer:
data_type * pointer_variable_name;
int *p;
 Take a look at program pointer-1.c land pointer-2.c
 Assignment:
add two numbers using pointer variable
subtract two numbers using pointer variable
multiply two numbers using pointer variable
divide two numbers using pointer variable
Pointer and array
 Arrays are closely related to pointers in C
programming
 Arrays and pointers are synonymous in terms of how
they use to access memory
 A pointer variable can take different addresses as
value whereas, in case of array it is fixed
Example program, try this
#include <stdio.h>
int main()
{
char c[4];
int i;
for(i=0;i<4;++i)
{
printf("Address of c[%d]=%x\n",i,&c[i]);
}
}
Pointer and array
&a[0] is equivalent to (a) AND, a[0] is equivalent to *(a).
&a[1] is equivalent to (a+1) AND, a[1] is equivalent to *(a+1).
&a[2] is equivalent to (a+2) AND, a[2] is equivalent to *(a+2).
&a[3] is equivalent to (a+1) AND, a[3] is equivalent to *(a+3).
. . .
&a[i] is equivalent to (a+i) AND, a[i] is equivalent to *(a+i).
Memory allocation using pointer (1/3)

 The amount of memory required is not known until


the program is run
 If memory is allocated at the beginning, program
will be inefficient, allocation can be too large or too
small
 Dynamic memory allocation allows program to
obtain more memory if needed, or release memory
when it is not needed and give it to other programs
 Amount of memory available depends on the
hardware and also operating system
Memory allocation using pointer (2/3)

 In C language there are four functions available for


memory allocation:
Function Meaning of function
Allocates requested size of bytes and returns a
malloc()
pointer first byte of allocated space
Allocates space for an array elements, initializes to
calloc()
zero and then returns a pointer to memory
free() deallocate the previously allocated space
realloc() Change the size of previously allocated space
Memory allocation using pointer (3/3)

ptr=(cast-type*)malloc(byte-size);
Example:
ptr=(int*)malloc(100*sizeof(int));

ptr=(cast-type*)calloc(n,element-size);
Example:
ptr=(float*)calloc(25,sizeof(float));

free(ptr);
ptr=realloc(ptr,newsize);
Pointer to pointer (1/3)
 We have learnt about a pointer to a variable
 We have new concept of a pointer pointing to a
pointer to a variable
Pointer to pointer (2/3)
 So we can see that in memory, pointer p1 holds the
address of pointer p2. Pointer p2 holds the address
of character ‘ch’
 So ‘p2′ is pointer to character ‘ch’, while ‘p1′ is
pointer to ‘p2′ or we can also say that ‘p2′ is a
pointer to pointer to character ‘ch’
Pointer to pointer (3/3)
 Pointer declaration for the above problem:
char *p2 = &ch;
char **p1 = &p2;
Example of pointer to pointer
#include <stdio.h> // file pointer-to-pointer.c
void main(void)
{
char **ptr = NULL;
char *p = NULL;
char c = 'd';
p = &c;
ptr = &p;
printf("\n c = [%d]\n",c);
printf("\n *p = [%d]\n",*p);
printf("\n **ptr = [%d]\n",**ptr);
}
Memory allocation for matrix
 Example program:
 Static memory allocation matrix-2-static.c
 Dynamic memory allocation matrix-3-pointer.c
Program and operating system
 One of the task of an operating system (OS) is to
manage memory
 When a program requires more memory, it will ask
the OS for the required memory:
 OS provide the needed memory if available
 OS return error if the needed memory is unavailable

 When the program is finished or no longer needs


the memory, it must release the memory back to the
OS
 The above condition is not always the case creating
memory leak and others
Garbage collection and OS
 There is a need of mechanism to collect the unused
memory left by program and make it available
again
 Garbage collection is the mechanism
 Memory leak (memori bocor):
mechanism that make the amount of memory
reduced after some time
 Garbage collection can reduce memory leak
What is collected by GC
 GC not only reclaim memory, but also some other
resources:
 Stack
 Network socket/ network socket
 Unclaimed PID and ports

 Network sockets
 Database handles
 Windows-user interaction
 Always do free() when pointer not used from
malloc()
Assignment
 Create program to multiply array1 and array2
 Ask the size of the array
 Allocate the memory for the array using malloc
 Ask for the array members
 Multiply the array
 Display the results

You might also like