26-08-2014, 11:55 AM
Pointers On Seminar Report
Pointers.ppt (Size: 349.5 KB / Downloads: 13)
Variable
A variable is a named memory location.
Variables provide direct access to its memory location.
A variable has a name, an address, a type,and a value:
"the name identifies the variable to the programmer
"the address specifies where in main memory the variable is located
What is a variable?
the type specifies how to interpret the data stored in main memory and how long the variable is
"the value is the actual data stored in the variable after if has been interpreted according to a given type
Pointer variable
A pointer is a variable that contains the memory location of another variable.
Syntax:-
type * variable name
You start by specifying the type of data stored in the location identified by the pointer.
The asterisk tells the compiler that you are creating a pointer variable.
Finally you give the name of the variable
Accessing a variable through itspointer its pointer
Once we declare a pointer variable we must point it to something. We can do this by assigning to the pointer the address of the variable you want to point as in the following example:
ptr=#
This places the address where num is stores into the variable ptr. If num is stored in memory 21260 address then the variable ptr has the value 21260.
Address and Pointers
Memory can be conceptualized as a linear set of data locations.
Variables reference the contents of a locations
Pointers have a value of the address of a given location
Pointer arithmetic
Valid operations on pointers include:
- the sum of a pointer and an integer
- the difference of a pointer and an integer
- pointer comparison
the difference of two pointers.
Increment/decrement in pointers
assignment operator used in pointers
Comparison in pointers
Two pointers of the same type, p and q, may be compared as long
as both of them point to objects within a single memory block
• Pointers may be compared using the <, >, <=, >=, == , !=
• When you are comparing two pointers, you are comparing the
values of those pointers rather than the contents of memory locations pointed to by these pointers
Void pointers
When a variable is declared as being a pointer to type void it is known as a generic pointer.
Since you cannot have a variable of type void, the pointer will not point to any data and therefore cannot be dereferenced.
It is still a pointer though, to use it you just have to typecast it to another kind of pointer first. Hence the term Generic pointer.
DANGLING POINTER
Dangling pointers arise when an object is deleted or deallocated, without modifying the value of the pointer, so that the pointer still points to the memory location of the deallocated memory
As the system may reallocate the previously freed memory to another process, if the original program then dereferences the (now) dangling pointer, unpredictable behavior may result, as the memory may now contain completely different data
Cause
deleting an object from memory explicitly or by destroying the stack frame on return does not alter associated pointers. The pointer still points to the same location in memory even though the reference has since been deleted and may now be used for other purposes
NULL POINTER
A null pointer is a regular pointer of any pointer type which has a special value that indicates that it is not pointing to any valid reference or memory address. This value is the result of type-casting the integer value zero to any pointer type.int * p;
p = 0; // p has a null pointer value
WILD POINTER
Wild pointers arise when a pointer is used prior to initialization to some known state, which is possible in some programming languages. They show the same erratic behaviour as dangling pointers, though they are less likely to stay undetected.
The wild pointer generates garbage memory location and pendent refernce.