inst.eecs.berkeley.edu/~cs61c CS61C : Machine Structurescs61c/sp08/lectures/03/2008SpCS61C-L03... · •HW0 due in discussion this week •HW1 due this Fri @ 23:59 PST •HW2 due
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
•Great run-time performance: generallymuch faster than Scheme or Java forcomparable code (because itoptimizes for a given architecture)•OK compilation time: enhancementsin compilation procedure (Makefiles)allow only modified files to berecompiled
•All compiled files (including theexecutable) are architecture specific,depending on both the CPU type andthe operating system.•Executable must be rebuilt on eachnew system.
• Called “porting your code” to a newarchitecture.
•The “change→compile→run [repeat]”iteration cycle is slow
C Syntax: main•To get the main function to acceptarguments, use this:int main (int argc, char *argv[])
•What does this mean?•argc will contain the number of stringson the command line (the executablecounts as one, plus one for eachargument). Here argc is 2:unix% sort myFile
•argv is a pointer to an array containingthe arguments as strings (more onpointers later).
•An address refers to a particularmemory location. In other words, itpoints to a memory location.•Pointer: A variable that contains theaddress of a variable.
Pointers•How to create a pointer:& operator: get address of a variable
int *p, x; p ? x ?
x = 3;p ? x 3
p =&x;p x 3
•How get a value pointed to? * “dereference operator”: get value pointed to
printf(“p points to %d\n”,*p);
Note the “*” gets used2 different ways inthis example. In thedeclaration to indicatethat p is going to be apointer, and in theprintf to get thevalue pointed to by p.
•Pointers are used to point to any datatype (int, char, a struct, etc.).•Normally a pointer can only point toone type (int, char, a struct, etc.).•void * is a type that can point toanything (generic pointer)
• Use sparingly to help avoid programbugs… and security issues… and a lotof other bad things!
•All declarations go at the beginning ofeach function except if you use C99.•Only 0 and NULL evaluate to FALSE.•All data is in memory. Each memorylocation has an address to use to referto it and a value stored in it.•A pointer is a C version of theaddress.* “follows” a pointer to its value& gets the address of a value
Administrivia : You have a question?• Do not email Dan (& expect response)
• Hundreds of emails in inbox• Email doesnʼt scale to classes with 200+ students!
• Tips on getting an answer to your question:• Ask a classmate• Ask Dan after or before lecture• The newsgroup, ucb.class.cs61c
Read it : Has your Q been answered already? If not, ask it and check back
• Ask TA in section, lab or OH• Ask Dan in OH• Ask Dan in lecture (if relevant to lecture)• Send your TA email• Send your Head TAs email• Send Dan email