Data Structures - 02. C Review, I

23
Universidade Federal da Paraíba Centro de Informática The C Programming Language: Part I Lecture 2 1107186 – Estruturas de Dados Prof. Christian Azambuja Pagot CI / UFPB

description

Slide da cadeira de Estrutura de Dados, ministrado pelo Prof. Dr. Christian Pagot, na Universidade Federal da Paraíba.

Transcript of Data Structures - 02. C Review, I

Page 1: Data Structures - 02. C Review, I

Universidade Federal da ParaíbaCentro de Informática

The C Programming Language:Part I

Lecture 2

1107186 – Estruturas de Dados

Prof. Christian Azambuja PagotCI / UFPB

Page 2: Data Structures - 02. C Review, I

2Universidade Federal da ParaíbaCentro de Informática

What is C?

● C is a imperative (procedural) general-purpose programming language.

● Example:

Page 3: Data Structures - 02. C Review, I

3Universidade Federal da ParaíbaCentro de Informática

Where it came from?

● C was developed in 1973 at AT&T Bell Labs by Ken Thompson (left) and Dennis Ritchie (right):

Jargon File

Julesmazur (Wikipedia.org)

Page 4: Data Structures - 02. C Review, I

4Universidade Federal da ParaíbaCentro de Informática

Where it came from?

● “The Development of the C Language”, by Dennis Ritchie:– http://cm.bell-labs.com/cm/cs/who/dmr/chist.html

Page 5: Data Structures - 02. C Review, I

5Universidade Federal da ParaíbaCentro de Informática

Features of the C Lang.

● C is imperative (procedural).– Instructions define the actions of the processor.

● C instructions map easily to machine instructions.

● C is meant to be cross-platform.– Source code can be compiled to different hardware with

minimal modifications.

● C uses lexical scoping.– Variable scope defined by its position in the source

code.

Page 6: Data Structures - 02. C Review, I

6Universidade Federal da ParaíbaCentro de Informática

Features of the C Lang. (cont.)

● C uses static type system.– Types are checked during compile time.

● C supports recursion.– A function can call itself.

● In C, function parameters are passed by value.– Copies

● C is weakly typed.– Casting.

Page 7: Data Structures - 02. C Review, I

7Universidade Federal da ParaíbaCentro de Informática

C Data Types

● Basic types.● Structured types.● User defined types.

Page 8: Data Structures - 02. C Review, I

8Universidade Federal da ParaíbaCentro de Informática

Basic Types

● char● int● float● double● pointers

Wikipedia

charsigned charunsigned charshortshort intsigned shortsigned short intunsigned shortunsigned short intintsigned intunsignedunsigned intlong

long intsigned longsigned long intunsigned longunsigned long intlong longlong long intsigned long longsigned long long intunsigned long longunsigned long long intfloatdoublelong double

Optional specifiers*

● signed● unsigned● short● long

*may not apply to all numeric types.

Page 9: Data Structures - 02. C Review, I

9Universidade Federal da ParaíbaCentro de Informática

Pointers

● A variable is a memory location into which data can be stored.

● C allows the programmer to access either the variable location or its contents.

Integer variable.

Pointer to an integer variable.

px receives the address of x.

int x;int *px; 

px = &x;

C code excerpt:

Page 10: Data Structures - 02. C Review, I

10Universidade Federal da ParaíbaCentro de Informática

Pointers

int x;int *px; 

x = 25;px = &x;

C code excerpt:

x

px

RAM Memory

Addr. Val.0 ...

1 ...

2 ...

3 ...

4 ...

100 ...

101 ...

102 ...

103 ...

104 ...

105 ...

106 ...

107 ...

. . .

1 byte

1 byte

Page 11: Data Structures - 02. C Review, I

11Universidade Federal da ParaíbaCentro de Informática

Pointers

int x;int *px; 

x = 25;px = &x;

C code excerpt:

PC

RAM Memory

x

Addr. Val.0 ...

1 ...

2 ...

3 ...

4 ...

px 100 ...

101 ...

102 ...

103 ...

104 ...

105 ...

106 ...

107 ...

. . .

00000000 00000000 00000000 000110012

Page 12: Data Structures - 02. C Review, I

12Universidade Federal da ParaíbaCentro de Informática

Pointers

int x;int *px; 

x = 25;px = &x;

C code excerpt:

PC

RAM Memory

0 ...

1 00011001

2 00000000

3 00000000

4 00000000

x

Addr. Val.

px 100 ...

101 ...

102 ...

103 ...

104 ...

105 ...

106 ...

107 ...

. . .

00000000 00000000 00000000 000110012

Little endian

Page 13: Data Structures - 02. C Review, I

13Universidade Federal da ParaíbaCentro de Informática

Pointers

int x;int *px; 

x = 25;px = &x;

C code excerpt:

PC

RAM Memory

x

px

Addr. Val.

100 ...

101 ...

102 ...

103 ...

104 ...

105 ...

106 ...

107 ...

. . .

0 ...

1 00011001

2 00000000

3 00000000

4 00000000

Page 14: Data Structures - 02. C Review, I

14Universidade Federal da ParaíbaCentro de Informática

Pointers

int x;int *px; 

x = 25;px = &x;

C code excerpt:

PC

RAM Memory

x

px

Addr. Val.

100 ...

101 ...

102 ...

103 ...

104 ...

105 ...

106 ...

107 ...

. . .

0 ...

1 00011001

2 00000000

3 00000000

4 00000000

00000000 00000000 00000000 0000000000000000 00000000 00000000 00000001

2

Page 15: Data Structures - 02. C Review, I

15Universidade Federal da ParaíbaCentro de Informática

Pointers

int x;int *px; 

x = 25;px = &x;

C code excerpt: RAM Memory

x

px

Addr. Val.

100 00000001

101 00000000

102 00000000

103 00000000

104 00000000

105 00000000

106 00000000

107 00000000

. . .

0 ...

1 00011001

2 00000000

3 00000000

4 00000000PC

00000000 00000000 00000000 0000000000000000 00000000 00000000 00000001

2

Page 16: Data Structures - 02. C Review, I

16Universidade Federal da ParaíbaCentro de Informática

Why different pointer types?

● Mainly due two reasons:– To reduce the occurrence of bugs.

– To allow for a pointer arithmetic!!!

Page 17: Data Structures - 02. C Review, I

17Universidade Federal da ParaíbaCentro de Informática

Pointer Arithmetic

int *px; 

*px = 2;*(px+1)=­1; // or px[1]=­1*(px+2)=5;  // or px[2]=5

C code excerpts:

char *py; 

*py = 2;*(py+1)=­1; // or py[1]=­1*(py+2)=5;  // or py[2]=5

C code excerpts:

12 13 14 15 16 17 18 19 20. . . . . .

pxpoints to

pypoints to

Hey! Have you asked permissionto write to all those

memory positions???

Addr.

1 byte RAM memory

(px+1)points to

(px+2)points to

12 13 14 15 16 17. . . . . .

RAM memory

Addr.

1 byte

(py+2)points to

(py+1)points to

Page 18: Data Structures - 02. C Review, I

18Universidade Federal da ParaíbaCentro de Informática

Memory Allocation Policies

● Memory can be allocated:– Automatically.

– Statically.

– Dynamically.

Page 19: Data Structures - 02. C Review, I

19Universidade Federal da ParaíbaCentro de Informática

Automatic Variables

● Memory is automatically allocated and deallocated when the program execution 'enters' or 'leaves' the lexical variable scope.

● Example:

Page 20: Data Structures - 02. C Review, I

20Universidade Federal da ParaíbaCentro de Informática

Static memory Allocation

● Memory is allocated at compile time.

● Memory remains allocated during the entire program life time.

● May have limited scope (e.g. static local).

C code example...(static_var.c)

Page 21: Data Structures - 02. C Review, I

21Universidade Federal da ParaíbaCentro de Informática

Dynamic Memory Allocation

● Approximated process memory layout (in the virtual memory space):

Code segment

Data

.bss

Heap

Stack

Instructions.

Initialized global, const and static vars.

Uninitialized vars.

Dynamic memoryallocation

Local vars, return Addresses, etc.

Process

Addr: 0x00000000

Addr: 0x0000FFFF

Page 22: Data Structures - 02. C Review, I

22Universidade Federal da ParaíbaCentro de Informática

Allocating Space in the Heap

● C functions allows for the allocation/deallocation of heap memory:– malloc()

● Allocate X bytes and returns a pointer to the first byte of allocated memory.

– calloc()● Allocate space for X array elements, initializes to zero, and return a pointer

to the allocated memory.

– free()● Deallocate previously allocated memory.

– realloc()● Change the size of previously allocated memory.

Page 23: Data Structures - 02. C Review, I

23Universidade Federal da ParaíbaCentro de Informática

Building an Array with malloc()

C code example...(malloc.c)