. Plab – Tirgul 3 Makefiles, Memory errors, Variables’ scope.

Slides:



Advertisements
Similar presentations
Dynamic Allocation and Linked Lists. Dynamic memory allocation in C C uses the functions malloc() and free() to implement dynamic allocation. malloc is.
Advertisements

SEE C GO Provisional Title. Syntax Types int, float, double, char, void Identifiers foo Operators + - * / ^ Delimiters ; {} () “” ‘’ Keywords return,
Chris Riesbeck, Fall 2007 Dynamic Memory Allocation Today Dynamic memory allocation – mechanisms & policies Memory bugs.
The make Utility Programming Tools and Environments Winter 2006.
Memory allocation CSE 2451 Matt Boggus. sizeof The sizeof unary operator will return the number of bytes reserved for a variable or data type. Determine:
Dynamic Data Structures H&K Chapter 14 Instructor – Gokcen Cilingir Cpt S 121 (July 26, 2011) Washington State University.
David Notkin Autumn 2009 CSE303 Lecture 20 static make.
Dynamic Memory Allocation in C++. Memory Segments in C++ Memory is divided in certain segments – Code Segment Stores application code – Data Segment Holds.
Compilation & linkage.h read.h.c read.c.c main.c.c list.c.h list.h prog1 Linkage: g++ read.o main.o list.o –o prog1.o main.o.o list.o.o read.o Compilation:
. Plab – Tirgul 5 pointers to pointers, libraries.
1 CSSE 332 Standard Library, Storage classes, and Make.
. Plab – Tirgul 4 structs & arrays, file I/O, debugging memory errors.
Pointers. Addresses in Memory When a variable is declared, enough memory to hold a value of that type is allocated for it at an unused memory location.
Plab – Tirgul 3 Makefiles, Libraries, Debugging and Common Bugs.
C and Data Structures Baojian Hua
Lecture 3 Interfaces Pointers to Functions Memory bugs, File I/O Variables – the special kind.
Introduction to C Programming in Unix Environment - I Abed Asi Extended System Programming Laboratory (ESPL) CS BGU Fall 2013/2014.
. Memory Management. Memory Organization u During run time, variables can be stored in one of three “pools”  Stack  Static heap  Dynamic heap.
Memory Layout C and Data Structures Baojian Hua
Dynamic Allocation and Linked Lists. Dynamic memory allocation in C C uses the functions malloc() and free() to implement dynamic allocation. malloc is.
Xin Liu Sep 16, Introduction Xin (Shane) Liu PhD Candidate in Computer Science Research Area: Computer Graphics Tutorial Page: pages.cpsc.ucalgary.ca/~liuxin/CPSC453.
Outline Midterm results Static variables Memory model
Dynamic Memory Allocation Conventional array and other data declarations An incorrect attempt to size memory dynamically Requirement for dynamic allocation.
Introduction to C Programming CE Lecture 7 Compiler options and makefiles.
Stack and Heap Memory Stack resident variables include:
Homework K&R chapter 4. HW3: ASCII Hex to Integer int axtoi (char s[ ]) { int i, n, flag; n = 0; flag = 1; for ( i = 0; flag; i++) /* for (i = 0; ; i++)
CS 11 C++ track: lecture 4 Today: More on memory management the stack and the heap inline functions structs vs. classes.
Pointers review Let a variable aa be defined as ‘int *aa;’, what is stored in aa? Let a variable aa be defined as ‘int ** aa;’ what is stored in aa? Why.
Dynamic Memory Allocation. Domain A subset of the total domain name space. A domain represents a level of the hierarchy in the Domain Name Space, and.
Dynamic memory allocation and Pointers Lecture 4.
Current Assignments Start Reading Chapter 6 Project 3 – Due Thursday, July 24 Contact List Program Homework 6 – Due Sunday, July 20 First part easy true/false.
Chapter 14 Memory API Chien-Chung Shen CIS, UD
Lecture 3 Classes, Structs, Enums Passing by reference and value Arrays.
Exam / Homework Exam 1 Starting K&R chapter 4 tonight
Functions & Pointers in C Jordan Erenrich
1 Lecture07: Memory Model 5/2/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
CMSC 202 Computer Science II for Majors. CMSC 202UMBC Topics Memory management operators Dynamic memory Project 2 questions.
Modular Programming. Introduction As programs grow larger and larger, it is more desirable to split them into sections or modules. C allows programs to.
EEL 3801 C++ as an Enhancement of C. EEL 3801 – Lotzi Bölöni Comments  Can be done with // at the start of the commented line.  The end-of-line terminates.
C code organization CSE 2451 Rong Shi. Topics C code organization Linking Header files Makefiles.
Multi-dimensional Arrays and other Array Oddities Rudra Dutta CSC Spring 2007, Section 001.
Week 4 - Friday.  What did we talk about last time?  Some extra systems programming stuff  Scope.
Announcements Partial Credit Due Date for Assignment 2 now due on Sat, Feb 27 I always seem to be behind and get tons of daily. If you me and.
Dynamic Memory Management & Static Class Members Lecture No 7 Object Oriented Programming COMSATS Institute of Information Technology.
Revisiting building. Preprocessing + Compiling 2 Creates an object file for each code file (.c ->.o) Each.o file contains code of the functions and structs.
Multiple file project management & Makefile
Lecture 3 Translation.
The make utility (original presentation courtesy of Alark Joshi)
CSE 303 Lecture 17 Makefiles reading: Programming in C Ch. 15
Overview 4 major memory segments Key differences from Java stack
Introduction to Programming
CSE 3302 Programming Languages
Day 04 Introduction to C.
Checking Memory Management
CSCI206 - Computer Organization & Programming
Instructor: Ioannis A. Vetsikas
This pointer, Dynamic memory allocation, Constructors and Destructor
Storage.
Announcements Homework #7 due Monday at 3:00pm
Overview 4 major memory segments Key differences from Java stack
Dynamic Memory Allocation
Memory Allocation CS 217.
Dynamic Memory A whole heap of fun….
C Programming Lecture-8 Pointers and Memory Management
Name Binding and Object Lifetimes
Programming Languages and Paradigms
Homework K&R chapter 4.
Makefiles, GDB, Valgrind
SPL – PS2 C++ Memory Handling.
Abstract Data Types and Stacks
Presentation transcript:

. Plab – Tirgul 3 Makefiles, Memory errors, Variables’ scope

Compilation & linkage.h read.h.c read.c.c main.c.c list.c.h list.h prog1 Linkage: g++ read.o main.o list.o –o prog1.o main.o.o list.o.o read.o Compilation: g++ -c read.c main.c list.c

Compilation & linkage.h read.h.c read.c.c main.c.c list.c.h list.h prog1.o main.o.o list.o.o read.o u If only one file is modified, will we have to recompile all over again? u No. The Makefile uses the dependencies graph

u Aim: Build only out-of-date files (use timestamps) u Makefile contains:  List of dependecies (no cycles)  “Recovery” scenario when any file is modified main.o: main.c list.h read.h g++ -c main.c u In words, if any of the files {main.c, list.h, read.h} was modified after main.o, the command “g++ -c main.c” will be performed Makefile Note, the tab here is essential!

Compilation & linkage.h read.h.c read.c.c main.c.c list.c.h list.h prog1.o main.o.o list.o.o read.o u If read.h is modified, what should be done? u We have to recreate only a subset of the files!

Compilation & linkage.h read.h.c read.c.c main.c.c list.c.h list.h prog1.o main.o.o list.o.o read.o Makefile example: prog1: read.o main.o list.o g++ main.o read.o list.o –o prog1 main.o: main.c read.h list.h g++ -c main.c read.o: read.c read.h g++ -c read.c list.o: list.c list.h g++ -c list.c Running make, e.g: make prog1 make main.o

Makefiles: macros u Macros are similar to variables  Upper case by convention  Example: OBJECTS = read.o list.o main.o prog1: ${OBJECTS} g++ ${OBJECTS} -o prog1

Makefiles: Explicit/implicit rules  We saw “explicit rules” so far, e.g: list.o: list.c list.h g++ -c list.c u Implicit rules (many kinds):  Example, creation by suffices. Create “.o” files from “.c” files.c.o: $*.c g++ -c –o $< $* - the match without the suffix (e.g. list) - file for which the match was made (e.g. list.o) $< - the matched dependency (e.g. list.c)

Makefiles: Explicit/implicit rules  One more example for implicit rule:.java.class: $*.java javac $< Result: For every “.java” file that was modified, a new “.class” file will be created. u When no explicit rule defined, an implicit rule will be used.  not always sufficient (e.g. doesn’t check.h files update)

. Some very common bugs (memory/pointers related)

bug 1 (1)struct Student { (2)int id; (3)char * name; (4)}; (5)Student * stud = (Student *) malloc( sizeof(Student) ); (6)stud->id = ; (7)stud->name = (char *) malloc(100*sizeof(char)); … (8)if (stud != NULL) { free(stud); } Memory leak!!! “name” is not free

bug 2 1) void myFunc() { 2) int * x = randomNum(); 3) int result = *x; //unexpected ! 4) *x = 17; //accessing unallocated space! 5) } 6) 7) int * randomNum() { 8) int j= srand( time(0) ); 9) return &j; 10) } u Never return a pointer of a stack-variable !

bug 3 1) void myFunc(char * input) { 2) char * name = NULL; 3) if (input != NULL ) { 4) name = (char*)malloc(MAX_SIZE); 5) strcpy(name,input); 6) } 7) … 8) free( name ); 9) } u Always use: if (output != NULL ) { free(output); }

bug 4 1) void myFunc(char * input) { 2) char * name; 3) if (input != NULL ) { 4) name = (char*)malloc(MAX_SIZE); 5) strcpy(output,input); 6) } 7) … 8) if ( name != NULL ) { 9) free( name ); 10) } 11) } u Always initialize pointers to NULL !

. Inter module variables’ scope

Static variables u Static variables in a function keep their value for the next call to the function  Allocated on the heap (1) void getUniqueID() { (2) static int id=0; (3) id++; (4) return id; (5) } (6) int main() { (7) int i = getUniqueID(); //i=1 (8) int j = getUniqueID(); //j=2 (9) }

Static variables, cont. u “static” variable on the global scope  Available only in the current module u “extern” variable  Defined outside the module file2.c extern int y; //y from file1.c extern int x; //x defined elsewhere int myFunc() { int y; //error … } file1.c int y; static int x; int myFunc() { int x; //error … }