slides deep c.pdf

17
CS-343 Operating Systems Fabián E. Bustamante, Fall 2004 Deep C Multifile projects Getting it running Data types Typecasting Memory management Pointers

Upload: dhananjayan89

Post on 10-Apr-2015

88 views

Category:

Documents


4 download

DESCRIPTION

Uploaded from Google Docs

TRANSCRIPT

Page 1: Slides deep c.pdf

CS-343 Operating SystemsFabián E. Bustamante, Fall 2004

Deep C

Multifile projects

Getting it running

Data types

Typecasting

Memory management

Pointers

Page 2: Slides deep c.pdf

2CS-343 Operating

Systems

Multifile Projects

Give your project a structure

Modularized design

Reuse whole modules vs. copy of code

The longer a file gets the less readable it is

Static variables accessible only within module

Page 3: Slides deep c.pdf

3CS-343 Operating

Systems

hello.h: The Module Declaration

#ifndef __HELLO_H__#define __HELLO_H__

/************Defines and Typedefs*****************************************/#undef EXTERN#ifdef __HELLO_IMPL__#define EXTERN #else#define EXTERN extern#endif

/************Function Prototypes******************************************//*********************************************************************** * Title: Prints some hello world messages * --------------------------------------------------------------------- * Purpose: Greets the whole world. * Input: void * Output: void ***********************************************************************/EXTERN void hello();

#endif /* __HELLO_H__ */

Page 4: Slides deep c.pdf

4CS-343 Operating

Systems

hello.c: The Module Implementation

#define __HELLO_IMPL__

/************System include***********************************************/#include <stdio.h>

/************Private include**********************************************/#include "hello.h"

/************Defines and Typedefs*****************************************/ /* #defines and typedefs should have their names in all caps. * Global variables begin with g. Global constants with k. Local * variables should be in all lower case. When initializing * structures and arrays, line everything up in neat columns. */#define MESSAGE "hello world!"

/**************Implementation***********************************************/

void hello(){ printf("%s\n", MESSAGE);}

Page 5: Slides deep c.pdf

5CS-343 Operating

Systems

main.c: The Main Function

/************Private include**********************************************/#include "hello.h"

/**************Implementation***********************************************/

int main(int argc, char* argv[]){ hello();

// exit with exit code 0 return 0;}

Page 6: Slides deep c.pdf

6CS-343 Operating

Systems

Makefile: Compile the project

TARGET=helloSOURCES= hello.c \ main.c

CC = gccCFLAGS = -g -Wall -UHAVE_CONFIG_H -D_GNU_SOURCE

$(TARGET): $(SOURCES)$(CC) $(CFLAGS) -o $@ $^

debug: $(TARGET)gdb $(TARGET)

clean:rm -f $(TARGET)rm -f *.orm -f *~

Page 7: Slides deep c.pdf

7CS-343 Operating

Systems

Console Output

[sempi@sempi hello]$ make cleanrm -f hellorm -f *.orm -f *~[sempi@sempi hello]$ lsCVS/ hello.c hello.h main.c Makefile[sempi@sempi hello]$ makegcc -g -Wall -UHAVE_CONFIG_H -D_GNU_SOURCE -o hello hello.c main.c[sempi@sempi hello]$ lsCVS/ hello* hello.c hello.h main.c Makefile[sempi@sempi hello]$ ./hellohello world![sempi@sempi hello]

Page 8: Slides deep c.pdf

8CS-343 Operating

Systems

Date Types

Basic data types (char, int, float, double)

Qualifliers (short, long)long int counter;

See <limits.h> and <floats.h>

Arraysint matrix[1000];

access: matrix[0] ... matrix[999]

Page 9: Slides deep c.pdf

9CS-343 Operating

Systems

Advanced Data Types

Structures (example from 'The C Programming Language')

struct point { int x; int y;}

...

struct point pt;

struct point maxpt = { 320, 200 };

printf(“%d,%d”, pt.x, pt.y);

Page 10: Slides deep c.pdf

10CS-343 Operating

Systems

Advanced Data Types (...)

Example from 'The C Programming Language'

/* makepoint: make a point from x and y components */struct point makepoint(int x, int y){ struct point temp;

temp.x = x; temp.y = y; return temp;}

Page 11: Slides deep c.pdf

11CS-343 Operating

Systems

Memory Management

Allocate memory <stdlib.h>– Deallocate what you have allocated

– DON'T rely on the cleanup at program termination

Mallocvoid *malloc(size_t n)

Calloc (for arrays)void *calloc(size_t n, size_t size)

Freevoid free(void *ptr)

Page 12: Slides deep c.pdf

12CS-343 Operating

Systems

Typecasting

Use with care– a good source for bugs in your code

– if you can avoid casts, AVOID IT!

Exampleint *ip

ip = (int *) calloc(n, sizeof(int));

free(ip); // free the alocated memory

Page 13: Slides deep c.pdf

13CS-343 Operating

Systems

Pointers

Use with care– a good source of bugs

– sometimes you can't avoid it

Example ('The C Programming Language')int x = 1; y = 2; z[10];int *ip; /* ip is a pointer to int */

ip = &x; /* ip now points to x */y = *ip; /* y is now 1 */*ip = 0; /* x is now 0 */ip = &z[0]; /* ip now points to z[0] */

Page 14: Slides deep c.pdf

14CS-343 Operating

Systems

Strings

String constant“This is a string”

Examplechar *str;

str = “Hello, world\n”

A string constant is always terminated with \0.– str from above points to some place in the

memory, where you find:Hello, world\n\0

Page 15: Slides deep c.pdf

15CS-343 Operating

Systems

Strings

Array vs. pointerschar astr[] = “Hello”;

char *pstr = “Hello”;

– Array refers always to the same storage (content may change).

– The pointer is initilized to point to a location, where you find the string constant, but the pointer may be modified to point elsewhere.

Page 16: Slides deep c.pdf

16CS-343 Operating

Systems

Tools

valgrind– memory allocation/deallocation check

– finds memory leaks

lint (splint)– code check

Page 17: Slides deep c.pdf

17CS-343 Operating

Systems

Bookshelf

B. Kernighan and D. Ritchie; The C Programming Language, 2nd Edition: 1988– This book doesn't teach you programming, but it

teachs you C!

R. Stevens; Advanced Programming in the Unix Environment

The Internet– You learn the most by looking at other people's code

(you benefit from good and bad examples).