represents the stoping condition of the loop. It begins the actual loop sequence. If it evaluates to tru, the statement (or statements) associated with the for loop are executed; if it evaluates to false, the loop terminates. In our example, each time that index evaluates to a value less than 10, the statement:
is executed. The third statement:
is shorthand notation for incrementing an arithmetic object by 1. It is equivalent to
It is executed after the statement associated with the for loop (assignment of the element subscripted by index with the value of index). Its execution completes one iteration of the for loop. The sequence repeats itself by once again testing the condition. When the condition evaluates to false, the loop terminates. The second loop operates in reverse order in printing the values.
Although C++ provides built-in support for an array type, that support is limited to the mechanics required to read and write individual elements. C++ does not support the abstraction of an array; there is no support for the operations one might wish to perform on an array, such as the assignment of one array to another, the comparison of two arrays for equality, or askingan array its size. Given two arrays, we cannot, for example, copy one to another as a unit using the assignment operator:
if we wish to assign one array to another, we must program that ourselves, copying each element in turn:
for (int index=0; index <>
array0[index] = array1[index];
Moreover, the array type has no self-knowledge. As we said , it does not know its size, so we must keep track of that independently of the array itself.
This becomes troublesome when we wish to pass arrays as general arguments to functions. In C++, we say that an array, unlike the integer and floating points types, is not a first-class citizen of the language. It is inherited from the C language and reflects the separation of data and the algorithms that operate on that data that are characteristic of the procedure paradigm.
Dynamic Memory Allocation and Pointers
Under C++, objects can be allocated either statically-that is, by the compiler as it processes our program source code-or dynamically-that is, by the compiler as it processes ourprogram source code – or dynamically – that is, by a run-time library invoked during program execution. the promary trade-off between the two methods of memory allocation is efficiency versus flexibility: static memory allocation is considerably more efficient to allocate because it is done prior to the execution of the program the amout and type of memory that we need. We cannot, for example, easily process and store the words of an arbitrary text file using a statically allocated string array. In general, the storage of an unknown number of elements requires the flexibility of dynamic memory allocation.
Until now, all our memory allocation has been static. For example, the definition:
int ival = 1024;
instructs the compiler to allocate sufficient storage to hold any value of type int, associate the name ival with that storage, and then place an initial value of 1024 in that storage. This is all done prior to program execution.
There are two values associated with the object ival: the value it contains – in this case – 1024 – and the address at which that value is stored. In C++, it is possible to access either value. When we write:
int ival2 = ival + 1;
we are accessing the value that ival contains, adding 1 to it, and initializing ival2 with that new value. In our example, ival2 has an initial value of 1025. How do we access and store the memory address?
C++ supports a pointer type to hold the memory address values of objects. For example, to declare a pointer type capable of holding ival’s address value, we write:
// a pointer to an object to type int
int *pint;
C++ predefines a special address-of operator (&) that, when applied to an object, return that object;s address value. Thus, to assign pint to ival’s memory address, we write:
int *pint;
pint = &ival; //assign pint address of ival
To access the actual object pint addresses, we must first dereference pint using the dereference operator (*). For example, here is how we would indirectly add 1 to ival through pint:
//inderectly adding 1 to ival through pint
*pint = *pint + 1;
This is exactly equivalent to the following direct manipulation of ival:
//directly adding 1 to ival
ival = ival + 1;
In this example, there is no real benefit in using the indirect pointer manipulation of ival: it is bot less efficient and more easily misporgrammed that the direct manipuation of ival. It is presented to provide a simple first look at pointers. One of the primary uses of pointers in C++ is the management and manipulation of dynamically allocated memory.
2 Primary Differences Between Static and Dynamic Memory Allocation
1. Static objects are named variables that we manipulate directly, whereas dynamic objects are unnamed varaibles we manipulate indirectly through pointers.
2. The allocation and deallocation of static objects is handled automatically by the compiler; the programmer needs to understant it by need not do anything about it. The allocation and deallocation of dynamic objecs, in contrast, must be managed explicitly by the programmer and, in practice, is considerable more error-prone. It is accomplished through the use of the new and delete expressions.
Objects are allocated dynamically through one of two versions of the new expression. The first instance allocates a single object of a specific type:
int *pint = new int(1024);
allocates an unnamed object of type int, initializes that object to a first value of 1024 and then returns the address of the object in memory. The address is then used to initialize our pointer object, pint. For dynamically allocated memory, our only access is an indirect access through a pointer.
A second version of the new expression allocates an array of elements of a specified type and dimension:
int *pia = new int[4];
allocates an array of four integer elements. Unfortunately, there is no way to specify an explicit initial value for the individual elements of a dynamically allocated array.
A sometimes confusing aspect of allocating a dynamic array is that the value returned is simply a pointer, the same type that is returned for the allocation of a single dynamic object. For example, the difference between pint and pia is that pia holds the address of the first element of the four-element array, whereas pint simply holds the address of the single object.
Deallocate memory using one of two delete expression:
//delete a single object
delete pint;
//delete an array of objects
delete [] pia;
If we forget to delete our dynamically allocated memory, we end up with a memory leak.
A memory leak is a chunk of dynamically allocated memory that we no longer have a pointer to, and thus we cannot return it to the program for later resure.
Source: C++ Primer by Lippman & Lajoie
write c++ array rotation one position to the right programm.