CSC 107 - Programming for Science Lecture 34: Dynamic Pointers.

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

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.
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.
Managing Memory Static and Dynamic Memory Type Casts Allocating Arrays of Dynamic Size Resizing Block of Memory Returning Memory from a Function Avoiding.
CSCI 171 Presentation 11 Pointers. Pointer Basics.
Managing Memory DCT 1063 PROGRAMMING 2 Mohd Nazri Bin Ibrahim Faculty of Computer, Media & Technology TATi University College
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.
Introduction of Memory Allocation. Memory Allocation There are two types of memory allocations possible in c. Compile-time or Static allocation Run-time.
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:
Pointers Discussion 5 Section Housekeeping HW 1 Issues Array Issues Exam 1 Questions? Submitting on Time!
Introduction to C Programming CE Lecture 18 Dynamic Memory Allocation and Ragged Arrays.
CSC Programming for Science Lecture 30: Pointers.
CS61C L05 C Structures, Memory Management (1) Garcia, Spring 2005 © UCB Lecturer PSOE Dan Garcia inst.eecs.berkeley.edu/~cs61c.
CS 61C L4 Structs (1) A Carle, Summer 2005 © UCB inst.eecs.berkeley.edu/~cs61c/su05 CS61C : Machine Structures Lecture #4: Strings & Structs
1 CS 201 Dynamic Data Structures Debzani Deb. 2 Run time memory layout When a program is loaded into memory, it is organized into four areas of memory.
CSC 107 – Programming For Science. Today’s Goal  Learn how pointers really used with arrays  Exploit the similarity between pointers & arrays  Take.
Pointers| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2006 Slide 1 Pointers by Jumail Bin Taliba Faculty of Computer.
CSC 107 – Programming For Science. Today’s Goal  Learn relationship between pointers & arrays  How and why they both are similar  Take advantage of.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 12: Pointers continued, C strings.
1 Programming with Pointers Turgay Korkmaz Office: SB Phone: (210) Fax: (210) web:
1 C - Memory Simple Types Arrays Pointers Pointer to Pointer Multi-dimensional Arrays Dynamic Memory Allocation.
Pointers and Arrays Beyond Chapter Pointers and Arrays What are the real differences? Pointer Holds the address of a variable Can be pointed.
6. More on Pointers 14 th September IIT Kanpur C Course, Programming club, Fall
CSC 107 – Programming For Science. Today’s Goal Variables  Variable  Variable name location to store data  Only for humans; 0 x 7E8A2410 harder to.
Computer Science and Software Engineering University of Wisconsin - Platteville 2. Pointer Yan Shi CS/SE2630 Lecture Notes.
CSC 107 – Programming For Science. Today’s Goal  Learn how pointers really used with arrays  Exploit the similarity between pointers & arrays  Take.
CSC 107 – Programming For Science. Today’s Goal  When lecture over, start understanding pointers  What a pointer is and what it is not  Why pointers.
CSC 107 – Programming For Science. Today’s Goal  When lecture over, start understanding pointers  What a pointer is and what it is not  Why pointers.
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.
Page 1 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Dynamic Memory Allocation Suppose we defined the data type: struct custrec.
Pointers: Basics. 2 What is a pointer? First of all, it is a variable, just like other variables you studied  So it has type, storage etc. Difference:
Pointers in C Computer Organization I 1 August 2009 © McQuain, Feng & Ribbens Memory and Addresses Memory is just a sequence of byte-sized.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 13: Data structures in C.
CSC Programming for Science Lecture 36: Structures.
ECE Application Programming
Introduction to Computer Organization & Systems Topics: C arrays C pointers COMP Spring 2014 C Part IV.
Pointers *, &, array similarities, functions, sizeof.
Lecture – Pointers1 C++ Pointers Joseph Spring/Bob Dickerson School of Computer Science Operating Systems and Computer Networks Based on notes by Bob Dickerson.
Pointers in C++. Topics Covered  Introduction to Pointers  Pointers and arrays  Character Pointers, Arrays and Strings  Examples.
PHY 107 – Programming For Science. Today’s Goal  Learn how arrays normally used in real programs  Why a function returning an array causes bugs  How.
Arrays as pointers and other stuff COP3275 – PROGRAMMING USING C DIEGO J. RIVERA-GUTIERREZ.
Pointers and Arrays An array's name is a constant whose value is the address of the array's first element. For this reason, the value of an array's name.
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;
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 11: Pointers.
CS162 - Topic #12 Lecture: –Arrays with Structured Elements defining and using arrays of arrays remember pointer arithmetic Programming Project –Any questions?
Pointers. Pointer Fundamentals  When a variable is defined the compiler (linker/loader actually) allocates a real memory address for the variable. –int.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 9.
Pointers: Basics. 2 Address vs. Value Each memory cell has an address associated with it
APS105 Malloc and 2D Arrays (text Ch6.4, Ch10.2).
DYNAMIC MEMORY ALLOCATION. Disadvantages of ARRAYS MEMORY ALLOCATION OF ARRAY IS STATIC: Less resource utilization. For example: If the maximum elements.
CSC Programming for Science Lecture 5: Actual Programming.
CSE 220 – C Programming malloc, calloc, realloc.
Stack and Heap Memory Stack resident variables include:
Chapter 8 Arrays, Strings and Pointers
ENEE150 Discussion 07 Section 0101 Adam Wang.
Programming Languages and Paradigms
Dynamic Memory Allocation
Dynamic Memory Allocation
CS111 Computer Programming
Dynamic Memory Allocation
EECE.2160 ECE Application Programming
Introduction to Problem Solving and Programming
CS111 Computer Programming
Pointers and Arrays Beyond Chapter 16
Pointer Variables A pointer is a variable that contains a memory address The address is commonly the location of another variable in memory This pointer.
C Programming Lecture-8 Pointers and Memory Management
Presentation transcript:

CSC Programming for Science Lecture 34: Dynamic Pointers

Problem of the Day You drive a bus from Rotterdam to Delft. At the 1 st stop, 33 people get in. At the 2 nd stop, 7 more people get in, and 11 passengers leave. The 3 rd stop, sees 5 people leave and 2 get in. After one hour, the bus arrives in Delft. What is the name of the driver? Read the question: You are the driver!

Today’s Goal After lecture, should understand strings Know how to use dynamic memory  Allocate an array  Use a dynamic array  Free the array Remember: Pointers are still hard

From Last Lecture char str[50] = “Car”; char *str2 = str; char bob[] = “Bob”; printf(“%s\n”, strcpy(str, bob)); printf(“%d %s\n”, strlen(str), str); printf(“%d %s\n”, strlen(bob), bob); printf(“%d %s\n”, strlen(str2), str2); strcpy(str, “Car”); printf(“%s\n”, strcat(str, bob)); printf(“%d %s\n”, strlen(str2), str2);

From Last Lecture char str[50] = “The quick brown fox”; char *str2, *str3; str2 = strchr(str, 'Q'); printf(“%p %s\n”, str2, str); str2 = strchr(str, 'q'); str3 = str2 + 1; printf(“%s %s %s\n”, str, str2, str3); str2[0] = '\0'; printf(“%s %s %s\n”, str, str2, str3);

Pointers & Variables Variable names a memory location  Initial value is unknown  Memory location updated via assignment  Value is that stored in memory location Pointer is type of variable…  … but whose value is a memory location  Aliases other variables  Interchangeable with array variable

Memory Location Access Arrays’ & pointers’ values are memory location  Often used interchangeably Use * or [] to access value  *p is a synonym for p[0] But what about other elements?  p[1] or *(p+1) gets 1 th element in p  Can use array index or pointer arithmetic

Pointer Arithmetic Addition & subtraction with pointer variable  Moves pointer over number of elements  Actual memory location may change by more Can only add or subtract integers  C cannot know decimal number will be whole  Cannot use ½ a memory location

Problem with Arrays Array declarations specify size of array  Must be literal value, e.g., 2, 1000, or Size must be large enough to hold all data  May not know max. size when writing code Maximum size may be rare case  Using too much memory slows performance  Slowdown is wasteful if memory not used

Dynamic Memory Allocation Use malloc() to allocate array dynamically  Must specify number of bytes needed  Returns array’s memory location (e.g., pointer)  If array could not be created, returns NULL Figuring out bytes needed is hard  C does not specify how big any data type is  Best to use the sizeof() function

malloc() examples int *up, *down; float *truth, *beauty; char *strange, bob[] = “Bob”; up = malloc(3 * sizeof(int)); down = up; truth = malloc(5 * sizeof(float)); beauty = malloc(2 * sizeof(int)); strange = malloc((strlen(bob) + 1)*sizeof(char)); strcpy(strange, bob); strange[2] = ‘t’; up[0] = 0; down[1] = 2;

We All Must Go Free Sometime malloc() allocations not freed automatically  Would eventually fill up computer’s memory  Need method of releasing memory not in use Can free memory using free() function  Must have been allocated via malloc()  Have to free entire array that was allocated  Once free, cannot reuse memory

malloc() examples int *up, *down, *charm; up = malloc(3 * sizeof(int)); down = up; charm = malloc(2000 * sizeof(int)); up[0] = 0; down[1] = 2; up[2] = 4; down = down + 1; free(down); free(up); down[1] = 34; charm[0] = 12;

For Next Lecture Read! Keep up with weekly assignments