1 Homework HW5 due today Review a lot of things about allocation of storage that may not have been clear when we covered them in our initial pass Introduction.

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

Data Structures Linked Lists Linked List Basics. Array Disadvantages Arrays, although easy to understand have lots of disadvantages Contiguous Memory.
Dynamic memory allocation
C Language.
Dynamic Memory Allocation in C.  What is Memory What is Memory  Memory Allocation in C Memory Allocation in C  Difference b\w static memory allocation.
Introduction to Memory Management. 2 General Structure of Run-Time Memory.
Programming Languages and Paradigms The C Programming Language.
Unions The storage referenced by a union variable can hold data of different types subject to the restriction that at any one time, the storage holds data.
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
SPLINT STATIC CHECKING TOOL Sripriya Subramanian 10/29/2002.
Chapter 7: User-Defined Functions II
Chapter 7: User-Defined Functions II Instructor: Mohammad Mojaddam.
CSCI 171 Presentation 11 Pointers. Pointer Basics.
Pointer applications. Arrays and pointers Name of an array is a pointer constant to the first element whose value cannot be changed Address and name refer.
Kernighan/Ritchie: Kelley/Pohl:
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:
Discussion: Week 3/26. Structs: Used to hold associated data together Used to group together different types of variables under the same name struct Telephone{
Dynamic Data Structures H&K Chapter 14 Instructor – Gokcen Cilingir Cpt S 121 (July 26, 2011) Washington State University.
Introduction to C Programming CE Lecture 18 Dynamic Memory Allocation and Ragged Arrays.
1 Homework Turn in HW2 at start of next class. Starting Chapter 2 K&R. Read ahead. HW3 is on line. –Due: class 9, but a lot to do! –You may want to get.
Rossella Lau Lecture 8, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 8: Polymorphism & C++ pointer  Inheritance.
CEG 221 Lesson 2: Homogeneous Data Types Mr. David Lippa.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
Pointers Applications
Shallow Versus Deep Copy and Pointers Shallow copy: when two or more pointers of the same types point to the same memory – They point to the same data.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes.
Pointer Data Type and Pointer Variables
Data Structures Using C++ 2E Chapter 3 Pointers and Array-Based Lists.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes.
C Course Lecture 4 This lecture we'll talk about: Multi-dimensional arrays. Pointer arithmetic. Pointers to structures. Multi-file programming. What is.
EE4E. C++ Programming Lecture 1 From C to C++. Contents Introduction Introduction Variables Variables Pointers and references Pointers and references.
CSC3315 (Spring 2009)1 CSC 3315 Programming Languages Hamid Harroud School of Science and Engineering, Akhawayn University
1 Homework HW6 due class 22 K&R 6.6 K&R 5.7 – 5.9 (skipped earlier) Finishing up K&R Chapter 6.
Dynamic Memory Allocation Conventional array and other data declarations An incorrect attempt to size memory dynamically Requirement for dynamic allocation.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 12: Pointers continued, C strings.
Chapter 6: User-Defined Functions
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
Stack and Heap Memory Stack resident variables include:
Dynamic Memory Allocation The process of allocating memory at run time is known as dynamic memory allocation. C does not Inherently have this facility,
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
1 Pointers to structs. 2 A pointer to a struct is used in the same way as a pointer to a simple type, such as an int. Pointers to structs were introduced.
C Functions Three major differences between C and Java functions: –Functions are stand-alone entities, not part of objects they can be defined in a file.
ECE 103 Engineering Programming Chapter 36 C Storage Classes Herbert G. Mayer, PSU CS Status 8/4/2014 Initial content copied verbatim from ECE 103 material.
Exam / Homework Exam 1 Starting K&R chapter 4 tonight
Pointers in C Computer Organization I 1 August 2009 © McQuain, Feng & Ribbens Memory and Addresses Memory is just a sequence of byte-sized.
Pointers *, &, array similarities, functions, sizeof.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
1 Homework / Exam Finishing K&R Chapter 5 today –Skipping sections for now –Not covering section 5.12 Starting K&R Chapter 6 next Continue HW5.
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
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.
+ Storage Classes and Linkage. + Introduction Scope describe the region or regions of a program that can access and identifier Variables can be shared.
1 Dynamic Memory Allocation. 2 In everything we have done so far, our variables have been declared at compile time. In these slides, we will see how to.
MORE POINTERS Plus: Memory Allocation Heap versus Stack.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 11: Pointers.
Chapter 12: Pointers, Classes, Virtual Functions, Abstract Classes, and Lists.
DYNAMIC MEMORY ALLOCATION. Disadvantages of ARRAYS MEMORY ALLOCATION OF ARRAY IS STATIC: Less resource utilization. For example: If the maximum elements.
Computer Organization and Design Pointers, Arrays and Strings in C
Course Contents KIIT UNIVERSITY Sr # Major and Detailed Coverage Area
Winter 2009 Tutorial #6 Arrays Part 2, Structures, Debugger
Chapter 7: User-Defined Functions II
CSCI206 - Computer Organization & Programming
Programmazione I a.a. 2017/2018.
This pointer, Dynamic memory allocation, Constructors and Destructor
Dynamic Memory Allocation
Clear1 and Clear2 clear1(int array[], int size) { int i; for (i = 0; i < size; i += 1) array[i] = 0; } clear2(int *array, int size) {
CSC 253 Lecture 8.
CSC 253 Lecture 8.
Memory Allocation CS 217.
Pointers The C programming language gives us the ability to directly manipulate the contents of memory addresses via pointers. Unfortunately, this power.
Presentation transcript:

1 Homework HW5 due today Review a lot of things about allocation of storage that may not have been clear when we covered them in our initial pass Introduction to C99 Review for exam next class –Through end of K&R 6.4 plus MAKE –Open Book/Open Notes

2 Review Declarations/Definitions, K&R pg. 9, 210 A declaration specifies the interpretation to be given to an identified variable A definition is a declaration that reserves storage In C89, declarations/definitions must be made before all executable statements in the same block {…} NOTE: In C99, this requirement is relaxed. All declarations/definitions must be made before being used in any executable statements.

3 Compiler versus Dynamic Allocation of Memory If size is known at “compile time” (i.e., it is a pre-defined constant value): –Use the compiler to allocate memory –This is the easiest way If size is NOT known at “compile time” (e.g., it is entered by user at run time): –You must use dynamic allocation of memory –Be careful to use malloc( ) and free( ) correctly

4 Compiler Allocated Array In HW5, you had a specification for maximum number of lines that tail had to be able to hold Hence: static char *lines[MAXLINE]; /* define array */ /* Static array elements are initialized = NULL for you */... /* No need to free memory allocated for the array lines */ /* (The compiler takes care of all the details for you) */

5 Dynamically Allocated Array If you had no specification for maximum number of lines tail had to be able to hold If you only get maximum number (n) at run time: static char **lines; /* pointer to array of pointers */ lines = (char **) malloc (n * sizeof (char *)); /* You must initialize all pointers in array = NULL */ /* Later, you must free memory allocated for the lines array after freeing memory for each line */ free ( (void *) lines); lines = NULL;

6 Static/Dynamic Arrays Regardless of how lines array is declared – static or dynamic, refer to it in the same way: lines[i] = (char *) malloc (strlen(line)+1); strcpy (lines[i], line); printf (“%s\n”, lines[i++]); free( (void *) lines[i]); lines[i] = NULL;

7 Compiler Allocated Array of structs struct pet { char *type; char *name; };/* each instance of struct pet is 8 bytes */ void print_list(struct pet *, int); /* function prototype */ int main ()/* array and structs allocated by compiler */ {/* all are automatic – allocated on stack */ struct pet list [ ] = {{“cat”, “fluffy”}, {“dog”, “spot”}}; print_list(list, sizeof list / sizeof(struct pet)); /* size = 2 */ return 0; }/* all memory used goes “poof” upon return */

8 Dynamically Allocated Array of structs int main () {int n = 2;/* all structs are in dynamic memory */ struct pet *list = (struct pet *) malloc (n * sizeof(struct pet)); list[0].type = “cat”; list[0].name = “fluffy”; list[1].type = “dog”; list[1].name = “spot”; print_list(list, n); free ( (void *) list); /* free memory used for the array */ list = NULL; /* optional - goes “poof” on return */ return 0; }

9 Dynamically Allocated Array of structs int main () {int n = 2; /* all structs are in dynamic memory */ struct pet *list = (struct pet *) malloc (n * sizeof(struct pet)); struct pet *copy = list; copy->type = “cat”; /* “real” C programmer’s way */ copy->name = “fluffy”; (++copy)->type = “dog”; copy->name = “spot”; print_list(list, n); free ( (void *) list); /* free memory used for the array */ return 0; /* list and copy go out of scope */ }

10 Print Either Array of structs void print_list(struct pet *list, int size) { while (size--) { /* defensive programming – check pointer values */ if (list != NULL && list->type != NULL && list->name != NULL) printf("%s, %s\n", list->type, list->name); list++; }

11 Memory Allocation Guidelines Memory allocation based on two dimensions – desired scope and time when size is known: Temporary ScopePermanent Scope Size known At compile time Size known only At run time Automatic No need to free External and/or Static No need to free Need to use malloc Must free eventually Need to use malloc Free before return

12 Introduction to C99 C99 standard relaxes a few of the C89 rules –// style single line comments may be used –Variables may be defined anywhere in a block. They do not need to be defined before all executable code (useful and helps to enable the next 2 rules) –Automatic array variables may be dimensioned with a value known at run time – not just with a constant –Loop variable may be declared in the initialization statement of a “for” loop– similar to Java –New complex and boolean data types are supported

13 Introduction to C99 gcc supports C99 with the –c99 option flag C89 code will compile correctly under C99 You aren’t required to use any C99 features! You may be able to use C99 to simplify the code for your particular program Careful - if there is any requirement for your code to be C89 compatible for any reason!

14 Dynamically-sized Arrays (C99) If you have no specification for maximum number of lines that the lines array has to be able to hold If you only get the value of n at run time: char *lines [n]; /* cannot be “static” or “external” */ /* You must initialize all pointers in array = NULL */ /* Normal rules of “block” scope for compiler-allocated memory apply! DON’T CALL FREE! */ Note: This would not have worked for hw5 where the lines array had to be static external

15 Review for Exam 2 Let’s go over the Practice Exam –Pointer problem –Program allocating memory for a struct