Presentation is loading. Please wait.

Presentation is loading. Please wait.

Department of Electronic & Electrical Engineering Types and Memory Addresses Pointers & and * operators.

Similar presentations


Presentation on theme: "Department of Electronic & Electrical Engineering Types and Memory Addresses Pointers & and * operators."— Presentation transcript:

1 Department of Electronic & Electrical Engineering Types and Memory Addresses Pointers & and * operators

2 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.

3 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.

4 Department of Electronic & Electrical Engineering Typical size of primary data types.

5 Department of Electronic & Electrical Engineering Memory (32 Linux machine)

6 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=3.14159265358979323846264338327950288419716939937510582097494459 23078164062862089986280348253421170679; /* Hmmm most of these digits will be thrown away ? */ double googol=1e100;

7 Department of Electronic & Electrical Engineering A little experiment with the debugger

8 Department of Electronic & Electrical Engineering Address &x is 1004 Address &y 1006 Variables in Memory Memory in bytes 51004 Value of x is 5 char x=5; int y=257; 1 (256) 1 0 1006 y occupies 4 bytes DETAILS DEPEND ON THE MACHINE AND COMPILER

9 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

10 Department of Electronic & Electrical Engineering Illustration --- Pointers int x ; int *ptr=&x ; *ptr=5 5 1024 x is stored at memory location 1024 the ptr variable contains the address of x

11 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)


Download ppt "Department of Electronic & Electrical Engineering Types and Memory Addresses Pointers & and * operators."

Similar presentations


Ads by Google