variables in c
play

Variables in C++ The variable C++ Variables Kinds of Variables - PDF document

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,


  1. 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 – sizeof (bool) = 1 int foo; – sizeof (short) = 2 float f = 7.0; – sizeof (int) = 4 char c = ‘d’; – sizeof (unsigned) = 4 – sizeof (float) = 4 – sizeof (double) = 8 – sizeof (long double) = 16 1

  2. Kinds of variables Variables in C++ • Basic variable • Basic variable – Memory associated with a variable with size based on • Pointer variable 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 0x345ABC2 foo 0x345ABC2 – sizeof (unsigned*) = 4 0x675ABC2 f 0x675ABC2 – sizeof (float*) = 4 – sizeof (double* = 4 – sizeof (long double*) = 4 2

  3. Variables in C++ Variables in C++ • Pointer Variables • Address of operator – Dereference operator * – You can always get the address of any variable – If ptr is a pointer or object by using the address of operator &. • 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 Functions Variables in C++ • In C++ function arguments are pass by value: Pointer variable • double f = 7.0; • double *fptr = &f; void foo (int j) int i = 7; { • double fv = (*fptr); foo (i); cout << “Arg is “ << j << endl; 0x675ABC2 fptr 0x675ABC2 cout << i; j = 12; 7 } f void – indicates that a function does not return a value 7 fv Functions Memory Storage Architecture void foo2 (int *j) • Typically, a C++ program maintains 4 int i = 7; { memory areas: int *ia = &i cout << “Arg is “ << *j << endl; foo (ia); *j = 12; Static For global variables } cout << i; For function calls Stack 7 j i 0x675ABC2 0x675ABC2 12 Heap Free store ia 0x675ABC2 Code Executable code 3

  4. 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++ int &foo; // Invalid • Reference variable int i; double f = 7; int &iref = i; // okay double &fref = f; int &ref7 = 7; // Invalid cout << “The value is “ << fref << “ and not “ << (*fref); int &badref = &i; // invalid fref 0x675ABC2 0x675ABC2 f 7 Functions Variables in C++ void foo2 (int &j) • Questions? int i = 7; { foo (i); cout << “Arg is “ << j << endl; cout << i; j = 12; } 7 j i 0x675ABC2 0x675ABC2 12 4

  5. Variable qualifiers Variable qualifiers const int i = 7; // okay • const i = 12; // not okay – A variable that cannot be modified. (oxymoron?) – When used with pointers – cannot modify the data the variable is pointing to. Const pointers and functions Const pointers and functions void foo2 (const int *j) void foo2 (int *j) int i = 7; int i = 7; { { const int *ia = &i int *ia = &i cout << “Arg is “ << *j << endl; cout << “Arg is “ << *j << endl; foo (ia); foo (ia); *j = 12; // Invalid! *j = 12; cout << i; } } 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 extern int globI; declared as static ) int globI = 7; int foo (int i) int globI = 7; main () { { globI = i; main () { … } globI = 12; … … } } File 2 File 1 5

  6. 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. { globI = 12; • Classes can also have static members … – 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 – struct – Static / Stack / heap / Code • Variable Qualifiers – const / static / extern / register / volatile • Have a good weekend. • Questions? 6

Recommend


More recommend