C++ Variables Variables in C++ the Variable the Variable
Total Page:16
File Type:pdf, Size:1020Kb
Variables in C++ • The variable C++ Variables • Kinds of Variables • Memory storage • Variable qualifiers The variable The variable • A variable declaration is a request for space • Basic data types in memory – int, short, long, unsigned – Memory associated with a variable with size – bool based on the kind of the variable. – char – Variable declarations are “executable” – float statements – double • Memory is allocated when declaration is made The variable The variable Variable declarations: • Data type sizes (using CC on Sun) – sizeof (char) = 1 int foo; – sizeof (bool) = 1 float f = 7.0; – sizeof (short) = 2 char c = ‘d’; – sizeof (int) = 4 – sizeof (unsigned) = 4 – sizeof (float) = 4 – sizeof (double) = 8 – sizeof (long double) = 16 1 Kinds of variables Variables in C++ • Basic variable • Basic variable • Pointer variable – Memory associated with a variable with size based on the data type of the variable • Reference variable int foo; double f = 7.0; double ff = f; foo f ff Variables in C++ Variables in C++ • Pointer Variables Pointer variable – Stores the memory address of an object. int *foo; – Can have pointers to basic data types. float *f = 7.0; // Invalid – C++ has no garbage collection! float *g = 0; // okay – NULL pointer takes value 0. float *h = 0x12345; // illegal!! The variable Variables in C++ • Data type sizes (using CC on Sun) Pointer variable – sizeof (char *) = 4 int *foo; – sizeof (bool*) = 4 double *f; – sizeof (short*) = 4 – sizeof (int*) = 4 foo 0x345ABC2 0x345ABC2 – sizeof (unsigned*) = 4 – sizeof (float*) = 4 f 0x675ABC2 0x675ABC2 – sizeof (double* = 4 – sizeof (long double*) = 4 2 Variables in C++ Variables in C++ • Address of operator • Pointer Variables – You can always get the address of any variable – Dereference operator * or object by using the address of operator &. – If ptr is a pointer • float f = 7.0; • i.e A variable whose contents is a memory address • float *fptr = &f; – then *ptr refers to the object or data item that is pointed to by ptr • Can be interpreted as: – The data item or object at ptr – The object or data item pointed to by ptr Variables in C++ Functions Pointer variable • In C++ function arguments are pass by value: • double f = 7.0; • double *fptr = &f; int i = 7; void foo (int j) • double fv = (*fptr); { foo (i); cout << “Arg is “ << j << endl; fptr 0x675ABC2 0x675ABC2 cout << i; j = 12; f 7 } void – indicates that a function does not return a value fv 7 Functions Memory Storage Architecture int i = 7; void foo2 (int *j) • Typically, a C++ program maintains 4 { memory areas: int *ia = &i cout << “Arg is “ << *j << endl; foo (ia); *j = 12; Static For global variables cout << i; } Stack For function calls i 127 0x675ABC2 0x675ABC2 j Heap Free store ia 0x675ABC2 Code Executable code 3 Heap storage Variables in C++ • To allocate a variable on the heap, use new • Reference Variables – new returns a pointer to the newly – Alias for an already existing object allocated space for the variable. – Usually used to pass function arguments by float *f = new float; reference. (*f) = 7.0; – Syntactically, references are treated like basic – All variables allocated on the heap using variables, yet they do contain memory new must be deallocated using delete. addresses. f 0x675ABC2 0x675ABC2 7 Variables in C++ Variables in C++ • Reference variable int &foo; // Invalid double f = 7; int i; double &fref = f; int &iref = i; // okay cout << “The value is “ << fref << int &ref7 = 7; // Invalid “ and not “ << (*fref); int &badref = &i; // invalid fref 0x675ABC2 0x675ABC2 f 7 Functions Variables in C++ int i = 7; void foo2 (int &j) • Questions? { foo (i); cout << “Arg is “ << j << endl; cout << i; j = 12; } i 127 0x675ABC2 0x675ABC2 j 4 Variable qualifiers Variable qualifiers • const const int i = 7; // okay – A variable that cannot be modified. i = 12; // not okay (oxymoron?) – When used with pointers – cannot modify the data the variable is pointing to. Const pointers and functions Const pointers and functions int i = 7; void foo2 (const int *j) int i = 7; void foo2 (int *j) { const int *ia = &i { int *ia = &i cout << “Arg is “ << *j << endl; foo (ia); cout << “Arg is “ << *j << endl; foo (ia); *j = 12; // Invalid! cout << i; *j = 12; cout << i; } } invalid Global variables Extern • All variables defined outside a function are global: • Used to refer to global variables defined – Global variables are stored in static memory. elsewhere. – Global variables are accessible by all (except when declared as static) int globI = 7; extern int globI; int globI = 7; main () int foo (int i) { { main () { … } globI = 12; globI = i; … … } } File 1 File 2 5 Static Static • Static variables are also stored in static static int globI = 7; memory • static limits the scope of a variable to a main () file or function. { • Classes can also have static members globI = 12; … – But more on that when we get to classes. } Static Compiler hints • volatile – Indicates that the variable can be changed in int foo (int i) unseen ways { • E.g. Changed by another thread static int globI = i; • Hint to compiler not to optimize away … • register } – Hint to compiler to place variable in computer register – Cannot take address of a register variable Summary Next time • Variables are a request for memory to store data • Aggregate data structures • Variable types –Arrays – Basic / Pointer / Reference – union • Memory Organization – Static / Stack / heap / Code – struct • Variable Qualifiers – const / static / extern / register / volatile • Have a good weekend. • Questions? 6.