lecture 6: memory, part 4ix.cs.uoregon.edu/~hank/212/lectures/cis212_f19_lec6.pdf · pause and do...

56
Hank Childs, University of Oregon October 17, 2019 Lecture 6: Memory, part 4 __. __ .__ __. __ ___ ___ ______ ___ .__ __. _______ _______ ___ .___________. ___ | | | | | \| | | | \ \/ / / | / \ | \| | | \ | \ / \ | | / \ | | | | | \| | | | \ V / | ,----' /^\ | \| | | .--. | | .--. | / ^ \ `---| |----` / ^ \ | | | | | . ` | | | > < || / /_\ \ | . ` | | | | | | | | | / /_\ \ || / /_\ \ | `--' | | |\ | | |/.\ __ | `----.__ / _____ \ | |\ | | '--' | | '--' | / _____ \ || / _____ \ \______/ |__| \__| |__| /__/ \__\ (_ ) \______(_ ) /__/ \__\ |__| \__| |_______/ |_______/ /__/ \__\ |__| /__/ \__\ _______.___________..______ __ __ ______ .___________. __ __ .______ _______ _______. / | || _ \ ||||/ || || | | | | _ \ | ____| / | | (----`---| |----`| |_) | | | | | | ,----'`---| |----`| | | | | |_) | | |__ | (----` \ \ || | / | | | | | | || | | | | | / | __| \ \ .----) | || | |\ \----.| `--' | | `----. || | `--' | | |\ \----.| |____.----) | |_______/ |__| | _| `._____| \______/ \______| |__| \______/ | _| `._____||_______|_______/

Upload: others

Post on 10-Aug-2020

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Lecture 6: Memory, part 4ix.cs.uoregon.edu/~hank/212/lectures/CIS212_F19_Lec6.pdf · Pause and do an example and film it. Memory Segments •Your program is divided into 4 segments:

Hank Childs, University of OregonOctober 17, 2019

Lecture 6:Memory, part 4

__. __ .__ __. __ ___ ___ ______ ___ .__ __. _______ _______ ___ .___________. ___| | | | | \ | | | | \ \ / / / | / \ | \ | | | \ | \ / \ | | / \| | | | | \| | | | \ V / | ,----' / ^ \ | \| | | .--. | | .--. | / ^ \ `---| |----` / ^ \| | | | | . ` | | | > < | | / /_\ \ | . ` | | | | | | | | | / /_\ \ | | / /_\ \| `--' | | |\ | | | / . \ __ | `----.__ / _____ \ | |\ | | '--' | | '--' | / _____ \ | | / _____ \\______/ |__| \__| |__| /__/ \__\ (_ ) \______(_ ) /__/ \__\ |__| \__| |_______/ |_______/ /__/ \__\ |__| /__/ \__\

_______.___________..______ __ __ ______ .___________. __ __ .______ _______ _______. / | || _ \ | | | | / || || | | | | _ \ | ____| / | | (----`---| |----`| |_) | | | | | | ,----'`---| |----`| | | | | |_) | | |__ | (----` \ \ | | | / | | | | | | | | | | | | | / | __| \ \

.----) | | | | |\ \----.| `--' | | `----. | | | `--' | | |\ \----.| |____.----) ||_______/ |__| | _| `._____| \______/ \______| |__| \______/ | _| `._____||_______|_______/

Page 2: Lecture 6: Memory, part 4ix.cs.uoregon.edu/~hank/212/lectures/CIS212_F19_Lec6.pdf · Pause and do an example and film it. Memory Segments •Your program is divided into 4 segments:

C&Memory• Callowsyoutodirectlyaccessmemory• Thereisno“babysitter”thatmonitorsyourmemoryaccesses– Unlessyouwanderoutsidea“segment,”inwhichcaseyourprogramcrashes

• Thishelpsperformance• Butmakesprogrammingdifficult– noseatbelts• HOWEVER:thereareprogramsthatwillmonitoryourmemoryaccessesandletyouknowwhenyouhavedonebadthings– valgrind &gdb

Page 3: Lecture 6: Memory, part 4ix.cs.uoregon.edu/~hank/212/lectures/CIS212_F19_Lec6.pdf · Pause and do an example and film it. Memory Segments •Your program is divided into 4 segments:

Thisweek’slab…

• Ifyou“domemorywrong,”thenyourprogramwillcrash

• Howtofix?• Specialprogram:gdb (GNUdebugger)– gdb cangiveyoulotsofhintsaboutwhyyourprogramcrashed!

Page 4: Lecture 6: Memory, part 4ix.cs.uoregon.edu/~hank/212/lectures/CIS212_F19_Lec6.pdf · Pause and do an example and film it. Memory Segments •Your program is divided into 4 segments:

NOCLASSTUESDAY

• InsteadYouTubelecture• Postedthisweekend• WillbeentirelyabouthowtodoProject2D

• (2Cdiscussedlaterthislecture)

Page 5: Lecture 6: Memory, part 4ix.cs.uoregon.edu/~hank/212/lectures/CIS212_F19_Lec6.pdf · Pause and do an example and film it. Memory Segments •Your program is divided into 4 segments:

Whatshouldyoubedoing?

• Duetoday:2B• 2Cmaybepostedtonightifwemakeitfarenoughinlecture

• Readingthetextbook,ifyouhavenotdoneitalready– Chapter2.1,2.2,2.3,2.4.,2.5,and2.6.1– Chapter4.1(butnot4.1.2),4.2-4.4–à willbeanupcomingquizondetailsinthebook(thingsIhavenotlecturedonwillbefairgame)

Page 6: Lecture 6: Memory, part 4ix.cs.uoregon.edu/~hank/212/lectures/CIS212_F19_Lec6.pdf · Pause and do an example and film it. Memory Segments •Your program is divided into 4 segments:

Review

Page 7: Lecture 6: Memory, part 4ix.cs.uoregon.edu/~hank/212/lectures/CIS212_F19_Lec6.pdf · Pause and do an example and film it. Memory Segments •Your program is divided into 4 segments:

Pointers• Pointer:pointstomemorylocation– Denotedwith‘*’– Example:“int *p”

• pointertoaninteger– Youneedpointerstogettoheapmemory

• Addressof:getstheaddressofmemory– Operator:‘&’– Example:

int x;int *y=&x;ß thisexampleispointingtoanautomaticvariable,notadynamicvariable

Page 8: Lecture 6: Memory, part 4ix.cs.uoregon.edu/~hank/212/lectures/CIS212_F19_Lec6.pdf · Pause and do an example and film it. Memory Segments •Your program is divided into 4 segments:

Whatisanarray?

• Blockofcontiguousmemory• IfelementseachhavesizeNbytesandthereareMelements,themN*Mcontiguousbytes

• LetAbeaddressofthebeginningofthearray• ThenA[0]isat“A”• AndA[1]isat“A+N”• A[2]isat“A+2*N”• andsoon…

Page 9: Lecture 6: Memory, part 4ix.cs.uoregon.edu/~hank/212/lectures/CIS212_F19_Lec6.pdf · Pause and do an example and film it. Memory Segments •Your program is divided into 4 segments:
Page 10: Lecture 6: Memory, part 4ix.cs.uoregon.edu/~hank/212/lectures/CIS212_F19_Lec6.pdf · Pause and do an example and film it. Memory Segments •Your program is divided into 4 segments:

Dynamicmemoryworksdifferently

• Youallocateit,itstaysarounduntilyoude-allocateitortheprogramends

• Important:youneedawaytokeeptrackofmemory– Ifnot,thememorywillbe“leaked.”

• Soweneedawayofmanagingdynamicmemory.

• TheconceptfordoingthisinCisPOINTERS

Page 11: Lecture 6: Memory, part 4ix.cs.uoregon.edu/~hank/212/lectures/CIS212_F19_Lec6.pdf · Pause and do an example and film it. Memory Segments •Your program is divided into 4 segments:

DynamicMemoryAllocation

• Specialbuilt-infunctiontoallocatememoryfromheap:malloc– InteractswithOperatingSystem– Argumentformalloc ishowmanybytesyouwant

• Alsobuilt-infunctiontodeallocate memory:free

Page 12: Lecture 6: Memory, part 4ix.cs.uoregon.edu/~hank/212/lectures/CIS212_F19_Lec6.pdf · Pause and do an example and film it. Memory Segments •Your program is divided into 4 segments:

free/malloc exampleEnablescompilertoseefunctionsthataren’tinthisfile.Moreonthisnextweek.

sizeof isabuiltinfunctioninC.Itreturnsthenumberofbytesforatype(4bytesforint).

don’thavetosayhowmanybytestofree…theOSknows

Page 13: Lecture 6: Memory, part 4ix.cs.uoregon.edu/~hank/212/lectures/CIS212_F19_Lec6.pdf · Pause and do an example and film it. Memory Segments •Your program is divided into 4 segments:

AutomaticvsDynamic

• Automaticmemorylivesonlyforitscurrentscope

• Dynamicmemorylivesuntilyoufreeit,oruntiltheprogramends

Page 14: Lecture 6: Memory, part 4ix.cs.uoregon.edu/~hank/212/lectures/CIS212_F19_Lec6.pdf · Pause and do an example and film it. Memory Segments •Your program is divided into 4 segments:

Thisisjustfine…

Page 15: Lecture 6: Memory, part 4ix.cs.uoregon.edu/~hank/212/lectures/CIS212_F19_Lec6.pdf · Pause and do an example and film it. Memory Segments •Your program is divided into 4 segments:

Thisisnotfine…

Page 16: Lecture 6: Memory, part 4ix.cs.uoregon.edu/~hank/212/lectures/CIS212_F19_Lec6.pdf · Pause and do an example and film it. Memory Segments •Your program is divided into 4 segments:

Andhereitgoeswrong…

Page 17: Lecture 6: Memory, part 4ix.cs.uoregon.edu/~hank/212/lectures/CIS212_F19_Lec6.pdf · Pause and do an example and film it. Memory Segments •Your program is divided into 4 segments:

Pauseanddoanexampleandfilmit

Page 18: Lecture 6: Memory, part 4ix.cs.uoregon.edu/~hank/212/lectures/CIS212_F19_Lec6.pdf · Pause and do an example and film it. Memory Segments •Your program is divided into 4 segments:

MemorySegments• Yourprogramisdividedinto4segments:– Codesegment:wherethemachinecodeinstructionsare

– Datasegment:wheretheglobalvariableslive…andotherthings

– “Stack”:whereautomaticmemorylives– “Heap”:wheredynamicmemorylives– Note:stack&heaparedatastructuresandwewilllearnmoreaboutthemlater.

Ifyouaccessmemoryaddressesoutsideyoursegments,yougeta“segmentationfault”…whichcausesacrash

Page 19: Lecture 6: Memory, part 4ix.cs.uoregon.edu/~hank/212/lectures/CIS212_F19_Lec6.pdf · Pause and do an example and film it. Memory Segments •Your program is divided into 4 segments:

“StackOverflow”

Stack Heap

Andinsomecasesitdoesn’thavetohittheheap.Justtakemorememorythanintended.

Page 20: Lecture 6: Memory, part 4ix.cs.uoregon.edu/~hank/212/lectures/CIS212_F19_Lec6.pdf · Pause and do an example and film it. Memory Segments •Your program is divided into 4 segments:

CharacterStrings

Page 21: Lecture 6: Memory, part 4ix.cs.uoregon.edu/~hank/212/lectures/CIS212_F19_Lec6.pdf · Pause and do an example and film it. Memory Segments •Your program is divided into 4 segments:

ASCIICharacterSet

imagesource:granneman.com

TherehavebeenvariousextensionstoASCII…nowmorethan128characters

Manyspecialcharactersarehandledoutsidethisconvention

Page 22: Lecture 6: Memory, part 4ix.cs.uoregon.edu/~hank/212/lectures/CIS212_F19_Lec6.pdf · Pause and do an example and film it. Memory Segments •Your program is divided into 4 segments:

SomeSpecialCharacters

New:%cprintscharacter

Page 23: Lecture 6: Memory, part 4ix.cs.uoregon.edu/~hank/212/lectures/CIS212_F19_Lec6.pdf · Pause and do an example and film it. Memory Segments •Your program is divided into 4 segments:

characterstrings

• Acharacter“string”is:– anarrayoftype“char”– thatisterminatedbytheNULLcharacter

• TheClibraryhasmultiplefunctionsforhandlingstrings

Page 24: Lecture 6: Memory, part 4ix.cs.uoregon.edu/~hank/212/lectures/CIS212_F19_Lec6.pdf · Pause and do an example and film it. Memory Segments •Your program is divided into 4 segments:

buildingacharacterstring

New:%sprints“strings”(arraysofcharsterminatedby\0)

Page 25: Lecture 6: Memory, part 4ix.cs.uoregon.edu/~hank/212/lectures/CIS212_F19_Lec6.pdf · Pause and do an example and film it. Memory Segments •Your program is divided into 4 segments:

buildingacharacterstring

Compilerautomaticallyaddsa\0foryou!

Page 26: Lecture 6: Memory, part 4ix.cs.uoregon.edu/~hank/212/lectures/CIS212_F19_Lec6.pdf · Pause and do an example and film it. Memory Segments •Your program is divided into 4 segments:
Page 27: Lecture 6: Memory, part 4ix.cs.uoregon.edu/~hank/212/lectures/CIS212_F19_Lec6.pdf · Pause and do an example and film it. Memory Segments •Your program is divided into 4 segments:

Didyounotice

• CharactersinCaresinglequotes:‘A’• StringsinCaredoublequotes:“helloworld”• Astringofasinglecharacterisstilldoublequotes:“A”– (Andthisisactually“A\0”)

Page 28: Lecture 6: Memory, part 4ix.cs.uoregon.edu/~hank/212/lectures/CIS212_F19_Lec6.pdf · Pause and do an example and film it. Memory Segments •Your program is divided into 4 segments:
Page 29: Lecture 6: Memory, part 4ix.cs.uoregon.edu/~hank/212/lectures/CIS212_F19_Lec6.pdf · Pause and do an example and film it. Memory Segments •Your program is divided into 4 segments:

Characterstringsexample

Page 30: Lecture 6: Memory, part 4ix.cs.uoregon.edu/~hank/212/lectures/CIS212_F19_Lec6.pdf · Pause and do an example and film it. Memory Segments •Your program is divided into 4 segments:

UsefulClibrarystringfunctions• strcpy:stringcopy• strncpy:stringcopy,butjustfirstNcharacters• strlen:lengthofastring

Page 31: Lecture 6: Memory, part 4ix.cs.uoregon.edu/~hank/212/lectures/CIS212_F19_Lec6.pdf · Pause and do an example and film it. Memory Segments •Your program is divided into 4 segments:

UsefulClibrarystringfunctions• strcpy:stringcopy• strncpy:stringcopy,butjustfirstNcharacters• strlen:lengthofastring

Whathappenedhere?

Page 32: Lecture 6: Memory, part 4ix.cs.uoregon.edu/~hank/212/lectures/CIS212_F19_Lec6.pdf · Pause and do an example and film it. Memory Segments •Your program is divided into 4 segments:

MoreusefulClibrarystringfunctions

source:cplusplus.com

Page 33: Lecture 6: Memory, part 4ix.cs.uoregon.edu/~hank/212/lectures/CIS212_F19_Lec6.pdf · Pause and do an example and film it. Memory Segments •Your program is divided into 4 segments:

argc &argv

• twoargumentstoeveryCprogram• argc:howmanycommandlinearguments• argv:anarraycontainingeachofthearguments

• “./a.out hankchilds”• à argc ==3• à argv[0]=“a.out”,argv[1]=“hank”,argv[2]=“childs”

Page 34: Lecture 6: Memory, part 4ix.cs.uoregon.edu/~hank/212/lectures/CIS212_F19_Lec6.pdf · Pause and do an example and film it. Memory Segments •Your program is divided into 4 segments:
Page 35: Lecture 6: Memory, part 4ix.cs.uoregon.edu/~hank/212/lectures/CIS212_F19_Lec6.pdf · Pause and do an example and film it. Memory Segments •Your program is divided into 4 segments:

Project2C• Assignedtoday• DueFridayOct25• Parsingfloatingpointnumbers(e.g.,-3.68)

• Lotsofhintsinprompt

Page 36: Lecture 6: Memory, part 4ix.cs.uoregon.edu/~hank/212/lectures/CIS212_F19_Lec6.pdf · Pause and do an example and film it. Memory Segments •Your program is divided into 4 segments:

Enums,structs,typedef,union

Page 37: Lecture 6: Memory, part 4ix.cs.uoregon.edu/~hank/212/lectures/CIS212_F19_Lec6.pdf · Pause and do an example and film it. Memory Segments •Your program is divided into 4 segments:

Enums

• Enums makeyourowntype– Typeis“listofkeywords”

• Enums areusefulforcodeclarity– Alwayspossibletodothesamethingwithintegers

• Becarefulwithenums– …youcan“contaminate”abunchofusefulwords

Page 38: Lecture 6: Memory, part 4ix.cs.uoregon.edu/~hank/212/lectures/CIS212_F19_Lec6.pdf · Pause and do an example and film it. Memory Segments •Your program is divided into 4 segments:

enum example

Ckeyword“enum”–

meansenumdefinitionis

coming

Thisenumcontains6differentstudenttypes

semi-colon!!!

Page 39: Lecture 6: Memory, part 4ix.cs.uoregon.edu/~hank/212/lectures/CIS212_F19_Lec6.pdf · Pause and do an example and film it. Memory Segments •Your program is divided into 4 segments:

enum example

Page 40: Lecture 6: Memory, part 4ix.cs.uoregon.edu/~hank/212/lectures/CIS212_F19_Lec6.pdf · Pause and do an example and film it. Memory Segments •Your program is divided into 4 segments:

enums translatetointegers…andyoucansettheirrange

Page 41: Lecture 6: Memory, part 4ix.cs.uoregon.edu/~hank/212/lectures/CIS212_F19_Lec6.pdf · Pause and do an example and film it. Memory Segments •Your program is divided into 4 segments:

Butenums canbeeasiertomaintainthanintegers

Ifyouhadusedintegers,thenthisisabiggerchangeandlikelytoleadtobugs.

Page 42: Lecture 6: Memory, part 4ix.cs.uoregon.edu/~hank/212/lectures/CIS212_F19_Lec6.pdf · Pause and do an example and film it. Memory Segments •Your program is divided into 4 segments:

SimpleDataTypes

• float• double• int• char• unsignedchar

Allofthesearesimpledatatypes

Page 43: Lecture 6: Memory, part 4ix.cs.uoregon.edu/~hank/212/lectures/CIS212_F19_Lec6.pdf · Pause and do an example and film it. Memory Segments •Your program is divided into 4 segments:

Structs:acomplexdatatype

• Structs:mechanismprovidedbyCprogramminglanguagetodefineagroupofvariables– Variablesmustbegroupedtogetherincontiguousmemory

• Alsomakesaccessingvariableseasier…theyareallpartofthesamegrouping(thestruct)

Page 44: Lecture 6: Memory, part 4ix.cs.uoregon.edu/~hank/212/lectures/CIS212_F19_Lec6.pdf · Pause and do an example and film it. Memory Segments •Your program is divided into 4 segments:

struct syntaxCkeyword“struct”–

meansstructdefinitionis

coming

Thisstructcontains6doubles,meaningitis48bytes

semi-colon!!!

“.”accessesdatamembersforastruct

Declaringaninstance

Page 45: Lecture 6: Memory, part 4ix.cs.uoregon.edu/~hank/212/lectures/CIS212_F19_Lec6.pdf · Pause and do an example and film it. Memory Segments •Your program is divided into 4 segments:

Nestedstructs

accessesdirpartofRay

accessesdirectionZpartofDirection(partofRay)

Page 46: Lecture 6: Memory, part 4ix.cs.uoregon.edu/~hank/212/lectures/CIS212_F19_Lec6.pdf · Pause and do an example and film it. Memory Segments •Your program is divided into 4 segments:

typedef

• typedef:tellcompileryouwanttodefineanewtype

savesyoufromhavingtotype“struct”everytimeyoudeclareastruct.

Page 47: Lecture 6: Memory, part 4ix.cs.uoregon.edu/~hank/212/lectures/CIS212_F19_Lec6.pdf · Pause and do an example and film it. Memory Segments •Your program is divided into 4 segments:

Otherusesfortypedef

• Declareanewtypeforcodeclarity– typedef int MilesPerHour;• MakesanewtypecalledMilesPerHour.• MilesPerHour worksexactlylikeanint.

• Alsousedforenums &unions– sametrickasforstructs …typedef savesyouaword

Page 48: Lecture 6: Memory, part 4ix.cs.uoregon.edu/~hank/212/lectures/CIS212_F19_Lec6.pdf · Pause and do an example and film it. Memory Segments •Your program is divided into 4 segments:

Soimportant:struct datamemberaccessisdifferentwithpointers

Pointers:use“->”Instances(i.e.,notpointers):use“.”

Page 49: Lecture 6: Memory, part 4ix.cs.uoregon.edu/~hank/212/lectures/CIS212_F19_Lec6.pdf · Pause and do an example and film it. Memory Segments •Your program is divided into 4 segments:

Unions

• Union:specialdatatype– storemanydifferentmemorytypesinonememorylocation

Whendealingwiththisunion,youcantreatitasafloat,asanint,oras

4characters.

Thisdatastructurehas4bytes

Page 50: Lecture 6: Memory, part 4ix.cs.uoregon.edu/~hank/212/lectures/CIS212_F19_Lec6.pdf · Pause and do an example and film it. Memory Segments •Your program is divided into 4 segments:

Unions

Whyareunionsuseful?

Page 51: Lecture 6: Memory, part 4ix.cs.uoregon.edu/~hank/212/lectures/CIS212_F19_Lec6.pdf · Pause and do an example and film it. Memory Segments •Your program is divided into 4 segments:

UnionsExample

Page 52: Lecture 6: Memory, part 4ix.cs.uoregon.edu/~hank/212/lectures/CIS212_F19_Lec6.pdf · Pause and do an example and film it. Memory Segments •Your program is divided into 4 segments:

UnionsExample

Page 53: Lecture 6: Memory, part 4ix.cs.uoregon.edu/~hank/212/lectures/CIS212_F19_Lec6.pdf · Pause and do an example and film it. Memory Segments •Your program is divided into 4 segments:

BonusSlides

Page 54: Lecture 6: Memory, part 4ix.cs.uoregon.edu/~hank/212/lectures/CIS212_F19_Lec6.pdf · Pause and do an example and film it. Memory Segments •Your program is divided into 4 segments:

Importantnoteondebugging:bufferingandprintf

• Important:printf isbuffered• So:– printf putsstringinbuffer– otherthingshappen– bufferiseventuallyprinted

• Butwhataboutacrash?– printf putsstringinbuffer– otherthingshappen…includingacrash– bufferisneverprinted!

Onesolution:fflush(stdout)

Page 55: Lecture 6: Memory, part 4ix.cs.uoregon.edu/~hank/212/lectures/CIS212_F19_Lec6.pdf · Pause and do an example and film it. Memory Segments •Your program is divided into 4 segments:

Let’spauseanddoavideoonfflush

Page 56: Lecture 6: Memory, part 4ix.cs.uoregon.edu/~hank/212/lectures/CIS212_F19_Lec6.pdf · Pause and do an example and film it. Memory Segments •Your program is divided into 4 segments:

mv:Unixcommandforrenamingafile