Download presentation
Presentation is loading. Please wait.
1
TUTORIAL 7 CS 137 F18 October 30th
2
Overview a5 Additional Hints a6 Hints Pointers Pointer Arithmetic
Memory Allocation Valgrind countMe.c Midterm p8 Solution
3
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
4
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: 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
5
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
6
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
7
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
8
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
9
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
10
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)
11
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:
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.