220 likes | 320 Vues
CS 241 Section Week #1. About Sections. Each week: We’ll spend additional time on topics that the instructors feel should be reviewed. We’ll prepare you for the upcoming homework or MP submissions. We’ll provide extra review/guidance for upcoming exams. C can be Ugly. #define DIT (
E N D
About Sections • Each week: • We’ll spend additional time on topics that the instructors feel should be reviewed. • We’ll prepare you for the upcoming homework or MP submissions. • We’ll provide extra review/guidance for upcoming exams.
C can be Ugly #define DIT ( #define DAH ) #define __DAH ++ #define DITDAH * #define DAHDIT for #define DIT_DAH malloc #define DAH_DIT gets #define _DAHDIT char _DAHDIT _DAH_[]="ETIANMSURWDKGOHVFaLaPJBXCYZQb54a3d2f16g7c8a90l?e'b.s;i,d:" ;main DIT DAH{_DAHDIT DITDAH _DIT,DITDAH DAH_,DITDAH DIT_, DITDAH _DIT_,DITDAH DIT_DAH DIT DAH,DITDAH DAH_DIT DIT DAH;DAHDIT DIT _DIT=DIT_DAH DIT 81 DAH,DIT_=_DIT __DAH;_DIT==DAH_DIT DIT _DIT DAH;__DIT DIT'\n'DAH DAH DAHDIT DIT DAH_=_DIT;DITDAH DAH_;__DIT DIT DITDAH _DIT_?_DAH DIT DITDAH DIT_ DAH:'?'DAH,__DIT DIT' 'DAH,DAH_ __DAH DAH DAHDIT DIT DITDAH DIT_=2,_DIT_=_DAH_; DITDAH _DIT_&&DIT DITDAH _DIT_!=DIT DITDAH DAH_>='a'? DITDAH DAH_&223:DITDAH DAH_ DAH DAH; DIT DITDAH DIT_ DAH __DAH,_DIT_ __DAH DAH DITDAH DIT_+= DIT DITDAH _DIT_>='a'? DITDAH _DIT_-'a':0 DAH;}_DAH DIT DIT_ DAH{ __DIT DIT DIT_>3?_DAH DIT DIT_>>1 DAH:'\0'DAH;return DIT_&1?'-':'.';}__DIT DIT DIT_ DAH _DAHDIT DIT_;{DIT void DAH write DIT 1,&DIT_,1 DAH;}
C can be Ugly #define DIT ( #define DAH ) #define __DAH ++ #define DITDAH * #define DAHDIT for #define DIT_DAH malloc #define DAH_DIT gets #define _DAHDIT char _DAHDIT _DAH_[]="ETIANMSURWDKGOHVFaLaPJBXCYZQb54a3d2f16g7c8a90l?e'b.s;i,d:" ;main DIT DAH{_DAHDIT DITDAH _DIT,DITDAH DAH_,DITDAH DIT_, DITDAH _DIT_,DITDAH DIT_DAH DIT DAH,DITDAH DAH_DIT DIT DAH;DAHDIT DIT _DIT=DIT_DAH DIT 81 DAH,DIT_=_DIT __DAH;_DIT==DAH_DIT DIT _DIT DAH;__DIT DIT'\n'DAH DAH DAHDIT DIT DAH_=_DIT;DITDAH DAH_;__DIT DIT DITDAH _DIT_?_DAH DIT DITDAH DIT_ DAH:'?'DAH,__DIT DIT' 'DAH,DAH_ __DAH DAH DAHDIT DIT DITDAH DIT_=2,_DIT_=_DAH_; DITDAH _DIT_&&DIT DITDAH _DIT_!=DIT DITDAH DAH_>='a'? DITDAH DAH_&223:DITDAH DAH_ DAH DAH; DIT DITDAH DIT_ DAH __DAH,_DIT_ __DAH DAH DITDAH DIT_+= DIT DITDAH _DIT_>='a'? DITDAH _DIT_-'a':0 DAH;}_DAH DIT DIT_ DAH{ __DIT DIT DIT_>3?_DAH DIT DIT_>>1 DAH:'\0'DAH;return DIT_&1?'-':'.';}__DIT DIT DIT_ DAH _DAHDIT DIT_;{DIT void DAH write DIT 1,&DIT_,1 DAH;} Especially when you try! (More examples at www.ioccc.org)
Topics This Section • SVN • C Code Examples in Real Life • Programming Tools
Subversion What it is • Collaboration tool for large projects • Good at Code Backups • Efficient - Uses diff's for backup compression • A good learning block for other version control systems (git, etc.)
Subversion What it is not • File system backup (bad at binaries) • Concurrent access tool (not google docs) • Good at merging lots of changes (commit often)
Try it! • svn checkout https://subversion.ews.illinois.edu/svn/sp11-cs241/NETID/ svn • If you have already checked out the repository, run `svn update` inside the directory
Try it! • cd ~/svn (what does ~ mean) • echo “this file holds my idea” > idea • ls && svn add idea(what does && do) • svn status • svn commit -m “my first big idea”
Try it! • Edit the file idea and save • Commit the changes (Do you remember the command) • Oh no, you ruined your first idea and want to go back!
Going back • svn log • svn up (short for?) • svn log • svn update -rXX
SVN • Conclusion • Learn it • Love it • Hate it
C examples • Go to ~/svn/ds/ds1 • Time for some real fun! • Open ds1.c using your favorite editor
Fun Part #1 • void problem1(){ • char str[7]="abc"; • strcat(str,"def"); • printf("%s",str); • } • //Issues ?
Fun Part #1 • Are you ready for the answers on the next slide? • Did you use the manpages for strcat?
Fun Part #1 • #include <string.h> //strcat • void problem1(){ • char str[7]; //avoid ptr to static mem • strcat(str,"abc"); • strcat(str,"def"); • printf("%s",str); • }
Test • gcc ds.c • What is binary called? • Does it work? • Uncomment Problem2
Fun Part #2 • void problem2(){ • char *str; • for(int i=0;i<42;i+1) • str = malloc( sizeof(char)); • if( factorial(i,str) ){ //Error • return 1; • } • printf("%d : %s\n",str,i); • } • int factorial(int num, const char* answer){ • while(num >= 0){ • num *= --num; • sprintf(answer,"%d",num); • }
Fun Part #2 • How many bugs can you find? • Once you are confident test your program
Fun Part #2 Are you ready for the answers? • Try running `valgrind a.out`
Fun Part #2 • constant int maxFieldSize=20 • void problem2(){ • char *str; • int i; • constant int theAnswer = 42; • str = malloc( sizeof(char)*(maxFieldSize) ); • for(i=0;i<theAnswer;i++){ • if( factorial(i,str) ){ //Error • printf(“factorial failed\n”); • exit(1); • } • printf("%d : %s\n",i,str); • } • } • int factorial(int num, char* answer){ • while(num > 0){ • num *= num--; • snprintf(answer,maxFieldSize,"%d",num); • }
Questions? • As a challenge see if you can optimize factorial for subsequent accesses - make sure that it still works for the general case