CS 2130 - PowerPoint PPT Presentation

1 / 44
About This Presentation
Title:

CS 2130

Description:

Hello world. Strings. We implement string-like data structures using character arrays ... hello. buffer = hello. Well behaved? C: demo ... – PowerPoint PPT presentation

Number of Views:79
Avg rating:3.0/5.0
Slides: 45
Provided by: ble87
Category:
Tags: hello

less

Transcript and Presenter's Notes

Title: CS 2130


1
CS 2130
  • Presentation 10
  • Strings
  • Library Fun Facts

2
Review
  • Declared Scope Storage
    Class Initialized
  • Outside all blocks 1 Global 1 static
    1 Once
  • 2 File 2 auto
    2 Each time
  • 3 Local
  • static outside all blocks 1 Global 1 static
    1 Once
  • 2 File 2 auto
    2 Each time
  • 3 Local
  • Inside a block 1 Global 1 static
    1 Once
  • 2 File 2 auto
    2 Each time
  • 3 Local
  • static inside a block 1 Global 1 static
    1 Once
  • 2 File 2 auto
    2 Each time
  • 3 Local

3
Strings
  • Important There are no strings in C

4
Strings
  • Many languages include built-in support for
    Strings
  • Typical examples include
  • Basic
  • Pascal
  • Scheme/Lisp
  • Java
  • "Built-in" here implies behind the scenes details
    are managed by the compiler and/or the run-time
    environment

5
Strings in C
  • There are no Strings in C.
  • There is an agreed upon convention to store
    string like data structures in arrays of
    characters.
  • Part of the convention includes terminating
    strings with a null character.
  • In many cases "string" operations in C look just
    like those found in other languages.
  • Do not be deceived. This is C...memory must be
    managed properly (by you)!

6
Compare
  • Basic
  • 10 a "Hello "
  • 20 b "world"
  • 30 a a b
  • 40 print a
  • run
  • Hello World
  • C
  • char a "Hello "
  • char b "world"
  • strcat(a, b)
  • printf("s\n", a)

7
Better?
  • char a12 "Hello "
  • char b "world"
  • strcat(a, b)
  • printf("s\n", a)

8
What's happening behind the scenes in Basic?
  • Who cares!?!?!?

9
What's happening in C?
  • char a12 "Hello "

a
H
e
l
l
o

\0
?
10
What's happening in C?
  • char a12 "Hello "
  • char b "world"

a
H
e
l
l
o

\0
b
w
o
r
l
d
\0
11
What's happening in C?
  • char a12 "Hello "
  • char b "world"
  • strcat(a, b)

a
H
e
l
l
o

w
o
r
l
d
\0
b
w
o
r
l
d
\0
12
What's happening in C?
  • char a12 "Hello "
  • char b "world"
  • strcat(a, b)
  • printf("s\n", a)

a
H
e
l
l
o

w
o
r
l
d
\0
b
w
o
r
l
d
\0
Hello world
13
Strings
  • We implement string-like data structures using
    character arrays
  • char s110 / Not a string! /
  • char s2 / Not a string! /
  • char s310 "foo" / Not a string /
  • char s4 "bar"
  • What acts like a string?
  • A pointer to a character followed by a sequence
    of characters terminating in a null byte
  • char s510 "foo"
  • f o o \0 ?? ?? ?? ?? ?? ??

14
Strings
  • char s110 "foo"
  • printf("s\n", s1)
  • Works to print
  • foo
  • Note If s1 is not null terminated the printf
    will print a character string until it encounters
    a null character or causes an error (segment
    fault etc.)

15
Strings
  • char s110
  • s10 'f'
  • s11 'o'
  • s12 'o'
  • printf("s\n", s1)
  • Sample output
  • fooâ-VGot 2
  • ,_VGot 2
  • Got 20
  • _VGot 2
  • _Got 16
  • Got 1

16
Strings
  • char s110 "foo"
  • char s2 "superduper"
  • foo\0 is stored in space allocated for s1 (an
    array)
  • on stack
  • or in changeable data area
  • plus?
  • superduper\0 is stored in the constant data area
    with s2 pointing to it.

17
Strings
  • char s110 "foo"
  • char s2 "superduper"
  • sizeof(s1) ?
  • 1) 4
  • 2) 10
  • 3) Depends
  • 4) 11

18
Strings
  • char s110 "foo"
  • char s2 "superduper"
  • sizeof(s2) ?
  • 4

19
Strings
  • char s110 "foo"
  • char s2 "superduper"
  • sizeof(s2) ?
  • 1) 1
  • 2) 4
  • 3) 10
  • 4) 11

20
Contrast
  • sizeof("Hello") ?
  • 6
  • strlen("Hello") ?
  • ???

21
Strings
  • char s110 "foo"
  • char s2 "superduper"
  • Legal?
  • s2 s1

Yes!
22
Strings
  • char s110 "foo"
  • char s2 "superduper"
  • Legal?
  • s1 s2

No!
23
Strings
  • char s110 "foo"
  • char s2 "superduper"
  • Does s2 s1 copy the strings?

No!
24
But it looks like it copies strings!
  • char s110 "foo"
  • char s2 "superduper"
  • s2 s1
  • printf("s\n", s1)
  • printf("s\n", s2)
  • What prints?

foo foo
25
Strings
\0
f
o
o
s1
\0
b
a
r
s2
s2 s1 What happens?
26
Strings
\0
f
o
o
s1
\0
b
a
r
s2
Memory Leak?
27
Strings
\0
f
o
o
s1
\0
b
a
r
s2
Depends!
28
Memory Leak?
  • char s1 "foo"
  • char s2 "bar"
  • s2 s1
  • No memory leak!
  • char s1 "foo"
  • char s2 malloc(4)
  • s2 s1
  • Memory Leak!

Paranoid delusional error checking omitted from
slide. Don't try this at home!
29
String Functions
  • include
  • strcpy(p, s)
  • strcmp(p, s)
  • strcat(p, s)
  • strlen(s)
  • Note strcpy and strcat dont malloc space
  • p must be allocated to be large enough to hold
    the results

30
String Functions
  • strcpy(s2, s1) returns what?
  • s2
  • Why?
  • Can be used like this
  • printf("s\n", strcpy(a, b))

31
String Functions
  • char s110 "foo"
  • char s2 "foobar"
  • strcpy(s2, s1) / Okay? /
  • No!
  • strcpy(s1, s2) / Okay? /
  • Yes!

32
String Functions
  • strdup - duplicate a string
  • include
  • char strdup(const char s)
  • What does it do? Different than strcpy? Why?
  • Cautions?
  • All of the same concerns that we had with malloc
    and free!

33
Danger!
  • gets
  • char gets(char s)
  • gets() reads a line from stdin into the buffer
    pointed to by s until either a terminating
    newline or EOF, which it replaces with '\0'. No
    check for buffer overrun is performed!
  • Because it is impossible to tell without knowing
    the data in advance how many characters gets()
    will read, and because gets() will continue to
    store characters past the end of the buffer, it
    is extremely dangerous to use. It has been used
    to break computer security. Use fgets() instead.

34
Example
  • include
  • include
  • include
  • int main(int argc, char argv)
  • char buffer10
  • gets(buffer)
  • printf("buffer s\n", buffer)
  • return EXIT_SUCCESS

35
Well behaved?
  • C\demo
  • hello
  • buffer hello

36
Well behaved?
  • C\demo
  • Now is the time for all good men to come to the
    aid of their
  • buffer Now is the time for all good men to come
    to the aid of their
  • In cygwin_except_handler
  • Exception trapped!
  • exception C0000005 at 20726F66
  • exception ax 0 bx 256F52C cx 46 dx BFFC9490
  • exception si 5 di 100113BF bp 20656D69 sp
    256F520
  • exception is STATUS_ACCESS_VIOLATION
  • Stack trace
  • yada, yada, yada

37
strtok
  • strtok - extract token from string
  • include
  • char strtok
  • (char s, const char delim)
  • A token' is a nonempty string of characters not
    occurring in the string delim, followed by \0 or
    by a character occurring in delim.
  • The strtok() function can be used to parse the
    string s into tokens. The first call to strtok()
    should have s as its first argument. Subsequent
    calls should have the first argument set to NULL.
    Each call returns a pointer to the next token, or
    NULL when no more tokens are found.

38
strtok
  • strtok - extract token from string
  • include
  • char strtok
  • (char s, const char delim)
  • If a token ends with a delimiter, this delimiting
    character is overwritten with a \0 and a pointer
    to the next character is saved for the next call
    to strtok. The delimiter string delim may be
    different for each call.
  • Never use this function. This function modifies
    its first argument. The identity of the
    delimiting character is lost. This function
    cannot be used on constant strings.
  • The strtok() function returns a pointer to the
    next token, or NULL if there are no more tokens.

39
Question
  • It's highly probable that strtok contains a
    variable of what storage class?
  • static
  • register
  • extern
  • manual
  • volatile

40
Questions?
41
Library Fun Facts
42
What are .a files?
  • They're libraries (collections of .o files,
    basically). Most libraries live in /usr/lib.
    When you specify "-lm" to cc/gcc at link time, it
    reads that as "use a library named 'm'" and then
    expands 'm' by wrapping 'lib' and '.a' around it
    to make 'libm.a' (which it finds in /usr/lib).
  • The library stuff is pretty natural for the
    installed libraries in /usr/lib. For
    special-purpose libraries, it's a little more
    cumbersome because you have to add two flags to
    the linking command
  • -L/the/directory/with/the/lib/files
  • and, for example
  • -lpgm
  • if you want to use libpgm.a in that directory.

43
Questions?
44
(No Transcript)
Write a Comment
User Comments (0)
About PowerShow.com