DOC PREVIEW
U of I CS 241 - C Basics

This preview shows page 1-2-3-4-5-32-33-34-35-65-66-67-68-69 out of 69 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 69 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 69 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 69 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 69 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 69 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 69 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 69 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 69 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 69 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 69 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 69 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 69 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 69 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 69 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 69 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

C BasicsAnnouncementsUnderstanding C / C++Slide 4Key DifferencesCommon MP MistakesC-StringsSlide 8Slide 9Slide 10strcat()Slide 12Slide 13Slide 14Slide 15C-Strings CorrectlyC-Strings (Correctly)Slide 18Slide 19Slide 20Slide 21Slide 22Slide 23Slide 24Lets look at some code…Slide 26Slide 27Slide 28Slide 29Slide 30Slide 31Slide 32Slide 33Slide 34Slide 35What’s in the stack frame?Slide 37Slide 38Slide 39Slide 40Slide 41Slide 42Slide 43Slide 44Slide 45Slide 46Slide 47Slide 48Slide 49Slide 50Slide 51What went wrong?Fix 1: Pass in the variableFix 2: Use heap memory…but we have valgrind!Using valgrind…mystring.cSlide 58Slide 59Slide 60Slide 61Slide 62mystring-2.cSlide 64Slide 65mystring-3.cSlide 67Slide 68Remember…1C BasicsMonday, August 31, 2009CS 241AnnouncementsMP0, a short machine problem, will be released today.Due: Monday, Sept. 7th at 11:59pm via svnMake sure to read README.txt and CS241.txt inside the MP before starting to code on the MP!2Understanding C / C++C++’s initial developmentProblem: Object oriented languages provided nice features to programmers, but were very, very slowThe development of C++ was an answer to provide objects to C.C++ was originally called “C with Classes” (1979)Result: C++ was developed from C3Understanding C / C++Result: C++ was developed from CEverything you can do in C++ (or Java, or C#, or Perl, or Python) can be done in C!All of the syntax you use in this class is valid C++. All of C++ syntax you’ve used, however, is not valid C.4Key DifferencesC does not have “iostreams”Must use printf() rather than coutprintf("hello world\n“); v. cout<<"hello world“<<endl;Must use malloc()/free() rather than new/delete to allocate heap memoryint *x = malloc(8 * sizeof(int)); free(x);int *x = new int[8]; delete(x);5Common MP MistakesFirst Mistake: Assuming memory has been set to 0 for you.6C-StringsIn C, there is not a data type “string”.Strings in C, sometimes called “C-strings”, are arrays of charactersC-strings are terminated by a NULL character (ASCII: 0, Text: '\0')7C-StringsConsider the code:char *s = “Hello World!”In memory:8H e l l o W o r l d ! \0sC-StringsIn Java, you can concatenate strings by:string s = s + " World!";In C, C-Strings are only pointers, so you can’t do:char *s = s + " World!";…you would get the sum of two memory locations!9C-StringsIn Java, you can concatenate strings by:string s = s + " World!";Instead, you have to use strcat():strcat(s, " World!");10strcat()strcat(char *destination, char *source) works by:Find the end of the destination stringAppend the source string to the end of the destination stringAdd a NULL to new destination string11C-StringsConsider the following code:char *s = malloc(11 * sizeof(char)); /* Allocate enough memory for an array of 11 characters, enough to store a 10-char long string. */strcat(s, "Hello");strcat(s, "World");12C-StringsConsider the following code:char *s = malloc(11 * sizeof(char)); /* Allocate enough memory for an array of 11 characters, enough to store a 10-char long string. */strcat(s, "Hello");strcat(s, "World");13??s?? ?? ?? ?? ?? ?? ?? ?? ?? ??C-StringsConsider the following code:char *s = malloc(11 * sizeof(char)); /* Allocate enough memory for an array of 11 characters, enough to store a 10-char long string. */strcat(s, "Hello");strcat(s, "World");14??s?? ?? ?? ?? ?? ?? ?? ?? ?? ??C-StringsConsider the following code:char *s = malloc(11 * sizeof(char)); /* Allocate enough memory for an array of 11 characters, enough to store a 10-char long string. */strcat(s, "Hello");strcat(s, "World");15??s?? ?? ?? ?? ?? ?? ?? ?? ?? ??Yikes! We don’t know where the C-string s actually ends! Since the OS doesn’t “clear” the memory for us, we’ll keep walking the memory until we find a ‘\0’.This could mean we write in memory we didn’t allocate (buffer overflow) or write in memory we can’t write in (segmentation fault)!C-Strings CorrectlyProblem: We never initialized our C-string to be empty!Fix: Initialize the first character to be empty!16C-Strings (Correctly)Consider the following code:char *s = malloc(11 * sizeof(char)); /* Allocate enough memory for an array of 11 characters, enough to store a 10-char long string. */s[0] = '\0';strcat(s, "Hello");strcat(s, "World");17C-Strings (Correctly)Consider the following code:char *s = malloc(11 * sizeof(char)); /* Allocate enough memory for an array of 11 characters, enough to store a 10-char long string. */s[0] = '\0';strcat(s, "Hello");strcat(s, "World");18??s?? ?? ?? ?? ?? ?? ?? ?? ?? ??C-Strings (Correctly)Consider the following code:char *s = malloc(11 * sizeof(char)); /* Allocate enough memory for an array of 11 characters, enough to store a 10-char long string. */s[0] = '\0';strcat(s, "Hello");strcat(s, "World");19\0s?? ?? ?? ?? ?? ?? ?? ?? ?? ??C-Strings (Correctly)Consider the following code:char *s = malloc(11 * sizeof(char)); /* Allocate enough memory for an array of 11 characters, enough to store a 10-char long string. */s[0] = '\0';strcat(s, "Hello");strcat(s, "World");20Hse l l o \0 ?? ?? ?? ?? ??C-Strings (Correctly)Consider the following code:char *s = malloc(11 * sizeof(char)); /* Allocate enough memory for an array of 11 characters, enough to store a 10-char long string. */s[0] = '\0';strcat(s, "Hello");strcat(s, "World");21Hse l l o W o r l d \0Common MP MistakesFirst Mistake: Assuming memory has been set to 0 for you.Common Related Mistake: You must always allocate to the length of the string plus one!strlen("Hello");…returns 5.…takes 6 (six) bytes to store in memory.22Common MP MistakesFirst Mistake: Assuming memory has been set to 0 for you.Second Mistake: Returning a variable in stack memory from a function.23Common MP MistakesSecond Mistake: Returning a variable in stack memory from a function.What is stack memory?24Lets look at some code…int b() { /* … */}int a() { /* … */ b();}int main(int argc, char **argv) { /* … */ a();}25At the beginning of the program, the OS will create a stack frame for the main() function.main()Stack


View Full Document

U of I CS 241 - C Basics

Documents in this Course
Process

Process

28 pages

Files

Files

37 pages

File I/O

File I/O

52 pages

Memory

Memory

23 pages

Threads

Threads

14 pages

Lecture

Lecture

55 pages

C Basics

C Basics

24 pages

Signals

Signals

27 pages

Memory

Memory

45 pages

Threads

Threads

47 pages

Threads

Threads

28 pages

LECTURE

LECTURE

45 pages

Threads

Threads

30 pages

Threads

Threads

55 pages

Files

Files

37 pages

SIGNALS

SIGNALS

22 pages

Files

Files

37 pages

Threads

Threads

14 pages

Threads

Threads

13 pages

Load more
Download C Basics
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view C Basics and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view C Basics 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?