Dynamically Allocated Memory String CGS 3460, Lecture 33 Apr 3, 2006 Hen-I Yang.

Slides:



Advertisements
Similar presentations
Pointers.
Advertisements

Dynamic 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.
ECE Application Programming Instructor: Dr. Michael Geiger Fall 2012 Lecture 31: Dynamic memory allocation.
CSCI 171 Presentation 11 Pointers. Pointer Basics.
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.
Growing Arrays in C By: Victoria Tielebein CS 265- Spring 2011.
1 Memory Allocation Professor Jennifer Rexford COS 217.
Agenda  Review: pointer & array  Relationship between pointer & array  Dynamic memory allocation.
ספטמבר 04Copyright Meir Kalech1 C programming Language Chapter 6: Dynamic Memory Allocation (DMA)
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.
1 Objectives ❏ To understand the relationship between arrays and pointers ❏ To understand the design and concepts behind pointer arithmetic ❏ To write.
Kernighan/Ritchie: Kelley/Pohl:
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.
Programming C/C++ on Eclipe Trình bày : Ths HungNM C/C++ Training.
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 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 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 CSE 303 Lecture 11 Heap memory allocation ( malloc, free ) reading: Programming in C Ch. 11, 17 slides created by Marty Stepp
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.
1 C - Memory Simple Types Arrays Pointers Pointer to Pointer Multi-dimensional Arrays Dynamic Memory Allocation.
Lecture 13 Static vs Dynamic Memory Allocation
7. Pointers, Dynamic Memory 20 th September IIT Kanpur 1C Course, Programming club, Fall 2008.
Dynamic Memory Allocation The process of allocating memory at run time is known as dynamic memory allocation. C does not Inherently have this facility,
David Notkin Autumn 2009 CSE303 Lecture 12 October 24, 2009: Space Needle.
6. More on Pointers 14 th September IIT Kanpur C Course, Programming club, Fall
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.
C Programming Day 4. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 More on Pointers Constant Pointers Two ways to.
Prachi A. Joshi Assistant Professor in CSE DIEMS,Aurangabad Unit 1 : Basic Concepts Pointers and dynamic memory allocation, Algorithm Specification, Data.
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 CSC103: Introduction to Computer and Programming Lecture No 24.
1 Lecture07: Memory Model 5/2/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
Pointers in C++. Topics Covered  Introduction to Pointers  Pointers and arrays  Character Pointers, Arrays and Strings  Examples.
CSC Programming for Science Lecture 34: Dynamic Pointers.
Arrays, Strings, and Memory. Command Line Arguments #include int main(int argc, char *argv[]) { int i; printf("Arg# Contents\n"); for (i = 0; i < argc;
ENEE150 – 0102 ANDREW GOFFIN Dynamic Memory. Dynamic vs Static Allocation Dynamic  On the heap  Amount of memory chosen at runtime  Can change allocated.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT:10 Advance Pointer Array, String and Dynamic Memory Allocation CS2311 Computer Programming.
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.
Arrays and Pointers (part 1) CSE 2031 Fall July 2016.
CSE 220 – C Programming malloc, calloc, realloc.
Day 03 Introduction to C.
Introduction to Programming
Day 03 Introduction to C.
Programming Languages and Paradigms
Pointers.
Programming and Data Structures
Pointers and dynamic memory
Dynamic Memory Allocation
Memory Allocation CS 217.
Pointers.
EECE.2160 ECE Application Programming
prepared by Senem Kumova Metin modified by İlker Korkmaz
By Hector M Lugo-Cordero September 17, 2008
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
Pointers.
EECE.2160 ECE Application Programming
Presentation transcript:

Dynamically Allocated Memory String CGS 3460, Lecture 33 Apr 3, 2006 Hen-I Yang

Previously… Dynamic Memory Allocation Dynamic Allocated Arrays

Agenda Dynamic Allocated Arrays Dynamic Allocated Strings Deallocation Strings

Malloc an Array int *a; a = malloc(n * sizeof(int)); Can we just say a = malloc(n* 4) ? Not a good idea After malloc, you have all the space needed for the array allocated After that, you can use a[i] or *(a+i) to access the elements

Calloc an Array int *a; A = calloc(n, sizeof(int)); Why do we use calloc? All elements are initialized to 0 at the time of allocation

Realloc an Array int *a; a = malloc(n * sizeof(int)); a = realloc(a, m * sizeof(int)); Why do we use realloc? So we can change its size after we malloc or calloc If first argument is null, realloc = malloc If second argument is zero, the memory is freed

Strings In C, strings are equivalent to a collection (array) of characters How do we setup a string?  char quarterback[] = “Palmer”;  7 bytes are allocate for variable name.  What if Mannings replaced Palmer as QB?  char director[9];  Let’s say, a guy named Roethlisberger comes along.  char * quarterback; quarterback = (char *) malloc (15);

How do we use malloc with strings? char * temp_buffer = (char *) malloc(20); char * name; char * title; scanf(“%s”, temp_buffer); name = (char *) malloc(strlen(temp_buffer)); strcpy(name, temp_buffer); strcpy(temp_buffer, “□ □ □ □ … □”); □ x 20 scanf(“%s”, temp_buffer); title = (char *) malloc(strlen(temp_buffer)); strcpy(title, temp_buffer);

Array of strings If there are multiple strings, we can use a 2 dimensional array to store it. But it waste too much space, instead we store an array of pointers to strings char * strings[NUMBER_OF_STRINGS]; strings[0], strings[1], …

Recycle Memory leak Free p = malloc(…); free(p); Calling free to a memory location, not previously allocated dynamically (e.g. an element of array, a variable) can have unpredictable effect Dangling pointer: refer to the pointer pointing to an address that is already freed Some programmers put p = null; whenever they free the memory p points to.

Strings String: an array of characters String constant (String literals) String variable

Summary Dynamic Allocated Arrays Dynamic Allocated Strings Deallocation Strings

Before you go Nothing for today. Enjoy your plan for tonight.