Presentation is loading. Please wait.

Presentation is loading. Please wait.

COM S 326X Deep C Programming for the 21st Century Prof. Rozier

Similar presentations


Presentation on theme: "COM S 326X Deep C Programming for the 21st Century Prof. Rozier"— Presentation transcript:

1 COM S 326X Deep C Programming for the 21st Century Prof. Rozier
Unit 3 – Variables, Continued

2 Declarations

3 Types Only four basic types in C int char
float – antiquated, bad practice to use (usually) double

4 Variable Modifiers All variables can be modified with keywords and qualifiers. Data type modifiers long and short are storage modifiers which request more, or less memory. Compilers are not guaranteed to honor long and short modifiers. unsigned and signed change the representation.

5 Const The const qualifier indicates a variable which:
Must be initialized Cannot be changed once initialized

6 Static The static keyword does a few things.
It ensures the variable is initialized to 0x0 unless otherwise specified It makes a variable’s scope restricted to the current file. When it leaves scope, it remains in memory, and if it ever returns to scope, will retain its previous value.

7 Extern The extern keyword indicates a variable is declared in another file that may, or may not, have been #included directly.

8 Volatile The volatile keyword indicates to the compiler that the variable can be changed by external entities other than the program itself. Its purpose is prevent the optimizer from removing an apparently unused variable.

9 Auto The auto keyword informs the compiler that the variable should automatically be created when in scope, and destroyed when out of scope. This is the default behavior. auto signed int x Is the same as int x

10 Register A particularly dangerous keyword, the register keyword attempts to bind a variable directly to a register. Considered bad practice. The compiler is usually better at deciding this than you are.

11 Variables and Memory Except for register variables, all other variables live in memory. 0x Text (Code) Data Heap 0xffffffff Stack

12 Variable allocation and scoping
Program initialized global variables main() $rsp local variables (main) $rbp

13 Variable allocation and scoping
Program initialized global variables main() foo() $rsp local variables (foo) $rbp local variables (main)

14 Variable allocation and scoping
Program initialized global variables main() foo() $rsp foo() local variables (foo) $rbp local variables (foo) local variables (main)

15 Variable allocation and scoping
Program initialized global variables main() foo() $rsp local variables (foo) $rbp local variables (main)

16 Variable allocation and scoping
Program initialized global variables main() $rsp local variables (main) $rbp

17 Variable allocation and scoping
Program initialized global variables main() bar() $rsp local variables (bar) $rbp local variables (main)

18 Variable allocation and scoping
Program initialized global variables main() foo() $rsp local variables (foo) $rbp local variables (main)

19 Variables and Memory Except for register variables, all other variables live in memory. We can also declare variables which take as their type memory address information.

20 Pointers To declare a variable of a type, such as int: int x;
To declare a variable which holds an address of an int we use the pointer operator ‘*’: int *x;

21 Pointers The following declaration: int* x, y, z;
Is not the same as this: int *x, *y, *z; This is why we always associate the * operator, or the dereference operator with the variable and not the type. Think of: int *x as meaning “*x is of type int”.

22 Pointers Two special unary operators: The address operator: ‘&’
The dereferencing operator: ‘*’ The address operator finds the storage location of whatever it is applied to: The address of x: &x The dereferencing operator finds what is stored at a given address: The value of what is at the address stored in x: *x

23 Pointers The ‘*’ and ‘&’ operators can be applied recursively:
int **x; A pointer to a pointer to an int.

24 Pointer exercises int x = 5; int *y; y = &x; y++; printf(“%p\n”, y);
printf(“%p\n”, &x); printf(“%d\n”, x); printf(“%d\n”, *y); printf(“%d\n”, *x);

25 Pointers We can use pointers to refer to variables that have been allocated by the compiler, statically, or dynamically, or we can use them to allocate dynamic memory from the heap. #include <stdlib.h> int *x; x = malloc(sizeof(int));

26 Passing Pointers C functions are called by reference.
void foo(int x) { x++; } int main(void) { int x = 5; foo(x);

27 Passing Pointers If we want to change a value, we should pass a pointer to x. void foo(int *x) { *x = *x + 1; } int main(void) { int x = 5; foo(&x);

28 Homework 2 is posted Due next Thursday, September 7th.


Download ppt "COM S 326X Deep C Programming for the 21st Century Prof. Rozier"

Similar presentations


Ads by Google