![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:](https://reader034.vdocument.in/reader034/viewer/2022050520/5fa425480761f33f9233dfa1/html5/thumbnails/1.jpg)
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:](https://reader034.vdocument.in/reader034/viewer/2022050520/5fa425480761f33f9233dfa1/html5/thumbnails/2.jpg)
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:](https://reader034.vdocument.in/reader034/viewer/2022050520/5fa425480761f33f9233dfa1/html5/thumbnails/3.jpg)
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:](https://reader034.vdocument.in/reader034/viewer/2022050520/5fa425480761f33f9233dfa1/html5/thumbnails/4.jpg)
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:](https://reader034.vdocument.in/reader034/viewer/2022050520/5fa425480761f33f9233dfa1/html5/thumbnails/5.jpg)
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:](https://reader034.vdocument.in/reader034/viewer/2022050520/5fa425480761f33f9233dfa1/html5/thumbnails/6.jpg)
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:](https://reader034.vdocument.in/reader034/viewer/2022050520/5fa425480761f33f9233dfa1/html5/thumbnails/7.jpg)
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:](https://reader034.vdocument.in/reader034/viewer/2022050520/5fa425480761f33f9233dfa1/html5/thumbnails/8.jpg)
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:](https://reader034.vdocument.in/reader034/viewer/2022050520/5fa425480761f33f9233dfa1/html5/thumbnails/9.jpg)
![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:](https://reader034.vdocument.in/reader034/viewer/2022050520/5fa425480761f33f9233dfa1/html5/thumbnails/10.jpg)
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:](https://reader034.vdocument.in/reader034/viewer/2022050520/5fa425480761f33f9233dfa1/html5/thumbnails/11.jpg)
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:](https://reader034.vdocument.in/reader034/viewer/2022050520/5fa425480761f33f9233dfa1/html5/thumbnails/12.jpg)
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:](https://reader034.vdocument.in/reader034/viewer/2022050520/5fa425480761f33f9233dfa1/html5/thumbnails/13.jpg)
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:](https://reader034.vdocument.in/reader034/viewer/2022050520/5fa425480761f33f9233dfa1/html5/thumbnails/14.jpg)
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:](https://reader034.vdocument.in/reader034/viewer/2022050520/5fa425480761f33f9233dfa1/html5/thumbnails/15.jpg)
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:](https://reader034.vdocument.in/reader034/viewer/2022050520/5fa425480761f33f9233dfa1/html5/thumbnails/16.jpg)
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:](https://reader034.vdocument.in/reader034/viewer/2022050520/5fa425480761f33f9233dfa1/html5/thumbnails/17.jpg)
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:](https://reader034.vdocument.in/reader034/viewer/2022050520/5fa425480761f33f9233dfa1/html5/thumbnails/18.jpg)
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:](https://reader034.vdocument.in/reader034/viewer/2022050520/5fa425480761f33f9233dfa1/html5/thumbnails/19.jpg)
“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:](https://reader034.vdocument.in/reader034/viewer/2022050520/5fa425480761f33f9233dfa1/html5/thumbnails/20.jpg)
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:](https://reader034.vdocument.in/reader034/viewer/2022050520/5fa425480761f33f9233dfa1/html5/thumbnails/21.jpg)
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:](https://reader034.vdocument.in/reader034/viewer/2022050520/5fa425480761f33f9233dfa1/html5/thumbnails/22.jpg)
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:](https://reader034.vdocument.in/reader034/viewer/2022050520/5fa425480761f33f9233dfa1/html5/thumbnails/23.jpg)
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:](https://reader034.vdocument.in/reader034/viewer/2022050520/5fa425480761f33f9233dfa1/html5/thumbnails/24.jpg)
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:](https://reader034.vdocument.in/reader034/viewer/2022050520/5fa425480761f33f9233dfa1/html5/thumbnails/25.jpg)
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:](https://reader034.vdocument.in/reader034/viewer/2022050520/5fa425480761f33f9233dfa1/html5/thumbnails/26.jpg)
![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:](https://reader034.vdocument.in/reader034/viewer/2022050520/5fa425480761f33f9233dfa1/html5/thumbnails/27.jpg)
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:](https://reader034.vdocument.in/reader034/viewer/2022050520/5fa425480761f33f9233dfa1/html5/thumbnails/28.jpg)
![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:](https://reader034.vdocument.in/reader034/viewer/2022050520/5fa425480761f33f9233dfa1/html5/thumbnails/29.jpg)
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:](https://reader034.vdocument.in/reader034/viewer/2022050520/5fa425480761f33f9233dfa1/html5/thumbnails/30.jpg)
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:](https://reader034.vdocument.in/reader034/viewer/2022050520/5fa425480761f33f9233dfa1/html5/thumbnails/31.jpg)
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:](https://reader034.vdocument.in/reader034/viewer/2022050520/5fa425480761f33f9233dfa1/html5/thumbnails/32.jpg)
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:](https://reader034.vdocument.in/reader034/viewer/2022050520/5fa425480761f33f9233dfa1/html5/thumbnails/33.jpg)
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:](https://reader034.vdocument.in/reader034/viewer/2022050520/5fa425480761f33f9233dfa1/html5/thumbnails/34.jpg)
![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:](https://reader034.vdocument.in/reader034/viewer/2022050520/5fa425480761f33f9233dfa1/html5/thumbnails/35.jpg)
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:](https://reader034.vdocument.in/reader034/viewer/2022050520/5fa425480761f33f9233dfa1/html5/thumbnails/36.jpg)
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:](https://reader034.vdocument.in/reader034/viewer/2022050520/5fa425480761f33f9233dfa1/html5/thumbnails/37.jpg)
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:](https://reader034.vdocument.in/reader034/viewer/2022050520/5fa425480761f33f9233dfa1/html5/thumbnails/38.jpg)
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:](https://reader034.vdocument.in/reader034/viewer/2022050520/5fa425480761f33f9233dfa1/html5/thumbnails/39.jpg)
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:](https://reader034.vdocument.in/reader034/viewer/2022050520/5fa425480761f33f9233dfa1/html5/thumbnails/40.jpg)
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:](https://reader034.vdocument.in/reader034/viewer/2022050520/5fa425480761f33f9233dfa1/html5/thumbnails/41.jpg)
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:](https://reader034.vdocument.in/reader034/viewer/2022050520/5fa425480761f33f9233dfa1/html5/thumbnails/42.jpg)
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:](https://reader034.vdocument.in/reader034/viewer/2022050520/5fa425480761f33f9233dfa1/html5/thumbnails/43.jpg)
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:](https://reader034.vdocument.in/reader034/viewer/2022050520/5fa425480761f33f9233dfa1/html5/thumbnails/44.jpg)
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:](https://reader034.vdocument.in/reader034/viewer/2022050520/5fa425480761f33f9233dfa1/html5/thumbnails/45.jpg)
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:](https://reader034.vdocument.in/reader034/viewer/2022050520/5fa425480761f33f9233dfa1/html5/thumbnails/46.jpg)
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:](https://reader034.vdocument.in/reader034/viewer/2022050520/5fa425480761f33f9233dfa1/html5/thumbnails/47.jpg)
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:](https://reader034.vdocument.in/reader034/viewer/2022050520/5fa425480761f33f9233dfa1/html5/thumbnails/48.jpg)
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:](https://reader034.vdocument.in/reader034/viewer/2022050520/5fa425480761f33f9233dfa1/html5/thumbnails/49.jpg)
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:](https://reader034.vdocument.in/reader034/viewer/2022050520/5fa425480761f33f9233dfa1/html5/thumbnails/50.jpg)
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:](https://reader034.vdocument.in/reader034/viewer/2022050520/5fa425480761f33f9233dfa1/html5/thumbnails/51.jpg)
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:](https://reader034.vdocument.in/reader034/viewer/2022050520/5fa425480761f33f9233dfa1/html5/thumbnails/52.jpg)
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:](https://reader034.vdocument.in/reader034/viewer/2022050520/5fa425480761f33f9233dfa1/html5/thumbnails/53.jpg)
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:](https://reader034.vdocument.in/reader034/viewer/2022050520/5fa425480761f33f9233dfa1/html5/thumbnails/54.jpg)
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:](https://reader034.vdocument.in/reader034/viewer/2022050520/5fa425480761f33f9233dfa1/html5/thumbnails/55.jpg)
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:](https://reader034.vdocument.in/reader034/viewer/2022050520/5fa425480761f33f9233dfa1/html5/thumbnails/56.jpg)
mv:Unixcommandforrenamingafile