Department of Electronic & Electrical Engineering Types and Memory Addresses Pointers & and * operators
Department of Electronic & Electrical Engineering Types of variables. Variables are declared as a given type. Variables must be declared before use. The type tells the compiler: How much memory is needed to store the variable. What operations can be performed on the variable.
Department of Electronic & Electrical Engineering Size of data types The amount of memory required the different data types can vary form one machine to another. The next slide shows typical sizes.
Department of Electronic & Electrical Engineering Typical size of primary data types.
Department of Electronic & Electrical Engineering Memory (32 Linux machine)
Department of Electronic & Electrical Engineering What data type should I use? Depends what precision is required and range. int numberOfFingers=10; float height=1.8; char x=10; /* char stores 8bits or a byte of data */ /* typically used to store characters */ /* encoded as byte values */ double pi= ; /* Hmmm most of these digits will be thrown away ? */ double googol=1e100;
Department of Electronic & Electrical Engineering A little experiment with the debugger
Department of Electronic & Electrical Engineering Address &x is 1004 Address &y 1006 Variables in Memory Memory in bytes Value of x is 5 char x=5; int y=257; 1 (256) y occupies 4 bytes DETAILS DEPEND ON THE MACHINE AND COMPILER
Department of Electronic & Electrical Engineering Address of operator and pointers. The ampersand operator & can be used to find the address of a variable; int x; int *ptr; /* ptr is a pointer variable */ /* 4 bytes for 32 bit */ /* 8 bytes for 64 bit */ ptr = &x; /* ptr points at x */ *ptr = 5; /* dereferencing a pointer */ * " contents of the memory location... " & " address of... " referencing dereferencing
Department of Electronic & Electrical Engineering Illustration --- Pointers int x ; int *ptr=&x ; *ptr= x is stored at memory location 1024 the ptr variable contains the address of x
Department of Electronic & Electrical Engineering Recap Variables have a type stored at an address in memory. Different types need different amounts of memory & address of operator (referencing) * contents of address operator (dereferencing)