CIS 190: C/C++ Programming. Lecture 3 Memory Management in C

Similar documents
CS2141 Software Development using C/C++ Debugging

6.S096: Introduction to C/C++

In Java we have the keyword null, which is the value of an uninitialized reference type

CSci 4061 Introduction to Operating Systems. Programs in C/Unix

Lecture 07 Debugging Programs with GDB

CIS 190: C/C++ Programming. Lecture 2 Pointers and More

Outline. Computer programming. Debugging. What is it. Debugging. Hints. Debugging

Kurt Schmidt. October 30, 2018

Lecture 8 Dynamic Memory Allocation

Quiz 0 Answer Key. Answers other than the below may be possible. Multiple Choice. 0. a 1. a 2. b 3. c 4. b 5. d. True or False.

Programming in C S c o t t S c h r e m m e r

Recitation 2/18/2012

POINTER AND ARRAY SUNU WIBIRAMA

CSC 1600 Memory Layout for Unix Processes"

DAY 3. CS3600, Northeastern University. Alan Mislove

High-performance computing and programming I. Introduction to C programming in *NIX

High-performance computing and programming Intro to C on Unix/Linux. Uppsala universitet

CS 326 Operating Systems C Programming. Greg Benson Department of Computer Science University of San Francisco

Class Information ANNOUCEMENTS

MPATE-GE 2618: C Programming for Music Technology. Unit 4.1

12. Debugging. Overview. COMP1917: Computing 1. Developing Programs. The Programming Cycle. Programming cycle. Do-it-yourself debugging

Lecture 14 Notes. Brent Edmunds

advanced data types (2) typedef. today advanced data types (3) enum. mon 23 sep 2002 defining your own types using typedef

CS201: Lab #4 Writing a Dynamic Storage Allocator

Programs. Function main. C Refresher. CSCI 4061 Introduction to Operating Systems

Quiz 0 Review Session. October 13th, 2014

ANITA S SUPER AWESOME RECITATION SLIDES

Exercise Session 6 Computer Architecture and Systems Programming

INITIALISING POINTER VARIABLES; DYNAMIC VARIABLES; OPERATIONS ON POINTERS

Memory Allocation in C C Programming and Software Tools. N.C. State Department of Computer Science

EL2310 Scientific Programming

Reading compiler errors

CS201- Introduction to Programming Current Quizzes

CSE 374 Programming Concepts & Tools. Brandon Myers Winter 2015 Lecture 11 gdb and Debugging (Thanks to Hal Perkins)

Chapter 2 (Dynamic variable (i.e. pointer), Static variable)

Limitations of the stack

PRINCIPLES OF OPERATING SYSTEMS

Princeton University Computer Science 217: Introduction to Programming Systems. Data Structures

TI2725-C, C programming lab, course

CS 103 Lab - Party Like A Char Star

CSE 351. GDB Introduction

CSCI-243 Exam 1 Review February 22, 2015 Presented by the RIT Computer Science Community

Lecture 14. No in-class files today. Homework 7 (due on Wednesday) and Project 3 (due in 10 days) posted. Questions?

Run-time Environments - 3

Announcements. assign0 due tonight. Labs start this week. No late submissions. Very helpful for assign1

Project 3a: Malloc and Free

CSE 374 Programming Concepts & Tools

Deep C. Multifile projects Getting it running Data types Typecasting Memory management Pointers. CS-343 Operating Systems

Basic C Programming. Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

Embedded Software TI2726 B. 3. C tools. Koen Langendoen. Embedded Software Group

Lectures 5-6: Introduction to C

Debugging (Part 2) 1

CSCI 2132 Software Development. Lecture 29: Dynamic Memory Allocation

Week 9 Part 1. Kyle Dewey. Tuesday, August 28, 12

Dynamic Data Structures (II)

Last week. Data on the stack is allocated automatically when we do a function call, and removed when we return

o Code, executable, and process o Main memory vs. virtual memory

Recitation: C Review. TA s 20 Feb 2017

Reminder of midterm 1. Next Thursday, Feb. 14, in class Look at Piazza announcement for rules and preparations

Using a debugger. Segmentation fault? GDB to the rescue!

Arrays and Memory Management

Laboratory Assignment #4 Debugging in Eclipse CDT 1

CSC C69: OPERATING SYSTEMS

Run-time Environments -Part 3

CMSC 341 Lecture 2 Dynamic Memory and Pointers

CS261: HOMEWORK 2 Due 04/13/2012, at 2pm

Object-Oriented Programming

Memory Management. CSC215 Lecture

CS113: Lecture 9. Topics: Dynamic Allocation. Dynamic Data Structures

CS61 Lecture II: Data Representation! with Ruth Fong, Stephen Turban and Evan Gastman! Abstract Machines vs. Real Machines!

C: Pointers. C: Pointers. Department of Computer Science College of Engineering Boise State University. September 11, /21

CS61, Fall 2012 Section 2 Notes

Here's how you declare a function that returns a pointer to a character:

System Assertions. Andreas Zeller

C Programming Basics II

Debugging! The material for this lecture is drawn, in part, from! The Practice of Programming (Kernighan & Pike) Chapter 5!

Programs in memory. The layout of memory is roughly:

We first learn one useful option of gcc. Copy the following C source file to your

Welcome to Lab! Feel free to get started until we start talking! The lab document is located on the course website:

Contents of Lecture 3

EE 355 Lab 4 - Party Like A Char Star

Project 2 Overview: Part A: User space memory allocation

Generating Programs and Linking. Professor Rick Han Department of Computer Science University of Colorado at Boulder

CS 11 C track: lecture 5

Fastbin_dup into stack exploitation

CSE 333 Autumn 2013 Midterm

CS 11 C track: lecture 6

CS201 Some Important Definitions

Intermediate Programming, Spring 2017*

Lecture 7: file I/O, more Unix

COMP26120: Algorithms and Imperative Programming. Lecture 5: Program structuring, Java vs. C, and common mistakes

CS 31: Intro to Systems Pointers and Memory. Martin Gagne Swarthmore College February 16, 2016

HW1 due Monday by 9:30am Assignment online, submission details to come

For Teacher's Use Only Q No Total Q No Q No

Outline. Lecture 1 C primer What we will cover. If-statements and blocks in Python and C. Operators in Python and C

Intermediate Programming, Spring 2017*

CS354 gdb Tutorial Written by Chris Feilbach

CSC209H Lecture 3. Dan Zingaro. January 21, 2015

CS 103 Lab 6 - Party Like A Char Star

Technical Questions. Q 1) What are the key features in C programming language?

Transcription:

CIS 190: C/C++ Programming Lecture 3 Memory Management in C

Outline Memory allocation Memory errors Debugging Makefiles

Memory each process gets its own memory chunk, or address space 0x000000 Stack Function calls, locals 4 GB address space 0xFFFFFFF Heap Global/static vars Code Dynamically allocated memory data segment code segment

Memory each process gets its own memory chunk, or address space 0x000000 Stack 4 GB address space Heap Global/static vars 0xFFFFFFF Code

Stack Allocation memory allocated by the program as it runs local variables function calls fixed at compile time can t be changed while running

Heap allocation dynamic memory allocation memory allocated at run-time two options for allocating memory: malloc() calloc() both require #include <stdlib.h> to work

malloc() <type>* malloc ( <size to be allocated> ) malloc returns a pointer of the size requested char *letters; letters = (char*) malloc(usernumletters * sizeof(char)); cast malloc to be a pointer of desired type call with how many elements needed, multiplied by the size of that element type

calloc() <type>* calloc (<number of elements>, <size of each element> ) calloc works very similarly to malloc, but it initializes all the allocated bits to zero float *grades; grades = (float*) calloc(usernumstudent, sizeof(float)); calloc takes longer than malloc

Correctly handling memory IMPORTANT: before using allocated memory make sure it s actually been allocated if memory wasn t correctly allocated, the pointer will be null, and your program should exit if (grades == NULL) { printf( Memory not allocated, exiting.\n ); exit(-1); }

Outline Memory allocation Memory errors Debugging Makefiles

Segmentation faults when program tries to access a memory location forbidden by the OS common causes for seg faults: accessing out-of-bounds on an array uninitialized pointers

Freeing memory stack allocated memory is automatically freed whenever functions return heap allocated memory was allocated by you so it must also be freed by you done using the free() function free( <name of pointer to memory> )

Freeing in order free() does not work recursively need to free each block of memory separately free must be called in a sensible order headofll (free last!) (free first!)

Memory mistakes memory leaks when data is allocated but not freed access to memory is lost, example: a loop that re-allocates memory to the same variable double free()ing freeing a pointer twice dangling pointer a pointer that points to memory that was freed

Outline Memory allocation Memory errors Debugging Makefiles

Understanding Errors hw2.c:87:7: error: foo undeclared file in which error occurs line number character number error message error or warning errors mean compiler fails: no executable created look at the line of code (then the one before) shortcut to go-to line in xemacs: ESC+g, then line number, enter

Debugging Basics start with the very first error and recompile every time an error is fixed errors will cascade (and de-cascade when fixed) probe statements with printf adding a printf might change your error! GOOGLE

Common Compiler Errors part 1 hw2.c:87:7: error: foo undeclared for variables: forgot to declare, or misspelled for functions: misspelled function, or forgot to #include the file containing the prototype hw2.c:37:6: warning: unused variable bar variable was declared but not used

Common Compiler Errors part 2 hw2.c:54: warning: suggest parentheses around assignment used as truth value usually a mistake: you meant to use == not = hw2.c: 51: error: expected ; before for missing semicolon on previous line of code

Common Linker Errors hw4.o: In function main : hw4.c:91: undefined reference to Fxn linker can t find code for Fxn in any.o file forgot to link.o file misspelled named of Fxn parameter list is different /usr/lib64/gcc/[...]/crt1.o: In function _start : /home/[...]/start.s:119: undefined reference to main you compiled a file that does not contain main() without using the c switch to indicate separate compilation

Easy error to fix > gcc -Wall structs.c In file included from /usr/include/stdio.h:33:0, from structs.c:6: /usr/lib64/gcc/x86_64-suse-linux/4.7/include/stddef.h:213:1: error: expected '=', ',', ';', 'asm' or ' attribute ' before 'typedef' In file included from /usr/include/stdio.h:74:0, from structs.c:6: /usr/include/libio.h:307:3: error: unknown type name 'size_t' /usr/include/libio.h:311:67: error: 'size_t' undeclared here (not in a function) /usr/include/libio.h:339:62: error: expected declaration specifiers or /usr/include/libio.h:348:6: error: expected declaration specifiers or '...' before 'size_t' /usr/include/libio.h:470:19: error: expected '=', ',', ';', 'asm' or ' attribute ' before '_IO_sgetn' In file included from structs.c:6:0: /usr/include/stdio.h:319:35: error: expected declaration specifiers or /usr/include/stdio.h:325:47: error: expected declaration specifiers or /usr/include/stdio.h:337:20: error: expected declaration specifiers or /usr/include/stdio.h:344:10: error: expected declaration specifiers or /usr/include/stdio.h:386:44: error: expected declaration specifiers or /usr/include/stdio.h:390:45: error: expected declaration specifiers or /usr/include/stdio.h:666:11: error: expected declaration specifiers or /usr/include/stdio.h:669:9: error: expected declaration specifiers or /usr/include/stdio.h:679:8: error: expected declaration specifiers or /usr/include/stdio.h:709:15: error: expected '=', ',', ';', 'asm' or ' attribute ' before 'fread' /usr/include/stdio.h:715:15: error: expected '=', ',', ';', 'asm' or ' attribute ' before 'fwrite' /usr/include/stdio.h:737:15: error: expected '=', ',', ';', 'asm' or ' attribute ' before 'fread_unlocked' /usr/include/stdio.h:739:15: error: expected '=', ',', ';', 'asm' or ' attribute ' before 'fwrite_unlocked' In file included from structs.c:9:0: /usr/include/string.h:43:8: error: expected declaration specifiers or '...' before 'size_t' /usr/include/string.h:46:56: error: expected declaration specifiers or /usr/include/string.h:55:18: error: expected declaration specifiers or /usr/include/string.h:62:42: error: expected declaration specifiers or /usr/include/string.h:65:56: error: expected declaration specifiers or /usr/include/string.h:92:48: error: expected declaration specifiers or /usr/include/string.h:129:39: error: expected declaration specifiers or /usr/include/string.h:137:9: error: expected declaration specifiers or /usr/include/string.h:143:57: error: expected declaration specifiers or /usr/include/string.h:150:15: error: expected '=', ',', ';', 'asm' or ' attribute ' before 'strxfrm' In file included from structs.c:9:0: /usr/include/string.h:165:15: error: expected '=', ',', ';', 'asm' or ' attribute ' before 'strxfrm_l' /usr/include/string.h:180:45: error: expected declaration specifiers or /usr/include/string.h:281:15: error: expected '=', ',', ';', 'asm' or ' attribute ' before 'strcspn' /usr/include/string.h:285:15: error: expected '=', ',', ';', 'asm' or ' attribute ' before 'strspn' /usr/include/string.h:395:15: error: expected '=', ',', ';', 'asm' or ' attribute ' before 'strlen' /usr/include/string.h:402:15: error: expected '=', ',', ';', 'asm' or ' attribute ' before 'strnlen' /usr/include/string.h:423:12: error: expected declaration specifiers or /usr/include/string.h:447:33: error: expected declaration specifiers or /usr/include/string.h:451:53: error: expected declaration specifiers or /usr/include/string.h:455:31: error: expected declaration specifiers or /usr/include/string.h:458:54: error: expected declaration specifiers or /usr/include/string.h:536:61: error: expected declaration specifiers or /usr/include/string.h:573:34: error: expected declaration specifiers or /usr/include/string.h:576:39: error: expected declaration specifiers or In file included from structs.c:11:0: /usr/include/stdlib.h:139:15: error: expected '=', ',', ';', 'asm' or ' attribute ' before ' ctype_get_mb_cur_max' In file included from structs.c:11:0: /usr/include/stdlib.h:331:4: error: expected declaration specifiers or /usr/include/stdlib.h:361:4: error: expected declaration specifiers or /usr/include/stdlib.h:465:22: error: expected declaration specifiers or /usr/include/stdlib.h:467:22: error: expected declaration specifiers or /usr/include/stdlib.h:467:38: error: expected declaration specifiers or /usr/include/stdlib.h:479:36: error: expected declaration specifiers or In file included from /usr/include/stdlib.h:491:0, from structs.c:11: /usr/include/alloca.h:32:22: error: expected declaration specifiers or In file included from structs.c:11:0: /usr/include/stdlib.h:497:22: error: expected declaration specifiers or /usr/include/stdlib.h:502:45: error: expected declaration specifiers or /usr/include/stdlib.h:502:65: error: expected declaration specifiers or /usr/include/stdlib.h:755:9: error: expected declaration specifiers or /usr/include/stdlib.h:755:25: error: expected declaration specifiers or /usr/include/stdlib.h:760:34: error: expected declaration specifiers or /usr/include/stdlib.h:760:50: error: expected declaration specifiers or /usr/include/stdlib.h:839:6: error: expected declaration specifiers or /usr/include/stdlib.h:842:6: error: expected declaration specifiers or /usr/include/stdlib.h:846:31: error: expected declaration specifiers or /usr/include/stdlib.h:850:31: error: expected declaration specifiers or /usr/include/stdlib.h:859:36: error: expected declaration specifiers or /usr/include/stdlib.h:863:34: error: expected declaration specifiers or /usr/include/stdlib.h:870:15: error: expected '=', ',', ';', 'asm' or ' attribute ' before 'mbstowcs' /usr/include/stdlib.h:873:15: error: expected '=', ',', ';', 'asm' or ' attribute ' before 'wcstombs'

Debuggers see what is going on inside the program more powerful and accurate than printf() probes examine variables (value and address) change variables on the fly step through code line by line skip blocks of code you don t want to see

Using DDD (or GDB) add the -g flag when compiling open program for testing using ddd [executable] or gdb [executable] add breakpoints to stop the program at specific points use print or display to show values (or addresses) of variables step through code line by line

DDD Tips File -> Open Source choose a different file to look at (and to set breakpoints in) Source -> Reload Source refresh the source you re using after recompiling without losing any breakpoints or data displays FINISH executes the current frame will pause when it hits a return (outside of main)

DDD Livecoding DDD livecoding example was taken wholesale from the sample session on this page: http://www.gnu.org/software/ddd/manual/html_ mono/ddd.html more information about using DDD GDB is the non-graphical version of DDD

Outline Memory allocation Memory errors Debugging Makefiles

Makefile required for all future homeworks makes it easier to grade makes it more accurate to grade #1 rule: make fully compiles program needs to be called Makefile

Homework 2 due tomorrow night @ midnight clarifications made on the homework page no warnings when compiling turnin instructions if you haven t started yet do it NOW!

Homework 3 Memory Diagrams write legibly double check your work due at BEGINNING of class, on paper no late days for this homework!