Introduction to C Programming in Unix Environment - II Abed Asi Extended System Programming...

40
Last lecture - summary Unix environment Data types in C C – simple programs C Arrays – A Glance Makefiles Java vs. C Abed Asi - ESPL 2

Transcript of Introduction to C Programming in Unix Environment - II Abed Asi Extended System Programming...

Page 1: Introduction to C Programming in Unix Environment - II Abed Asi Extended System Programming Laboratory (ESPL) CS Department @ BGU Fall 2014/2015 Some slides.

2

Last lecture - summary

Unix environment Data types in C C – simple programs C Arrays – A Glance Makefiles Java vs. C

Abed Asi - ESPL

Page 2: Introduction to C Programming in Unix Environment - II Abed Asi Extended System Programming Laboratory (ESPL) CS Department @ BGU Fall 2014/2015 Some slides.

3

Today

Memory Arrangement Structs & Arrays Pointers Memory Management in C Memory layout in Unix

Abed Asi - ESPL

Page 3: Introduction to C Programming in Unix Environment - II Abed Asi Extended System Programming Laboratory (ESPL) CS Department @ BGU Fall 2014/2015 Some slides.

4

Definitions

Bit – a binary digit – zero or one

Byte – 8 bits

Abed Asi - ESPL

Page 4: Introduction to C Programming in Unix Environment - II Abed Asi Extended System Programming Laboratory (ESPL) CS Department @ BGU Fall 2014/2015 Some slides.

5

Memory Arrangement

Memory is arranged in a sequence of addressable units (usually bytes) sizeof( <type> ) returns the number of units it takes

to store a type. sizeof(char) = 1 sizeof(int) = 4 (on most of our machines)

Abed Asi - ESPL

Page 5: Introduction to C Programming in Unix Environment - II Abed Asi Extended System Programming Laboratory (ESPL) CS Department @ BGU Fall 2014/2015 Some slides.

6

Memory Arrangement

Abed Asi - ESPL

int main(){

char c; int i,j; double x; …

Page 6: Introduction to C Programming in Unix Environment - II Abed Asi Extended System Programming Laboratory (ESPL) CS Department @ BGU Fall 2014/2015 Some slides.

7

Array

Abed Asi - ESPL

Defines a block of consecutive cells

int main(){ int i; int a[4]; …

Page 7: Introduction to C Programming in Unix Environment - II Abed Asi Extended System Programming Laboratory (ESPL) CS Department @ BGU Fall 2014/2015 Some slides.

8

Arrays - the [ ] operator

Address Computation Examples arr[0] 40+0*sizeof(int) = 40 arr[3] 40+3*sizeof(int) = 52 arr[i] 40+i*sizeof(int) = 40 + 4*i

arr[-1] 40+(-1)*sizeof (int) = 36 // can be the code segment or other variables

Abed Asi - ESPL

int arr[5] = {1, 5, 2, 1 ,3}; /*arr begins at address 40*/

1 5 2 1 3

40 44 48 52 56 60

Page 8: Introduction to C Programming in Unix Environment - II Abed Asi Extended System Programming Laboratory (ESPL) CS Department @ BGU Fall 2014/2015 Some slides.

9

Structs

In C, we can define new types These types are a composite of other

types – structures origin of classes!

Abed Asi - ESPL

struct Complex {double _real;double _imag;

{;

struct Complex c;c._real = 1;c._imag = 0;

sizeof(struct Complex)? =

Page 9: Introduction to C Programming in Unix Environment - II Abed Asi Extended System Programming Laboratory (ESPL) CS Department @ BGU Fall 2014/2015 Some slides.

10

Structs

Contiguously-allocated region of

memory Refer to members within structure by

names Members may be of different types

Abed Asi - ESPL

struct rec { int i; int a[3]; int *p;};

Page 10: Introduction to C Programming in Unix Environment - II Abed Asi Extended System Programming Laboratory (ESPL) CS Department @ BGU Fall 2014/2015 Some slides.

11

Pointers

Declaration<type> *p; p points to objects of type <type>

Pointer value

*p = x;

y = *p;

*p refers to the object p points to

value pointer

&x - the pointer to x

Abed Asi - ESPL

Page 11: Introduction to C Programming in Unix Environment - II Abed Asi Extended System Programming Laboratory (ESPL) CS Department @ BGU Fall 2014/2015 Some slides.

12

Pointers

Abed Asi - ESPL

int main(){ int i,j; int *x; // x points to an integer i = 1; x = &i; j = *x; x = &j; (*x) = 3;}

i j x1

Page 12: Introduction to C Programming in Unix Environment - II Abed Asi Extended System Programming Laboratory (ESPL) CS Department @ BGU Fall 2014/2015 Some slides.

13

Pointers

Abed Asi - ESPL

int main(){ int i,j; int *x; // x points to an integer i = 1; x = &i; j = *x; x = &j; (*x) = 3;}

i j x1

0x0100

0x0100

Page 13: Introduction to C Programming in Unix Environment - II Abed Asi Extended System Programming Laboratory (ESPL) CS Department @ BGU Fall 2014/2015 Some slides.

14

Pointers

Abed Asi - ESPL

int main(){ int i,j; int *x; // x points to an integer i = 1; x = &i; j = *x; x = &j; (*x) = 3;}

i j x1

0x0100

0x01001

Page 14: Introduction to C Programming in Unix Environment - II Abed Asi Extended System Programming Laboratory (ESPL) CS Department @ BGU Fall 2014/2015 Some slides.

15

Pointers

Abed Asi - ESPL

int main(){ int i,j; int *x; // x points to an integer i = 1; x = &i; j = *x; x = &j; (*x) = 3;}

i j x1

0x0100

0x01041

Page 15: Introduction to C Programming in Unix Environment - II Abed Asi Extended System Programming Laboratory (ESPL) CS Department @ BGU Fall 2014/2015 Some slides.

16

Pointers

Abed Asi - ESPL

int main(){ int i,j; int *x; // x points to an integer i = 1; x = &i; j = *x; x = &j; (*x) = 3;}

i j x1

0x0100

0x01043

Page 16: Introduction to C Programming in Unix Environment - II Abed Asi Extended System Programming Laboratory (ESPL) CS Department @ BGU Fall 2014/2015 Some slides.

17

Pointers – the swap function

Abed Asi - ESPL

void swap(int a, int b){ int temp = a; a = b; b = temp;}….int main(){ int x, y; x = 3; y = 7; swap(x, y);….

void swap(int *pa, int *pb){ int temp = *pa; *pa = *pb; *pb = temp;}….int main(){ int x, y; x = 3; y = 7; swap(&x, &y); // x == 7, y == 3…

Does nothing Works

Page 17: Introduction to C Programming in Unix Environment - II Abed Asi Extended System Programming Laboratory (ESPL) CS Department @ BGU Fall 2014/2015 Some slides.

Pointer Arithmetic

int a[4]; int *p = a; char *q = (char *)a; // Explicit cast

Where do p and q point ?

Abed Asi - ESPL18

a[1] a[2] a[3]a[0]

q p

p++; q++;

And now ?

Page 18: Introduction to C Programming in Unix Environment - II Abed Asi Extended System Programming Laboratory (ESPL) CS Department @ BGU Fall 2014/2015 Some slides.

19

Pointers and Arrays

Abed Asi - ESPL

illegalillegal

Arrays are essentially constant pointers

int *p; int a[4]; p = a; // same as p = &a[0] p[1] = 102; // same as *(p+1)=102; *(a+1) = 102; // same p++; // p == a+1 == &a[1] a = p; a++;

Page 19: Introduction to C Programming in Unix Environment - II Abed Asi Extended System Programming Laboratory (ESPL) CS Department @ BGU Fall 2014/2015 Some slides.

20

Pointers and Arrays

Abed Asi - ESPL

int *p;int a[4];sizeof (p) = sizeof (void*) = ?sizeof (a) = ? special case where array is not considered as pointer

Page 20: Introduction to C Programming in Unix Environment - II Abed Asi Extended System Programming Laboratory (ESPL) CS Department @ BGU Fall 2014/2015 Some slides.

21

Pointers and Arrays

Abed Asi - ESPL

int foo( int *p );

and

int foo( int a[] );

Declaring the same interface In both cases, a pointer to int is being passed to the function

foo

Page 21: Introduction to C Programming in Unix Environment - II Abed Asi Extended System Programming Laboratory (ESPL) CS Department @ BGU Fall 2014/2015 Some slides.

22

Pointers to pointers

(1) int i=3;(2) int j=4;(3) int k=5;

3i: 4j: 5k:

ip1: ip2:

ipp:

(4) int *ip1 = &i;

(5) int *ip2 = &j;

(6) int **ipp = &ip1;

Page 22: Introduction to C Programming in Unix Environment - II Abed Asi Extended System Programming Laboratory (ESPL) CS Department @ BGU Fall 2014/2015 Some slides.

23

Pointers to pointers

(1) int i=3;(2) int j=4;(3) int k=5;

3i: 4j: 5k:

ip1: ip2:

ipp:

(4) int *ip1 = &i;

(5) int *ip2 = &j;

(6) int **ipp = &ip1;(7) ipp = &ip2;

Page 23: Introduction to C Programming in Unix Environment - II Abed Asi Extended System Programming Laboratory (ESPL) CS Department @ BGU Fall 2014/2015 Some slides.

24

Pointers to pointers

(1) int i=3;(2) int j=4;(3) int k=5;

3i: 4j: 5k:

ip1: ip2:

ipp:

(4) int *ip1 = &i;

(5) int *ip2 = &j;

(6) int **ipp = &ip1;(7) ipp = &ip2;

(8) *ipp = &k;

Page 24: Introduction to C Programming in Unix Environment - II Abed Asi Extended System Programming Laboratory (ESPL) CS Department @ BGU Fall 2014/2015 Some slides.

25

Memory Management

During run time, variables can be stored in one of three “pools”

Stack Static heap Dynamic heap

Abed Asi - ESPL

Page 25: Introduction to C Programming in Unix Environment - II Abed Asi Extended System Programming Laboratory (ESPL) CS Department @ BGU Fall 2014/2015 Some slides.

26

Memory layout in Unix

Abed Asi - ESPL

program code

Stack

Dynamic heap

Static heap

Each program has its own logical memory

OS maps logical memory to physical memory

A program logical memory is not visible to other programs

Page 26: Introduction to C Programming in Unix Environment - II Abed Asi Extended System Programming Laboratory (ESPL) CS Department @ BGU Fall 2014/2015 Some slides.

27

Stack

Maintains memory during function calls Argument of the function Local variables Call Frame

Variables on the stack have limited “life time”

Abed Asi - ESPL

Page 27: Introduction to C Programming in Unix Environment - II Abed Asi Extended System Programming Laboratory (ESPL) CS Department @ BGU Fall 2014/2015 Some slides.

28

Stack - example

Abed Asi - ESPL

int foo( int a, double f ){ int b;

…}

a

f

b

<call>

Page 28: Introduction to C Programming in Unix Environment - II Abed Asi Extended System Programming Laboratory (ESPL) CS Department @ BGU Fall 2014/2015 Some slides.

29

Stack - example

Abed Asi - ESPL

int foo( int a, double f ){ int b; … { int c; … }…}

a

f

b

<call>

Page 29: Introduction to C Programming in Unix Environment - II Abed Asi Extended System Programming Laboratory (ESPL) CS Department @ BGU Fall 2014/2015 Some slides.

30

Stack - example

Abed Asi - ESPL

int foo( int a, double f ){ int b; … { int c; … }…}

a

f

b

c

<call>

Page 30: Introduction to C Programming in Unix Environment - II Abed Asi Extended System Programming Laboratory (ESPL) CS Department @ BGU Fall 2014/2015 Some slides.

31

Stack - example

Abed Asi - ESPL

int foo( int a, double f ){ int b; … { int c; … } …}

a

f

b

c

<call>

Page 31: Introduction to C Programming in Unix Environment - II Abed Asi Extended System Programming Laboratory (ESPL) CS Department @ BGU Fall 2014/2015 Some slides.

32

Stack - example

Abed Asi - ESPL

int foo( int a, double f ) { int b; … { int c; … } … }

a

f

b

c

<call>

Page 32: Introduction to C Programming in Unix Environment - II Abed Asi Extended System Programming Laboratory (ESPL) CS Department @ BGU Fall 2014/2015 Some slides.

33

Stack

Abed Asi - ESPL

How the stack looks like when a recursive function is invoked ?

what about infinite loops ?

Page 33: Introduction to C Programming in Unix Environment - II Abed Asi Extended System Programming Laboratory (ESPL) CS Department @ BGU Fall 2014/2015 Some slides.

34

Static heap

Abed Asi - ESPL

Memory for global variables (initialized? , unitialized?)

Static variables are defined throughout the execution of the program

int add2(int a, int b) { int c = 0; static int total = 0;   c = a+b; total+= c; return c; }

Page 34: Introduction to C Programming in Unix Environment - II Abed Asi Extended System Programming Laboratory (ESPL) CS Department @ BGU Fall 2014/2015 Some slides.

35

Dynamic heap

Memory that can be allocated and freed by the program during run time

The program controls how much is allocated and when

Limitations based on run-time situation Available memory on the computer

Abed Asi - ESPL

Page 35: Introduction to C Programming in Unix Environment - II Abed Asi Extended System Programming Laboratory (ESPL) CS Department @ BGU Fall 2014/2015 Some slides.

36

Dynamic heap

void *malloc( size_t Size );

Returns a pointer to a new memory block of size Size bytes

Returns NULL if it cannot allocate memory of this size

Abed Asi - ESPL

Page 36: Introduction to C Programming in Unix Environment - II Abed Asi Extended System Programming Laboratory (ESPL) CS Department @ BGU Fall 2014/2015 Some slides.

37

How we use it?

void *malloc( size_t Size );

int* iptr = (int*) malloc(sizeof(int));

struct Complex* complex_ptr = (struct Complex*)

malloc(sizeof(struct Complex));

Page 37: Introduction to C Programming in Unix Environment - II Abed Asi Extended System Programming Laboratory (ESPL) CS Department @ BGU Fall 2014/2015 Some slides.

38

De-allocating memory

void free( void *p );

Returns the memory block pointed by p to the pool of unused memory

No error checking! If p was not allocated by malloc or was

free-ed before, undefined behavior

Page 38: Introduction to C Programming in Unix Environment - II Abed Asi Extended System Programming Laboratory (ESPL) CS Department @ BGU Fall 2014/2015 Some slides.

39

Further knowledge

Read manual page of malloc calloc realloc free

Page 39: Introduction to C Programming in Unix Environment - II Abed Asi Extended System Programming Laboratory (ESPL) CS Department @ BGU Fall 2014/2015 Some slides.

40

Memory layout in Unix

Abed Asi - ESPL

program code

Stack

Dynamic heap

Static heap

Each program has its own logical memory

OS maps logical memory to physical memory

A program logical memory is not visible to other programs

Page 40: Introduction to C Programming in Unix Environment - II Abed Asi Extended System Programming Laboratory (ESPL) CS Department @ BGU Fall 2014/2015 Some slides.

41

Memory layout in Unix

#include <stdio.h>#include <stdlib.h>

int global_variable;

int main(int argc, char **argv) { int local_variable; static int static_variable; int *dynamic_variable =

(int*)malloc(sizeof(int));

printf("addresses of:\n""\tfunction main: 0x%08lx\n""\tglobal variable: 0x%08lx\n""\tlocal variable: 0x%08lx\n""\tstatic variable: 0x%08lx\n""\tdynamic memory: 0x%08lx\n",(unsigned long)main,(unsigned long)&global_variable,(unsigned long)&local_variable,(unsigned long)&static_variable,(unsigned long)dynamic_variable);return 0;}

Abed Asi - ESPL

addresses of:function main: 0x080483f4global variable: 0x0804a018local variable: 0xbf806598static variable: 0x0804a022dynamic memory: 0x08c19008