Deep C. Multifile projects Getting it running Data types Typecasting Memory management Pointers. CS-343 Operating Systems

Similar documents
PRINCIPLES OF OPERATING SYSTEMS

Dynamic memory allocation

CS Basics 15) Compiling a C prog.

Berner Fachhochschule Haute cole spcialise bernoise Berne University of Applied Sciences 2

CSCI 2132 Software Development. Lecture 29: Dynamic Memory Allocation

CS 326 Operating Systems C Programming. Greg Benson Department of Computer Science University of San Francisco

CSci 4061 Introduction to Operating Systems. Programs in C/Unix

2 Compiling a C program

Kurt Schmidt. October 30, 2018

Ricardo Rocha. Department of Computer Science Faculty of Sciences University of Porto

Programs. Function main. C Refresher. CSCI 4061 Introduction to Operating Systems

CS 11 C track: lecture 5

Outline. Lecture 1 C primer What we will cover. If-statements and blocks in Python and C. Operators in Python and C

Memory Management. CSC215 Lecture

Reliable C++ development - session 1: From C to C++ (and some C++ features)

Basic C Programming. Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

C Review. MaxMSP Developers Workshop Summer 2009 CNMAT

CSCI-243 Exam 1 Review February 22, 2015 Presented by the RIT Computer Science Community

CS 261 Fall C Introduction. Variables, Memory Model, Pointers, and Debugging. Mike Lam, Professor

Intermediate Programming, Spring 2017*

Structures and Pointers

Memory Management. CS449 Fall 2017

CAAM 420 Fall 2012 Lecture 15. Roman Schutski

Structures. Basics of Structures (6.1) EECS l Now struct point is a valid type. l Defining struct variables: struct point { int x; int y; };

Dynamic Memory Allocation (and Multi-Dimensional Arrays)

High-performance computing and programming I. Introduction to C programming in *NIX

Memory Allocation in C C Programming and Software Tools. N.C. State Department of Computer Science

POINTER AND ARRAY SUNU WIBIRAMA

Midterm Exam Nov 8th, COMS W3157 Advanced Programming Columbia University Fall Instructor: Jae Woo Lee.

o Code, executable, and process o Main memory vs. virtual memory

Contents. Custom data type struct data type Fixed length memory Alias data type

Outline. Briefly review the last class Pointers and Structs Memory allocation Linked lists

C Introduction. Comparison w/ Java, Memory Model, and Pointers

Memory Allocation in C

Topic 6: A Quick Intro To C

Arrays and Memory Management

CMPS 105 Systems Programming. Prof. Darrell Long E2.371

Topic 6: A Quick Intro To C. Reading. "goto Considered Harmful" History

Programming in C First meeting

In Java we have the keyword null, which is the value of an uninitialized reference type

CS61, Fall 2012 Section 2 Notes

Introduction to C. Robert Escriva. Cornell CS 4411, August 30, Geared toward programmers


Course Information and Introduction

Two s Complement Review. Two s Complement Review. Agenda. Agenda 6/21/2011

C-Programming. CSC209: Software Tools and Systems Programming. Paul Vrbik. University of Toronto Mississauga

Luar Topics. C A Low level Programming Language Make A dependency based build system YUYV Representation of Color ZMP Zero Moment Point control

Programming in C week 1 meeting Tiina Niklander

Programming in C First meeting Tiina Niklander

CSE 303 Lecture 8. Intro to C programming

Read: Kernighan & Ritchie

Memory (Stack and Heap)

ch = argv[i][++j]; /* why does ++j but j++ does not? */

Understanding Pointers

Memory Management. a C view. Dr Alun Moon KF5010. Computer Science. Dr Alun Moon (Computer Science) Memory Management KF / 24

ECE 264 Exam 2. 6:30-7:30PM, March 9, You must sign here. Otherwise you will receive a 1-point penalty.

CS201 - Lecture 1 The C Programming Language

C Pointers. Abdelghani Bellaachia, CSCI 1121 Page: 1

Dynamic Memory. Dynamic Memory Allocation Strings. September 18, 2017 Hassan Khosravi / Geoffrey Tien 1

Faculty of Science Final Examination. Computer Science B Introduction to Software Systems

CS240: Programming in C

Programming. Pointers, Multi-dimensional Arrays and Memory Management

CSCI 171 Chapter Outlines

From Java to C. Thanks to Randal E. Bryant and David R. O'Hallaron (Carnegie-Mellon University) for providing the basis for these slides

Programs in memory. The layout of memory is roughly:

CSCI-243 Exam 2 Review February 22, 2015 Presented by the RIT Computer Science Community

ECEN 449 Microprocessor System Design. Review of C Programming. Texas A&M University

Exercise Session 2 Systems Programming and Computer Architecture

THE UNIVERSITY OF WESTERN ONTARIO. COMPUTER SCIENCE 211a FINAL EXAMINATION 17 DECEMBER HOURS

Design and development of embedded systems for the Internet of Things (IoT) Fabio Angeletti Fabrizio Gattuso

Chapter 14. Dynamic Data Structures. Instructor: Öğr. Gör. Okan Vardarlı. Copyright 2004 Pearson Addison-Wesley. All rights reserved.

Class Information ANNOUCEMENTS

#include <stdio.h> int main() { char s[] = Hsjodi, *p; for (p = s + 5; p >= s; p--) --*p; puts(s); return 0;

ECEN 449 Microprocessor System Design. Review of C Programming

Introduction to C. Sean Ogden. Cornell CS 4411, August 30, Geared toward programmers

Dynamic Memory Management. Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

Introduction to C. Ayush Dubey. Cornell CS 4411, August 31, Geared toward programmers

Computer Programming Practice (2008 Winter) Practice 10 C Scoping & Pointer

Overview of today s lecture. Quick recap of previous C lectures. Introduction to C programming, lecture 2. Abstract data type - Stack example

Chapter 11 Introduction to Programming in C

Tutorial 1: Introduction to C Computer Architecture and Systems Programming ( )

Systems Programming and Computer Architecture ( )

CSE au Midterm Exam Nov. 2, 2018 Sample Solution

Dynamic memory allocation (malloc)

CSC 1600 Memory Layout for Unix Processes"

CSE 333 Lecture 6 - data structures

C Compilation Model. Comp-206 : Introduction to Software Systems Lecture 9. Alexandre Denault Computer Science McGill University Fall 2006

Week 9 Part 1. Kyle Dewey. Tuesday, August 28, 12

Dynamically Allocated Memory in C

EPITA SPE C Workshop - D0

C PROGRAMMING Lecture 5. 1st semester

C Bootcamp. September 3rd, 2017

unsigned char memory[] STACK ¼ 0x xC of address space globals function KERNEL code local variables

Introduction to C Programming (Part C) Copyright 2008 W. W. Norton & Company. All rights Reserved

Course organization. Part I: Introduction to C programming language (Week 1-12) Chapter 1: Overall Introduction (Week 1-4)

ELEC 377 C Programming Tutorial. ELEC Operating Systems

Here's how you declare a function that returns a pointer to a character:

Memory and C/C++ modules

CS 31: Intro to Systems Pointers and Memory. Martin Gagne Swarthmore College February 16, 2016

Memory Management I. two kinds of memory: stack and heap

Transcription:

Deep C Multifile projects Getting it running Data types Typecasting Memory management Pointers Fabián E. Bustamante, Fall 2004

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 2

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 */ 3

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); } 4

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; 5

Makefile: Compile the project TARGET=hello SOURCES= hello.c \ main.c CC = gcc CFLAGS = -g -Wall -UHAVE_CONFIG_H -D_GNU_SOURCE $(TARGET): $(SOURCES) $(CC) $(CFLAGS) -o $@ $^ debug: $(TARGET) gdb $(TARGET) clean: rm -f $(TARGET) rm -f *.o rm -f *~ 6

Console Output [sempi@sempi hello]$ make clean rm -f hello rm -f *.o rm -f *~ [sempi@sempi hello]$ ls CVS/ hello.c hello.h main.c Makefile [sempi@sempi hello]$ make gcc -g -Wall -UHAVE_CONFIG_H -D_GNU_SOURCE -o hello hello.c main.c [sempi@sempi hello]$ ls CVS/ hello* hello.c hello.h main.c Makefile [sempi@sempi hello]$./hello hello world! [sempi@sempi hello] 7

Date Types Basic data types (char, int, float, double) Qualifliers (short, long) long int counter; See <limits.h> and <floats.h> Arrays int matrix[1000]; access: matrix[0]... matrix[999] 8

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); 9

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; 10

Memory Management Allocate memory <stdlib.h> Deallocate what you have allocated DON'T rely on the cleanup at program termination Malloc void *malloc(size_t n) Calloc (for arrays) void *calloc(size_t n, size_t size) Free void free(void *ptr) 11

Typecasting Use with care a good source for bugs in your code if you can avoid casts, AVOID IT! Example int *ip ip = (int *) calloc(n, sizeof(int)); free(ip); // free the alocated memory 12

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] */ 13

Strings String constant This is a string Example char *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 14

Strings Array vs. pointers char 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. 15

Tools valgrind memory allocation/deallocation check finds memory leaks lint (splint) code check 16

Bookshelf B. Kernighan and D. Ritchie; The C Programming Language, 2 nd 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). 17