Dynamic Memory Allocation

Slides:



Advertisements
Similar presentations
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.
Advertisements

CS1061: C Programming Lecture 21: Dynamic Memory Allocation and Variations on struct A. O’Riordan, 2004, 2007 updated.
ECE Application Programming Instructor: Dr. Michael Geiger Fall 2012 Lecture 31: Dynamic memory allocation.
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.
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.
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:
1 Day 03 Introduction to C. 2 Memory layout and addresses r s int x = 5, y = 10; float f = 12.5, g = 9.8; char c = ‘r’, d = ‘s’;
1 1 Lecture 4 Structure – Array, Records and Alignment Memory- How to allocate memory to speed up operation Structure – Array, Records and Alignment Memory-
Memory Layout C and Data Structures Baojian Hua
CS 11 C track: lecture 5 Last week: pointers This week: Pointer arithmetic Arrays and pointers Dynamic memory allocation The stack and the heap.
Outline Midterm results Static variables Memory model
17. ADVANCED USES OF POINTERS. Dynamic Storage Allocation Many programs require dynamic storage allocation: the ability to allocate storage as needed.
Dynamic Memory Allocation Conventional array and other data declarations An incorrect attempt to size memory dynamically Requirement for dynamic allocation.
1 C - Memory Simple Types Arrays Pointers Pointer to Pointer Multi-dimensional Arrays Dynamic Memory Allocation.
Lecture 13 Static vs Dynamic Memory Allocation
C Programming Lecture 10-1 : Array & Pointer. Character Array String A sequence of characters The last character should be ‘\0’ that indicates “the end.
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.
1 Dynamic Memory Allocation –The need –malloc/free –Memory Leaks –Dangling Pointers and Garbage Collection Today’s Material.
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.
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
Pointers in C Computer Organization I 1 August 2009 © McQuain, Feng & Ribbens Memory and Addresses Memory is just a sequence of byte-sized.
ECE Application Programming
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
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.
MORE POINTERS Plus: Memory Allocation Heap versus Stack.
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.
Dynamic Allocation in C
Memory allocation & parameter passing
Stack and Heap Memory Stack resident variables include:
Computer Organization and Design Pointers, Arrays and Strings in C
ECE Application Programming
Day 03 Introduction to C.
Lesson One – Creating a thread
Introduction to Programming
ENEE150 Discussion 07 Section 0101 Adam Wang.
Chapter 19 Data Structures
Day 03 Introduction to C.
C Basics.
CSCI206 - Computer Organization & Programming
Programming Languages and Paradigms
CSC215 Lecture Memory Management.
Dynamic Memory Allocation
Dynamic Memory Allocation
Memory Allocation CS 217.
Dynamic Memory Allocation
EECE.2160 ECE Application Programming
Review & Lab assignments
Dynamic Memory Allocation (and Multi-Dimensional Arrays)
Dynamic Memory A whole heap of fun….
CS111 Computer Programming
Chien-Chung Shen CIS/UD
EENG212 – Algorithms & Data Structures Fall 07/08 – Lecture Notes # 5b
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
Dynamic Memory A whole heap of fun….
Dynamic Memory.
C Programming Lecture-8 Pointers and Memory Management
Comp 208 Computers in Engineering Yi Lin Fall, 2005
Pointers and References
Dynamic Memory – A Review
EECE.2160 ECE Application Programming
SPL – PS2 C++ Memory Handling.
Module 13 Dynamic Memory.
CSE 303 Concepts and Tools for Software Development
Presentation transcript:

Dynamic Memory Allocation Problem: Don't know how much memory to allocate to an array Why? Memory is allocated to the correct size for a record to be written to disk Size depends upon a particular computer configuration How? We use the C malloc and free functions

malloc() and free() void *malloc(size_t size) void free(void * data) void * is a generic pointer for any type of data size_t is a typedef for unsigned integers allocates size in bytes returns pointer to new memory or NULL void free(void * data) releases memory pointed to by data If data is NULL, no harm done C has no garbage collection, skipping free results in memory leaks Prevent dangling references: set a pointer to NULL after free Freeing from a function (pass a pointer to a pointer) Function freeMyStructure( void **data) { free(*data); *data = NULL); } Dangling reference: a pointer to dynamic memory that was released Alias: Two pointers to the same dynamically allocated memory

Rules Never attempt to free memory twice Never use a pointer after its memory was freed (dangling pointer) Never attempt to free memory that wasn't allocated dynamically. Set all pointers to null initially, and after freeing them

Example Static allocation: int nums[100; Dynamic allocation int *nums, n; printf("How Many?\n"); scanf("%d", &n); nums = malloc(n * sizeof(int)); ••• free(nums); nums = NULL; Note: sizeof is an operator that finds the byte length of a data type

Standard Programming Environment Editors: vi, emacs, pico Compiler: gcc or cc Linker: gcc or cc Automated system builder: make, ant Profiler: gprof (find inefficient functions) Source control system: SCCS, CVS tracks code changes coordinates changes among programmers automatically assign release numbers

Hints for the last lab The file syntax is: model:year:rate\n A sscanf %s will read the entire string up to a space. If none, it reads everything Find the pointer to the colon, and store a NULL ('\0') How to find the colon? Either a character by character search, or by using the C strstr method You can then use sscanf on the back portion '\0' F o r d : 9 8 . 5