How to: Implement Arithmetic of Pointers in C++

1 comment - This post in romanian

In C++ programming language, memory areas can be accessed indirectly through pointer type variable [...]. Also, available with a memory address, it ensures movement on memory areas from the memory address stored in the variable pointer. This is ensured by arithmetic operators and it is called arithmetic of pointers.

Depending on the nature of arithmetic operator, the following shiftings on the memory areas are allowed:

  • Forward moving: it is ensured by the operators ++ and +; operator ++ causes alteration of the memory address for the variable that is applied by increasing the value sizeof (TipDataPointer) for stored address; the new address points to a memory area having sizeof (TipDataPointer) bytes bigger than previous address; operator + does not cause change of the memory address for operand pointer variable and it is a component of an expression that contains more than one operand as pointer variable; at least one operand (except pointer variable) indicates the number of items that increase the memory address stored in the pointer variable; product of this value and sizeof(TipDataPointer) represents the number of bytes that increases the memory referenced by the new address.

Examples:

int *pi;
pi=new int;
*pi=2;//initializing the content referred by pointer pi 
pi++;//value 4 increases the memory address of variable pi, \
for increasing the memory address in pi with 1 element of type int; \
new address is overwritten in the variable pi and it refers heap memory \
area with sizeof(int) bytes bigger than previous address; new value int \
referred by pointer pi is a residual one
int *pj;
pj=pi+1;//memory address value of pi does not change; \
memory address stored in the pj is the address in variable \
pi increased by the value 4 for increasing the address by \
1 element of type int; after assigning, pj refers a memory area \
with sizeof(int) bytes after the area referred by pi and it has a \
residual value
  • Back moving: provided by operators - - and -; operator - - leads to alteration of the memory address for the variable to which it applies by subtracting the value sizeof(TipDataPointer) from stored address; the new address points to a memory area having sizeof(TipDataPointer) bytes smaller than previous address; operator - does not lead to changes for memory address of the pointer variable as operand and it is used in an expression; if at least one operand is not pointer variable then this value indicates the value that reduces the memory address stored in the pointer variable; the product of this value and sizeof(TipDataPointer) represents the number of bytes that reduces the memory referenced by the new address.

Examples:

int *pi;
pi=new int;
*pi=2;//initializing the content referred by pointer pi \
pi--;//it is decreased the memory address in pi with the \
value 4 as decreasing the memory address with 1 element \
of type int; new address is overwritten in the variable pi and refers \
an heap memory area with sizeof (int) bytes smaller than \
previous address; the new value int referred through pointer pi \
is a residual value
int *pj;
pj=pi-1;//value of memory address of pi does not change \
memory address stored by pj is the address in pi decreased \
by value 4 as decreasing of the memory address with 1 element \
of type int; after assigning, pj refers to a memory area with sizeof (int) \
bytes under the content referenced by pi and it has a residual value
pj=pi+1;//memory address value of pi does not change; \
memory address stored by pj conteins the initial value of \
address stored in pi when heap memory allocation is done; \
pointer variable pj refers to the heap memory of type int, \
having the content as value 2

Other arithmetic operations allowed on memory areas in C + + programming language:

  • Difference between two pointer variables within the same series of values: specifies the number of elements of TipDataPointer type contained between operands of the difference expression;

Example:

int *pi,*pj;
pi=new int [10];//allocates in heap memory space to store \
10 values of type int each 
pj=pi+6;//pj contains the element address with displacement \
6 (rank 7 range of values)
int n=pj-pi;//n contains the number of elements \
of type int between pi and pj, respectively the absolute value 6; \
the integer value sign indicates positive for pentru pj>pi \
and si negative for pj<pi
  • Adding two pointer variable is not allowed, being generated a compiler error.

, , , , , , , , ,