Date post: | 01-Jan-2016 |
Category: |
Documents |
Upload: | ariel-doyle |
View: | 34 times |
Download: | 3 times |
CS 241Section 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 (#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 DITDAH{_DAHDITDITDAH _DIT,DITDAHDAH_,DITDAH DIT_,DITDAH _DIT_,DITDAHDIT_DAH DITDAH,DITDAH DAH_DIT DITDAH;DAHDITDIT _DIT=DIT_DAH DIT 81DAH,DIT_=_DIT__DAH;_DIT==DAH_DIT DIT _DITDAH;__DITDIT'\n'DAH DAH DAHDIT DITDAH_=_DIT;DITDAHDAH_;__DIT DIT DITDAH_DIT_?_DAH DIT DITDAHDIT_ DAH:'?'DAH,__DITDIT' 'DAH,DAH_ __DAH DAH DAHDITDITDITDAH DIT_=2,_DIT_=_DAH_;DITDAH _DIT_&&DITDITDAH _DIT_!=DIT DITDAH DAH_>='a'?DITDAHDAH_&223:DITDAH DAH_ DAH DAH;DITDITDAH DIT_ DAH __DAH,_DIT___DAH DAHDITDAH DIT_+= DIT DITDAH _DIT_>='a'? DITDAH _DIT_-'a':0DAH;}_DAH DIT DIT_ DAH{ __DIT DITDIT_>3?_DAH DIT DIT_>>1 DAH:'\0'DAH;returnDIT_&1?'-':'.';}__DIT DIT DIT_ DAH _DAHDITDIT_;{DIT void DAH write DIT1,&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 DITDAH{_DAHDITDITDAH _DIT,DITDAHDAH_,DITDAH DIT_,DITDAH _DIT_,DITDAHDIT_DAH DITDAH,DITDAH DAH_DIT DITDAH;DAHDITDIT _DIT=DIT_DAH DIT 81DAH,DIT_=_DIT__DAH;_DIT==DAH_DIT DIT _DITDAH;__DITDIT'\n'DAH DAH DAHDIT DITDAH_=_DIT;DITDAHDAH_;__DIT DIT DITDAH_DIT_?_DAH DIT DITDAHDIT_ DAH:'?'DAH,__DITDIT' 'DAH,DAH_ __DAH DAH DAHDITDITDITDAH DIT_=2,_DIT_=_DAH_;DITDAH _DIT_&&DITDITDAH _DIT_!=DIT DITDAH DAH_>='a'?DITDAHDAH_&223:DITDAH DAH_ DAH DAH;DITDITDAH DIT_ DAH __DAH,_DIT___DAH DAHDITDAH DIT_+= DIT DITDAH _DIT_>='a'? DITDAH _DIT_-'a':0DAH;}_DAH DIT DIT_ DAH{ __DIT DITDIT_>3?_DAH DIT DIT_>>1 DAH:'\0'DAH;returnDIT_&1?'-':'.';}__DIT DIT DIT_ DAH _DAHDITDIT_;{DIT void DAH write DIT1,&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> //strcatvoid 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 #2void 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 #2constant int maxFieldSize=20void 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