COMP 1402 Winter 2009 Tutorial #8 malloc / calloc / realloc.

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

Linked Lists CSE 2451 Matt Boggus. Dynamic memory reminder Allocate memory during run-time malloc() and calloc() – return a void pointer to memory or.
Dynamic memory allocation
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.
Carnegie Mellon 1 Dynamic Memory Allocation: Basic Concepts : Introduction to Computer Systems 17 th Lecture, Oct. 21, 2010 Instructors: Randy Bryant.
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.
CS1061: C Programming Lecture 21: Dynamic Memory Allocation and Variations on struct A. O’Riordan, 2004, 2007 updated.
Dynamically Allocated Memory String CGS 3460, Lecture 33 Apr 3, 2006 Hen-I Yang.
ECE Application Programming Instructor: Dr. Michael Geiger Fall 2012 Lecture 31: Dynamic memory allocation.
Spring 2005, Gülcihan Özdemir Dağ Lecture 12, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 12 Outline 12.1Introduction.
Growing Arrays in C Language. When to Use Do not use Growing Sorted Array –O(n 2 ) Operation –Avoid when n is large Use Keep track of a variable –Few.
Managing Memory Static and Dynamic Memory Type Casts Allocating Arrays of Dynamic Size Resizing Block of Memory Returning Memory from a Function Avoiding.
Dynamic Memory Allocation The memory usage for program data can increase or decrease as your program runs. The memory usage for program data can increase.
Managing Memory DCT 1063 PROGRAMMING 2 Mohd Nazri Bin Ibrahim Faculty of Computer, Media & Technology TATi University College
Growing Arrays in C By: Victoria Tielebein CS 265- Spring 2011.
1 Memory Allocation Professor Jennifer Rexford COS 217.
User-Level Memory Management in Linux Programming
Agenda  Review: pointer & array  Relationship between pointer & array  Dynamic memory allocation.
CIS 415 – Operating System (Lab) CIS 415 Lab 5 Valgrind (memcheck) Dave Tian.
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.
CP104 Introduction to Programming Structure II Lecture 32 __ 1 Data Type planet_t and Basic Operations Abstract Data Type (ADT) is a data type combined.
C Lab 3 C Arraylist Implementation. Goals ●Review ○ Referencing/Dereferencing ○ Free ●realloc and memmove ●ArrayList ●Debugging with GDB.
Memory Allocation. Memory A memory or store is required in a computer to store programs (or information or data). Data used by the variables in a program.
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{
Linked Structures, Project 1: Linked List Bryce Boe 2013/10/16 CS24, Fall 2013.
Dynamic memory allocation. The process of allocating memory at run time is known as dynamic memory allocation. C have four library functions for allocating.
Programming III SPRING 2015 School of Computer and Information Sciences Francisco R. Ortega, Ph.D. McKnight Fellow and GAANN Fellow LECTURE #5 More about.
1 CSE 303 Lecture 11 Heap memory allocation ( malloc, free ) reading: Programming in C Ch. 11, 17 slides created by Marty Stepp
. Memory Management. Memory Organization u During run time, variables can be stored in one of three “pools”  Stack  Static heap  Dynamic heap.
Pointers Applications
CS 11 C track: lecture 5 Last week: pointers This week: Pointer arithmetic Arrays and pointers Dynamic memory allocation The stack and the heap.
17. ADVANCED USES OF POINTERS. Dynamic Storage Allocation Many programs require dynamic storage allocation: the ability to allocate storage as needed.
Pointers and Arrays Beyond Chapter Pointers and Arrays What are the real differences? Pointer Holds the address of a variable Can be pointed.
Lecture 13 Static vs Dynamic Memory Allocation
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,
Week 9 Part 1 Kyle Dewey. Overview Dynamic allocation continued Heap versus stack Memory-related bugs Exam #2.
ECE 103 Engineering Programming Chapter 47 Dynamic Memory Alocation Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE 103.
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.
Prachi A. Joshi Assistant Professor in CSE DIEMS,Aurangabad Unit 1 : Basic Concepts Pointers and dynamic memory allocation, Algorithm Specification, Data.
1 CHAPTER 5 POINTER. 2 Pointers  Basic concept of pointers  Pointer declaration  Pointer operator (& and *)  Parameter passing by reference  Dynamic.
ECE Application Programming
+ Dynamic memory allocation. + Introduction We often face situations in programming where the data is dynamics in nature. Consider a list of customers.
ECE Application Programming Instructors: Dr. Michael Geiger & Nasibeh Nasiri Fall 2015 Lecture 31: Structures (cont.) Dynamic memory allocation.
1 Lecture07: Memory Model 5/2/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
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.
ENEE150 – 0102 ANDREW GOFFIN Dynamic Memory. Dynamic vs Static Allocation Dynamic  On the heap  Amount of memory chosen at runtime  Can change allocated.
MORE POINTERS Plus: Memory Allocation Heap versus Stack.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 9.
DYNAMIC MEMORY ALLOCATION. Disadvantages of ARRAYS MEMORY ALLOCATION OF ARRAY IS STATIC: Less resource utilization. For example: If the maximum elements.
CSE 220 – C Programming malloc, calloc, realloc.
Winter 2009 Tutorial #6 Arrays Part 2, Structures, Debugger
Introduction to Programming
Pointers and dynamic memory
CSC215 Lecture Memory Management.
Dynamic Memory Allocation
Memory Allocation CS 217.
EECE.2160 ECE Application Programming
Review & Lab assignments
Dynamic Memory Allocation (and Multi-Dimensional Arrays)
CS111 Computer Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
C Programming Lecture-8 Pointers and Memory Management
Dynamic Memory – A Review
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
Module 13 Dynamic Memory.
Dynamic Data Structures
Presentation transcript:

COMP 1402 Winter 2009 Tutorial #8 malloc / calloc / realloc

Tutorial 8 malloc / calloc / realloc The Data Display Debugger (DDD)‏ Exercises on memory management (linked lists & arrays).

malloc, calloc, realloc malloc – allocates a chunk of memory void *malloc(size_t size); e.g.: int *ptr = malloc(10 * sizeof (int));

malloc, calloc, realloc calloc – allocates and initialize memory (to zero)‏ void *calloc(size_t nelem, size_t elsize); e.g.: int *ptr = calloc(10, sizeof (int));

malloc, calloc, realloc realloc – grow or shrink a block of memory void *realloc(void *pointer, size_t size); e.g.: int *new_ptr = realloc(ptr, 250*sizeof(int));

Pitfalls int *ptr = malloc(10*sizeof (int)); *ptr = 0;  ptr could be NULL!!! (must check if ptr==NULL)‏ int foo() { int *tmp = malloc(100000*sizeof (int));... }  If tmp is not freed then we have a memory leak!  Moreover, at each call we allocate a new buffer!!!

Exercise – calloc & realloc Compile and run memory.c. The program uses calloc and realloc to allocate and resize a buffer. Start with a small buffer size and increase it gradually. Note that in some cases the address of the array stays the same and in others it changes. Can you explain why? What is the memory allocation limit of your system?

DDD – Data Display Debugger DDD can do three main kinds of things to help you catch bugs in the act:  Start your program with specified parameters and make it stop on specified conditions.  Examine what has happened, when your program has stopped.  Change things in your program, so you can experiment with correcting the effects of one bug and go on to learn about another. See for a detailed manualhttp://

$ gcc -g -o sample sample.c $ ddd sample

Move the cursor to the desired location (note the argument field)‏ Click on Break Program => Run

Move the mouse pointer on the variable's name Step through your program (either click or type)‏

Make sure the variable is in the argument field Click on Display use size for dynamically allocated arrays

Status => Backtrace

Select the variable in the source code click on Set

Click on Edit to edit your source code with vi

Exercise – Deallocating linked list Say you have a linked list which you are not using anymore. You wrote the following code for freeing up the memory: node *p = head; while (p!= NULL) { free(p); p=p->next; } This code could crash your program! (even worse – might work on your system and fail on others, e.g. the TA's...)‏

Exercise – Deallocating linked list Therefore, the correct code should use a temporary pointer. Go to ex1.c and fix this bug (ah yes, you'll find another small bug there...)‏

Exercise – A game of Tic-Tac-Toe The code in ex2.c is meant to implement the following game:

Exercise – A game of Tic-Tac-Toe However, some parts were not implemented, and others contain bugs. Use the debugger to figure out what's wrong with the code and correct it. Hint: focus on pointers and memory allocation.

Exercise – A game of Tic-Tac-Toe The following can help you in allocating a 2D array: