TUTORIAL 7 CS 137 F18 October 30th.

Slides:



Advertisements
Similar presentations
C Language.
Advertisements

David Notkin Autumn 2009 CSE303 Lecture 13 This space for rent.
Module R2 CS450. Next Week R1 is due next Friday ▫Bring manuals in a binder - make sure to have a cover page with group number, module, and date. You.
Dynamic memory allocation in C (Reek, Ch. 11) 1CS 3090: Safety Critical Programming in C.
CSCI 171 Presentation 11 Pointers. Pointer Basics.
ספטמבר 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.
Memory and Files Dr. Andrew Wallace PhD BEng(hons) EurIng
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:
Memory Arrangement Memory is arrange in a sequence of addressable units (usually bytes) –sizeof( ) return the number of units it takes to store a type.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 9 Pointers and Dynamic Arrays.
1 ES 314 Advanced Programming Lec 3 Sept 8 Goals: complete discussion of pointers discuss 1-d array examples Selection sorting Insertion sorting 2-d arrays.
CS 61C L03 C Arrays (1) A Carle, Summer 2005 © UCB inst.eecs.berkeley.edu/~cs61c/su05 CS61C : Machine Structures Lecture #3: C Pointers & Arrays
Pointers Applications
Dynamic Memory Allocation The process of allocating memory at run time is known as dynamic memory allocation. C does not Inherently have this facility,
Pointers OVERVIEW.
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 4 Pointers and Dynamic Arrays Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.
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 and Pointers Lecture 4.
Prachi A. Joshi Assistant Professor in CSE DIEMS,Aurangabad Unit 1 : Basic Concepts Pointers and dynamic memory allocation, Algorithm Specification, Data.
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.
Pointers *, &, array similarities, functions, sizeof.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 14: Pointers.
CSE 374 Programming Concepts & Tools Hal Perkins Fall 2015 Lecture 10 – C: the heap and manual memory management.
1 Lecture07: Memory Model 5/2/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 9 Pointers and Dynamic Arrays.
Pointers1 WHAT IS A POINTER? Simply stated, a pointer is an address. A running program consists of three parts: execution stack, code, and data. They are.
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. Chapter 9 Pointers and Dynamic Arrays.
© M. Gross, ETH Zürich, 2014 Informatik I für D-MAVT (FS 2014) Exercise 7 – Pointers.
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.
C Lab 2 Intermediate Pointers & Basic Structures.
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.
Overview of memory management
Dynamic Allocation in C
EGR 2261 Unit 11 Pointers and Dynamic Variables
Pointers and Dynamic Arrays
Stack and Heap Memory Stack resident variables include:
Computer Organization and Design Pointers, Arrays and Strings in C
Winter 2009 Tutorial #6 Arrays Part 2, Structures, Debugger
Dynamic Allocation Review Structure and list processing
CSE 374 Programming Concepts & Tools
Valgrind Overview What is Valgrind?
C Programming Language Review and Dissection IV
CSE 303 Concepts and Tools for Software Development
Checking Memory Management
Student Book An Introduction
CSCI206 - Computer Organization & Programming
Programming Languages and Paradigms
Lecture 6 C++ Programming
Arrays & Dynamic Memory Allocation
Circular Buffers, Linked Lists
CSC215 Lecture Memory Management.
CS111 Computer Programming
Pointers.
Dynamic Memory A whole heap of fun….
Pointers The C programming language gives us the ability to directly manipulate the contents of memory addresses via pointers. Unfortunately, this power.
Dynamic Allocation in C
TUTORIAL 8 CS 137 F18 November 5th.
EENG212 – Algorithms & Data Structures Fall 07/08 – Lecture Notes # 5b
TUTORIAL 11 CS 137 F18 November 27th.
Dynamic Memory A whole heap of fun….
C Programming Lecture-8 Pointers and Memory Management
Homework Continue with K&R Chapter 5 Skipping sections for now
Pointer & Memory Allocation Review
Dynamic Memory CSCE 121.
Valgrind Overview What is Valgrind?
Dynamic Memory – A Review
Module 13 Dynamic Memory.
Presentation transcript:

TUTORIAL 7 CS 137 F18 October 30th

Overview a5 Additional Hints a6 Hints Pointers Pointer Arithmetic Memory Allocation Valgrind countMe.c Midterm p8 Solution

a5 Additional Hints Struggling with sum to 15 or cribbage straights Break it down to cases; 1 – 5 cards Utilize poker definitions Sort the hand Common Issues Check for straight AND flushes in poker Same program is returning different values Check all initialized values are given values; not garbage Same applies for array entries indexing within array

a6 Hints Questions? MUST remove main function (comment or delete) Careful of Common Memory Allocation Errors (explained in later slides) a6p1: euclidean_algorithm.c See lecture slides for a refresher on the Euclidean Algorithm See Extended Euclidean Algorithm: https://en.wikipedia.org/wiki/Euclidean_algorithm Iterative or recursive approach (iterative is probably easier) a6p2: run.c Make sure to mutate len with int *ans_len; will be tested Return char array; look up Ascii table for numbers Digit extraction/play a6p3: merge.c Sort as before but using two arrays (already sorted) Care for indexing

Pointers Operators: Reading right-to-left: see pointer_intro.c “&”: gets the address of whatever it is placed before, ex. &num “*” after a type: declares that the variable is a pointer to the given type, ex. int *intpointer “*” before a pointer: dereferences the pointer to give the value of the variable that the pointer points at Reading right-to-left: int *intpointer == “intpointer is a pointer to an int” char **pointer == “pointer is a pointer to a pointer to a char” see pointer_intro.c

Pointer Arithmetic Main take away, if p is a pointer conceptually, p + n moves p forward n of whatever p points at (normalized) in reality, p + n moves p forward n blocks, where each block is the size of what p points at ie. int *p; p + 3 // p increases by 3*sizeof(int) = 12 bytes Synonymous to array indexing see pointers_and_arrays.c can also subtract pointers see sumArray.c (subtraction version of one done in lecture) Can also apply it to incremental operators Use brackets when in combination with dereferencing

Pointers and Structure Syntax Recall from last week: struct health{ int maximum; int health; }; If we had struct health *ptr_health; Then to access the elements you could either do the following: (*ptr_health).maximum ptr_health->maximum See ptr_structBotw.c See pointers_and_structures.c

Memory Allocation Basics #include <stdlib.h> void *malloc (size_t size); returns a pointer to a block of memory (on the heap) size bytes long void free (void *); frees memory block that pointer points to void *realloc (void *p, size_t size); resizes previously allocated block of memory option A) resizes old block of memory to be size bytes long option B) frees old block & allocates new block of memory size bytes long see memory_allocation.c and my_memory_allocation.c

Valgrind Memory error checker built in to the student environment To run: valgrind ./a.out Look for error print outs Invalid free Invalid read/write Look at Heap Summary: in use at exit should be 0 under total heap usage, # of allocs should equal # of frees Look at Leak Summary won’t be there if your program isn’t faulty

Memory Allocation Common Mistakes Not freeing all memory Losing access to allocated memory through a pointer error (lostAccess.c) Forgetting to free something (forgotFree.c) Freeing too much Freeing something that isn’t allocated (notMalloced.c) Freeing something that is already freed (alreadyFreed.c) Trying to read/write to memory that is not allocated Read/write to an element outside the bounds of an array (outOfBounds.c) Using an uninitialized pointer (unitPointer.c) Accessing recently freed memory (accessFreed.c)

countMe.c Create a C program that takes in an array of ints, 0 or greater, the size of the array n, and a pointer to an int length. Want to return a pointer to a block of allocated memory (on the heap) that contains how many times each number in the original array appears in the respected index. Mutate the length of the new array in the int pointer length. int *count(int *array, int n, int *length); Sample Input: a[] = {1,2,3,4,5,4,3,2}; Sample Output: 0 1 2 2 2 1