Presentation is loading. Please wait.

Presentation is loading. Please wait.

C Strings Doing strings the old fashioned way. strings vs c-strings C++ strings are an object data type – State : list of characters – Can ask it to perform.

Similar presentations


Presentation on theme: "C Strings Doing strings the old fashioned way. strings vs c-strings C++ strings are an object data type – State : list of characters – Can ask it to perform."— Presentation transcript:

1 C Strings Doing strings the old fashioned way

2 strings vs c-strings C++ strings are an object data type – State : list of characters – Can ask it to perform behaviors: myString.erase() myString.substr() … C does not have objects…

3 C-Strings "C –String" : arrays of chars: – string literal can initialize

4 C-Strings "C –String" : arrays of chars: – After that – no aggregate assignment:

5 Mysteries: This: Shows in debugger as:

6 Mystery… Can specify array size – But 3 chars isn't enough…

7 Mysteries: What the hell?

8 Null Terminated Need to tell when strings ends – arrays don't store size – char with value of 0 ends a string 0123456 651101001141011190 Andrew\0

9 0 vs \0 0 in a string or char means "The digit 0" – char value is 48 To say char value 0 use: \0

10 0 mean No \0 says string is done: – This c-string would not stop Keep reading adjacent parts of memory until we hit byte with value 0!!! 012345 65110100114101119 Andrew

11 0 mean No \0 says string is done – Most code thinks this is 3 letters long: 0123456 6511010001011190 And\0ew

12 How long is it??? How long is this c-string?

13 How long is it??? How long is this c-string? The array is 100 elements long The length of the c-string is 6 The c-string is stored in 7 elements of space 0123456… 651101001141011190… Andrew\0???

14 String carries length with it Pass by reference for efficiency Working With C++ Strings

15 Array passing already efficient Pass length or just read until \0 Working With C-Strings

16 Buffer Overflow Arbitrary sized data read into fixed sized container can produce overflow or underflow

17 Heartbleed

18

19

20 Overflow Overflow can write into existing data or code

21 Overflow Memory View Before reading middle name: After:

22 Reading Safely scanf("%s", myCString) potentially unsafe fgets(myCString, size, stdin) only reads in size -1 chars – Prevent overflow

23 C String Functions

24 Functions C provides functions to do comparisons, find things, etc… C++ LibraryC Library

25 Char * What is this?

26 Pointers * means "memory address" char * : address where you can find a char

27 Pointers and Arrays Arrays are passed as memory address These are equivalent:

28 Converting c-string  string – Assignment… string will figure it out

29 Converting string  c-string: – String object's c_str() behavior


Download ppt "C Strings Doing strings the old fashioned way. strings vs c-strings C++ strings are an object data type – State : list of characters – Can ask it to perform."

Similar presentations


Ads by Google