ENEE150 Discussion 07 Section 0101 Adam Wang.

Slides:



Advertisements
Similar presentations
Dynamic memory allocation
Advertisements

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.
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.
CSCI 171 Presentation 11 Pointers. Pointer Basics.
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’;
Memory and C++ Pointers.  C++ objects and memory  C++ primitive types and memory  Note: “primitive types” = int, long, float, double, char, … January.
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.
CS50 SECTION: WEEK 4 Kenny Yu. Announcements  Problem Set 4 Walkthrough online  Problem Set 2 Feedback has been sent out  CORRECTION: Expect all future.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 12: Pointers continued, C strings.
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.
7. Pointers, Dynamic Memory 20 th September IIT Kanpur 1C Course, Programming club, Fall 2008.
Stack and Heap Memory Stack resident variables include:
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
Computer Science and Software Engineering University of Wisconsin - Platteville 2. Pointer Yan Shi CS/SE2630 Lecture Notes.
Week 9 Part 1 Kyle Dewey. Overview Dynamic allocation continued Heap versus stack Memory-related bugs Exam #2.
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.
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.
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
+ Dynamic memory allocation. + Introduction We often face situations in programming where the data is dynamics in nature. Consider a list of customers.
1 Lecture07: Memory Model 5/2/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
CSC Programming for Science Lecture 34: Dynamic Pointers.
ENEE150 – 0102 ANDREW GOFFIN Dynamic Memory. Dynamic vs Static Allocation Dynamic  On the heap  Amount of memory chosen at runtime  Can change allocated.
MORE POINTERS Plus: Memory Allocation Heap versus Stack.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 11: Pointers.
C Tutorial - Pointers CS 537 – Introduction to Operating Systems.
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.
Memory allocation & parameter passing
CMSC 341 Lecture 2 – Dynamic Memory and Pointers (Review)
EGR 2261 Unit 11 Pointers and Dynamic Variables
Stack and Heap Memory Stack resident variables include:
Winter 2009 Tutorial #6 Arrays Part 2, Structures, Debugger
Chapter 6 – Data Types CSCE 343.
Day 03 Introduction to C.
Computer Science 210 Computer Organization
Introduction to Programming
Dynamic Memory Allocation Strings
ENEE150 Discussion 06 Section 0101 Adam Wang.
Day 03 Introduction to C.
Checking Memory Management
CSCI206 - Computer Organization & Programming
Dynamic Memory Allocation
Programming Languages and Paradigms
Dynamic Memory Allocation
Clear1 and Clear2 clear1(int array[], int size) { int i; for (i = 0; i < size; i += 1) array[i] = 0; } clear2(int *array, int size) {
14th September IIT Kanpur
Computer Science 210 Computer Organization
Dynamic Memory Allocation
Memory Allocation CS 217.
EECE.2160 ECE Application Programming
Dynamic Memory Allocation (and Multi-Dimensional Arrays)
Pointers The C programming language gives us the ability to directly manipulate the contents of memory addresses via pointers. Unfortunately, this power.
CS111 Computer Programming
Pointers and Arrays Beyond Chapter 16
7. Pointers, Dynamic Memory
Dynamic Memory A whole heap of fun….
Dynamic Memory – A Review
Pointers, Dynamic Data, and Reference Types
SPL – PS2 C++ Memory Handling.
CSE 303 Concepts and Tools for Software Development
Presentation transcript:

ENEE150 Discussion 07 Section 0101 Adam Wang

Overview More on project 2 Structs Malloc Quiz

Reading in the title and Author One big loop that runs until you’re done with the file In the loop, write your code that parses the title, author, time, categories, instructions, and ingredients When the loop starts again that same code should work for the next recipe

Method 1 (From last week) We have read in everything into raw_recipes[MAX_SIZE] We need to find the character right after “Recipe: ” Use a counter that will move across the entire array int i = 0 Reference the character with raw_recipes[i]

Method 1 We’ll keep moving it until we see the ‘:’ character Then we’ll move until we hit the first non-space character raw_recipes[i] != ‘ ’ Alternatively, just bump i up by 6 Then every char until the ‘\n’ we copy into the titles array titles[index] = raw_recipes[i] Don’t forget to append the ‘\0’ char at the end to make it a string titles[index] = ‘\0’

Method 2 (maybe more intuitive) We’ll use our string methods to find the title Create a new pointer, set it to raw_recipes Use strstr to find “Recipe: “ Bump the pointer up by 13 Now you can copy it in char by char like method 1 (this time you’re using pointer arithmetic though)

Method 2 You could alternatively use strncpy to copy in the title Use strstr again to find the next “\n” char Use strncpy to copy it into the title array Source would be p_start Destination is the title array Number of chars is p_end – p_start Append the ‘\0’ to title[p_end – p_start]

Structs struct books { char title[50]; char author[50]; char subject[100]; int book_id; }; struct books s1 = {“a”, “b”, “c”, 20}; Everything is stored right next to each other (more or less) s1 = s2 is VALID for structs! s1 == s2 is NOT VALID for structs

Typedef typedef struct data { float score1; int score2; int score3; int score4; } data_el; data_el example = {2.5,7,8,9}; // just makes typing it out easier example.score4++;

Struct pointers typedef struct my_structure { char name[20]; int number; int rank; } my_struct; my_struct var = {“bob”, 5, 10}; my_struct *ptr = &var; printf(“Name: %s”, ptr->name); // same as (*ptr).name

Malloc Dynamically allocating memory for new variables Memory gets taken from the heap Regular variables are made on the runtime stack + Main advantage is now we don’t have to waste memory - Taking from the heap is a low slower than stack Computer has to figure out where to find the memory (this is called fragmentation) Heap is considered global memory whereas variables on the stack are restricted to the function they’re in

Malloc int *ptr = (int *)malloc(sizeof(int)); heap Why do we use sizeof instead of 4 bytes? Not all computers will use 4 bytes for a size of an integer Malloc normally returns a void pointer; this cannot be dereferenced, but can be casted to anything heap ptr 0xEF 0xA4 0x37 0x9D 0xAE536…

Free Free(ptr); Computer knows this is memory is no longer needed Pointer that points to freed memory is called a dangling pointer C does not have automatic garbage collection like other languages

Arrays Arrays are basically pointers to the first element int *array = (int *) malloc (10 * sizeof(int)); or int *array = (int *) calloc (10 * sizeof(int));

Very important concepts to know Stack vs heap When would you use either Tradeoffs How does computer visualize memory Void pointers, NULL pointers, dangling pointers, when they are dangerous Malloc; how it works Memory leaks: occurs when user is not freeing memory Tradeoffs in usage

Quiz wget ece.umd.edu/~aw97/quizzes/quiz2.c Write a function count_x that takes in a string and returns the number of x’s count_x("abcxyz") ----> 1 count_x("20458x4820x;?$#") ----> 2 count_x("abc") ----> 0 You MUST use pointer arithmetic, no array indexing Submit under assignment 101: submit 2017 fall enee 150 0101 101 quiz2.c