1 / 29

CSE 303 Concepts and Tools for Software Development

Learn C programming concepts, memory management, and debugging techniques. Explore the C memory model, functions, and process operation. Includes code examples and execution explanations.

rosadavis
Download Presentation

CSE 303 Concepts and Tools for Software Development

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CSE 303Concepts and Tools for Software Development Richard C. DavisUW CSE – 10/11/2006 Lecture 7 – Introduction to C

  2. Administravia • Extra Office Hours • Richard: Thursday (10/12), CSE 210 CSE 303 Lecture 7

  3. Tip of the Day • ACM Student Chapter Tutorials • Accessible from CSE 303 “Comp. Resources” • Fast way to find way around Linux • Printing • Lists of common commands • Emacs tutorials • Language tutorials • Sed, Awk, Python • C, C++ • Tool Tutorials ( CSE 303 Lecture 7

  4. Where are We? • Before: Shell • Files • Users • Processes • Now: C and C++ (for “processes”) • Numbers • Memory CSE 303 Lecture 7

  5. Welcome to C • Comparing to Java: • Lower Level: good for compiler, bad for you • Unsafe: wrong programs can do anything • “Standard Library” is smaller • Similar Syntax: pan help or confuse • Procedural: Not “object oriented” • Java-like thinking can get you in trouble! CSE 303 Lecture 7

  6. Plan for Learning C • Learn “C Style” Programming • Syntax • Pointers & Memory Management • Good idioms • Why use C? • Later • “C++ Style” Programming • Development Tools CSE 303 Lecture 7

  7. Learning Method • We’ll Look at How C Programs Run • Not promised by C’s definition • Allows reasoning about the implementation • Know why C does what it does • Avoid this in general! • But sometimes need to debug programs • Note: Text does not cover this in detail • Notes will be important CSE 303 Lecture 7

  8. Today • C Memory Model • Simple C Programs • Introduction to Pointers CSE 303 Lecture 7

  9. Address Space of Unix Process 0xFFFFFFFF Address space is an array of 8-bit bytes Stack(Dynamically Allocated) An index into this array is called an address Heap(Dynamically Allocated) Address Space Typical total size is 232 Static Data(“Data Segment”) We’ll assume that an integer is 4 bytes Code(“Text Segment”) 0x00000000 CSE 303 Lecture 7

  10. More on the Address Space • Read an unused part of memory? • May cause “segmentation fault” (crash) • Code: instructions for program (read only) • Static Data: usually global variables • Stack: local variables and code addresses • Grows and shrinks as program executes • Heap: data (like objects from Java’s new) • Managed manually CSE 303 Lecture 7

  11. Hello World #include <stdio.h> /* * First C program */ int main() { // Print to the stream "stdout" fputs("Hello World\n",stdout); return 0; } CSE 303 Lecture 7

  12. Testing Hello World • Compiling and running hello.c • Compile: gcc –g –Wall –o hi hello.c • Run:./hi • Compile Command Meaning • gcc : Gnu C Compiler • -g : include debugging information • -Wall : show all warnings (good for learning) • -o hi : specifies program name • Omit? Program is named a.out CSE 303 Lecture 7

  13. Header Files • #include <stdio.h> • Directive to C Preprocessor (more later) • Finds stdio.h and includes entire contents • stdio.h is a “header” file • stdio.h describes fputs and stdout CSE 303 Lecture 7

  14. Functions • fputs • Function that sends characters to a stream • \n is an escape sequence for “newline” • main • Every C program starts execution here • Functions like Java methods, but… • Not part of a class • Not associated with an object (no “this) CSE 303 Lecture 7

  15. How Processes Operate • Load Code/Static Data into memory • Store current “Stack Frame” • Address of current instruction • Local variables • Calling “Functions” (like Methods in Java) • Creates new stack frame • Current instruction becomes “return address” • On exit, delete all memory CSE 303 Lecture 7

  16. Execution Example #include <stdio.h> int main() { int integer1; int integer2; int sum; integer1 = 10; integer2 = 20; sum = integer1 + integer2; printf(“\nSum is %d”, sum); return 0; } Stack after line 4 1 2 3 4 5 6 7 8 9 integer1 XXXX integer2 XXXX sum XXXX CSE 303 Lecture 7

  17. Execution Example #include <stdio.h> int main() { int integer1; int integer2; int sum; integer1 = 10; integer2 = 20; sum = integer1 + integer2; printf(“\nSum is %d”, sum); return 0; } Stack after line 5 1 2 3 4 5 6 7 8 9 integer1 10 integer2 XXXX sum XXXX CSE 303 Lecture 7

  18. Execution Example #include <stdio.h> int main() { int integer1; int integer2; int sum; integer1 = 10; integer2 = 20; sum = integer1 + integer2; printf(“\nSum is %d”, sum); return 0; } Stack after line 6 1 2 3 4 5 6 7 8 9 integer1 10 integer2 20 sum XXXX CSE 303 Lecture 7

  19. Execution Example #include <stdio.h> int main() { int integer1; int integer2; int sum; integer1 = 10; integer2 = 20; sum = integer1 + integer2; printf(“\nSum is %d”, sum); return 0; } Stack after line 7 1 2 3 4 5 6 7 8 9 integer1 10 integer2 20 sum 30 CSE 303 Lecture 7

  20. Execution Example Stack during execution of printf #include <stdio.h> int main() { int integer1; int integer2; int sum; integer1 = 10; integer2 = 20; sum = integer1 + integer2; printf(“\nSum is %d”, sum); return 0; } 1 2 3 4 5 6 7 8 9 integer1 10 integer2 20 sum 30 activation record for printf CSE 303 Lecture 7

  21. Activation Records Return Address (code) Address for returned value Argument 1 Argument 2 … Local Variable 1 Local Variable 2 … CSE 303 Lecture 7

  22. Introduction to Pointers • Pointer = variable that holds an address • Declaring a pointer • int *mypointer; • Assigning an address to a pointer • mypointer = &integer1; • Accessing data pointed to by a pointer • *mypointer CSE 303 Lecture 7

  23. Pointer Execution Example #include <stdio.h> int main() { int integer1; int* mypointer; integer1 = 10; mypointer = &integer1; printf(“\nValue is %d”, integer1); printf(“\nValue is %d”, *mypointer); return 0; } Stack after line 3 1 2 3 4 5 6 7 8 9 integer1 XXXX mypointer XXXX CSE 303 Lecture 7

  24. Pointer Execution Example #include <stdio.h> int main() { int integer1; int* mypointer; integer1 = 10; mypointer = &integer1; printf(“\nValue is %d”, integer1); printf(“\nValue is %d”, *mypointer); return 0; } Stack after line 4 1 2 3 4 5 6 7 8 9 integer1 10 mypointer XXXX CSE 303 Lecture 7

  25. Pointer Execution Example #include <stdio.h> int main() { int integer1; int* mypointer; integer1 = 10; mypointer = &integer1; printf(“\nValue is %d”, integer1); printf(“\nValue is %d”, *mypointer); return 0; } Stack after line 4 1 2 3 4 5 6 7 8 9 integer1 10 mypointer CSE 303 Lecture 7

  26. Why C is Dangerous • C deals with variables, functions, etc… • But Programmer must keep bits straight! • arr is array of 10 elements: arr[30] is what? • Could be anything • Writing 3263827 to a return address • Program could start running from anywhere • Why would anyone work like this? • Fast: No “unnecessary” overhead • Need to for embedded/operating systems CSE 303 Lecture 7

  27. Summary • C Memory Model • Simple C Programs • Introduction to Pointers CSE 303 Lecture 7

  28. Reading • Skim Sections that look familiar! • Author does not assume you know Java • C++ book does assume you know Java • Programming in C • Chapter 1: Introduction • Chapter 2: Fundamentals • Chapter 3: Compiling and Running • Chapter 11: (pp235-240) Pointers CSE 303 Lecture 7

  29. Next Time • C Program Structure • Expressions • Local Variables • Left vs. Right expressions • Dangling Pointers CSE 303 Lecture 7

More Related