Tema 6: Programming 2 and vectors defined with 2013-2014 and
Index and vectors defined with and 1 2 3 and vectors defined with and
and vectors defined with and Size is constant and known a-priori when the program is implemented Declaration of variables int i=0; char c; float vf[3]={1.0, 2.0, 3.0}; i c vf[0] vf[1] vf[2] 0 1.0 2.0 3.0 1000 1002 1004 1006 1008
and vectors can be used to store large amounts of data which maximum size is unknown when the program is implemented The amount of data to be stored is calculated during the program execution and it can dynamically change can be managed in C++ using pointers defined with and
and vectors defined with and A pointer is a number (long integer) which represents a memory address (location) The type of data contained in the memory address must be specified when the pointer is declared are declared using the character * Examples: int *punteroaentero; char *punteroachar; int *VectorPunterosAEntero[tVECTOR]; double **punteroapunteroareal;
and vectors defined with and *x Content of the address pointed by x &x address of the variable x Example: int i=3; int *pi; pi=&i; // pi=memory address of i *pi = 11; // content of pi=11. Therefore, i = 11 i pi 11 1000 1000 1002 1004 1006 1008
and vectors defined with and : int *pi=&i; // pi stores the address of i The NULL pointer is that one which is not pointing to any variable int *pi=null; Important: pointers without any assigned address should always be set to NULL.
and vectors defined with and Exercise 1 Analyze the output of these code excerpts: int e1; int *p1, *p2; e1 = 7; p1 = &e1; p2 = p1; e1++; (*p2) += e1; cout << *p1; int a=7; int *p=&a; int **pp=&p; cout << **pp;
(1/2) and vectors defined with and can be used to allocate (with new) or deallocate (with delete) dynamic memory Example: double *pd; pd = new double; // memory allocation *pd = 4.75; cout << *pd << endl; // prints 4.75 delete pd; // memory deallocation pd 2000 4.75 1000 1002... 2000 2002 Important: Variables allocated with new are stored in a different memory region than local variables.
(2/2) and vectors defined with and Using new, the compiler allocates memory and returns the initial address of the space allocated If there is not enough free memory for the allocation, new returns NULL Always that dynamic memory is allocated using new, it must be deallocated with delete Warning: After delete, the pointer is not set to NULL by default A pointer can be reused; after deallocating its content, it can be used again to allocate new memory
and vectors (1/2) and vectors defined with and can also be used to create new vectors (arrays) and matrices The memory allocation size must be specified between square brackets To free all the allocated memory, it s necessary to use the square brackets again Example: int *pv; int n=10; pv=new int[n]; // memory allocation for n integers pv[0]=585; // the pointer is used as a vector delete [] pv; // free the alocated memory
and vectors (2/2) can be used for direct access to the components of vectors and matrices. Example: int v[tvector]; int *pv; pv = &(v[7]); *pv = 117; // v[7] = 117; and vectors defined with and
defined with and vectors defined with and New data types can be defined using : int entero; // entero is a type, like int entero a,b; // int a,b; To obtain a clear code, pointers are usually defined with : int *punteroaentero; struct { char c; int i; } Registro, *PunteroRegistro;
and and vectors defined with and The operator -> can be used for accessing the fields of a structure which is being pointed struct { char c; int i; } Registro, *PunteroRegistro; PunteroRegistro pr; pr = new Registro; pr->c = a ; // (*pr).c = a ;
(1/2) and vectors defined with and Passing pointers to functions void f (int *p) { *p=2; } // By value void f2 (int *&p) { // By reference int num=10; p=# } int main() { int i=0; int *p=&i; f(p); f2(p); } // Function call
(2/2) Passing pointers to functions using and vectors defined with and int* PInt; void f (PInt p){ *p=2; } // By value void f2 (PInt &p) { // By reference int num=10; p=# } int main() { int i=0; PInt p=&i; f(p); f2(p); } // Function call
and vectors defined with and Using a pointer without allocated memory or pointing nowhere int *pentero; *pentero = 7; // Error!!! Use a pointer after being deallocated punteroregistro p,q; p = new REGISTRO;... q = p; delete p; q->num =7; // Error!!! Free deallocated memory int *p=&i; delete p;
and vectors defined with and Exercise 2 Given the following structure: struct { char nombre[32]; int edad; } Cliente; Make a program for reading a client (only one) from a binary file. The program must allocate the structure in dynamic memory using a pointer, print its content and finally free the memory.