27
Chapter Nine Chapter Nine Strings Strings

Chapter Nine

  • Upload
    torin

  • View
    29

  • Download
    0

Embed Size (px)

DESCRIPTION

Chapter Nine. Strings. Char vs String Literals. Size of data types: sizeof(“hello\n”)7 bytes sizeof(“hello”)6 bytes sizeof(“X”)2 bytes sizeof(‘x’)1 byte Every string in C++ is an array of characters with a NULL terminator to mark the end - PowerPoint PPT Presentation

Citation preview

Page 1: Chapter Nine

Chapter NineChapter Nine

StringsStrings

Page 2: Chapter Nine

                                                                                                                              

                                                      

                                                     

Page 3: Chapter Nine

Char vs String LiteralsChar vs String Literals Size of data types:Size of data types:

– sizeof(“hello\n”)sizeof(“hello\n”) 7 bytes7 bytes– sizeof(“hello”)sizeof(“hello”) 6 bytes 6 bytes – sizeof(“X”)sizeof(“X”) 2 bytes2 bytes– sizeof(‘x’)sizeof(‘x’) 1 byte1 byte

Every string in C++ is an array of characters Every string in C++ is an array of characters with a NULL terminator to mark the endwith a NULL terminator to mark the end

An array of characters w/o a NULL is not a An array of characters w/o a NULL is not a stringstring

Page 4: Chapter Nine

char st3[5] will not compile: “initializer-string for array of chars is too long” - no room for \0

Page 5: Chapter Nine

String I/O FunctionsString I/O Functions

All library functions assume your array of characters All library functions assume your array of characters includes a NULL terminator. If not, result may be includes a NULL terminator. If not, result may be “segmentation fault, core dumped”“segmentation fault, core dumped”

iostream.hiostream.hcoutcout prints all characters up to prints all characters up to \\00cincin reads a word (stops at a space) into a reads a word (stops at a space) into a

char array, appends a char array, appends a \\00cin.getlinecin.getline reads a complete line of input into a reads a complete line of input into a

char array, appends a char array, appends a \\00cin.getcin.get reads a single character, no reads a single character, no \\00

Page 6: Chapter Nine
Page 7: Chapter Nine

getline()getline() Symtax Symtax

getlinegetline, one of cin’s functions, uses default arguments , one of cin’s functions, uses default arguments for the length of the line to read, and the character to for the length of the line to read, and the character to stop on.stop on.

void void cin.getlinecin.getline(char str[], int = 80, char = ‘\n’);(char str[], int = 80, char = ‘\n’); dot cin’s getline functiondot cin’s getline function

cin.getline(name,80,’\n’);cin.getline(name,80,’\n’); read at most 80 chars read at most 80 chars stop on newlinestop on newline

cin.getline(name, 40, ‘\t’);cin.getline(name, 40, ‘\t’); read at most 40 chars read at most 40 chars stop on tabstop on tab

Page 8: Chapter Nine

Size mattersSize matters

If maximum length of first name is 20If maximum length of first name is 20

char first[21];char first[21];

If maximum length of last name is 30If maximum length of last name is 30

char last[31];char last[31];

If there will be at most two spaces between first and last If there will be at most two spaces between first and last names:names:

char name[53];char name[53]; // 20 + 2 + 30 + \0// 20 + 2 + 30 + \0

Invalid size character array declaration is a common Invalid size character array declaration is a common cause of errors and system vulnerabilitiescause of errors and system vulnerabilities

Page 9: Chapter Nine

ReminderReminder

Strings are arrays of characters with Strings are arrays of characters with a NULL marker or sentinel at the enda NULL marker or sentinel at the end

Array names are constant pointers to Array names are constant pointers to the element typethe element type

Other pointers can be used to access Other pointers can be used to access elements (offset or indices)elements (offset or indices)

NULL is defined to be 0 (false)NULL is defined to be 0 (false)

Page 10: Chapter Nine

                                                                                                                              

                                                      

                                                      Slide 3 of 32

// When loop finished, append NULL

Page 11: Chapter Nine

                                                                                                                              

                                                      

                                                      Slide 4 of 32

Page 12: Chapter Nine

                                                                                                                              

                                                      

                                                      Slide 5 of 32

This final version is very similar to the strcpy() included in the C++ string library

Page 13: Chapter Nine

Visiting the LibraryVisiting the Library

C++ has an extensive library of C++ has an extensive library of string-handling functionsstring-handling functions

Every C++ string function expects a Every C++ string function expects a pointer to an array of characters pointer to an array of characters terminated by a NULL characterterminated by a NULL character

Unterminated strings are a common Unterminated strings are a common cause of segmentation faultscause of segmentation faults

Failure to check bounds is a common Failure to check bounds is a common cause of system vulnerabilitiescause of system vulnerabilities

Page 14: Chapter Nine

                                                                                                                              

                                                      

                                                      Slide 7 of 32

Page 15: Chapter Nine

                                                                                                                              

                                                      

                                                      Slide 8 of 32

Page 16: Chapter Nine

                                                                                                                              

                                                      

                                                      Slide 9 of 32

Page 17: Chapter Nine

                                                                                                                              

                                                      

                                                      Slide 10 of 32

Page 18: Chapter Nine

                                                                                                                              

                                                      

                                                      Slide 11 of 32

Page 19: Chapter Nine

                                                                                                                              

                                                      

                                                      Slide 12 of 32

Page 20: Chapter Nine

Slide 13 of 32

Page 21: Chapter Nine

                                                                                                                              

                                                      

                                                      Slide 14 of 32

Page 22: Chapter Nine

                                                                                                                              

                                                      

                                                      Slide 15 of 32

Page 23: Chapter Nine

                                                                                                                              

                                                      

                                                      Slide 16 of 32

Page 24: Chapter Nine

                                                                                                                              

                                                      

                                                      Slide 18 of 32 Note how much more memory is required by c4 – 80 bytes vs 16 bytes for c5 + 15 bytes for the strings stored “somewhere”

Page 25: Chapter Nine

                                                                                                                              

                                                      

                                                      Slide 19 of 32

Page 26: Chapter Nine

                                                                                                                              

                                                      

                                                      Slide 20 of 32

Page 27: Chapter Nine

ErrorsErrors

char msg[5] = “Error”; // not enough spacechar msg[5] = “Error”; // not enough space

char line[80];char line[80]; // for 80 char line, need 81 in // for 80 char line, need 81 in arrayarray

Forgetting to put NULL into stringForgetting to put NULL into string

Reading beyond end of array. Often happens Reading beyond end of array. Often happens because library routine expects to find a NULLbecause library routine expects to find a NULL

Buffer overflow. Writing beyond end of arrayBuffer overflow. Writing beyond end of array

char *newloc;char *newloc;

strcpy(newloc,“Don’t try this”);strcpy(newloc,“Don’t try this”);

// need char* but must also have a block of // need char* but must also have a block of memory set asidememory set aside