CS 3411 Systems Programming

CS 3411 Systems Programming Department of Computer Science Michigan Technological University C vs. C++ (cont.) Examples of Pointer Use: Strings in...
Author: Derek Walker
3 downloads 2 Views 165KB Size
CS 3411 Systems Programming

Department of Computer Science Michigan Technological University

C vs. C++ (cont.)

Examples of Pointer Use: Strings in C

I

There is no string data type in C.

I

Instead, a string is assumed to be a sequence of char terminated by a zero byte.

I

A char * is generally used as a string; just a pointer to the first char in the zero-terminated sequence of chars.

I

Careful when declaring a string:

char ∗STR ; / ∗ Only memory a l l o c a t e d i s t o p o i n t e r v a r i a b l e ∗ / char s t r [ 2 0 ] ; / ∗ 20 b y t e s a l l o c a t e d t o h o l d c o n t e n t s o f s t r i n g ∗

String Codes Example I Some examples of string functions: # include < s t d i o . h> int mystrlen ( s ) char ∗s ; { char ∗p ; p = s; while ( ∗ p ) p ++; r e t u r n p−s ; } strcpyv1 ( t , s ) char ∗s , ∗ t ; { while ( ( ∗ t = ∗s ) ! = ’ \ 0 ’ ) { s ++; t ++; } }

String Codes Example II strcpyv2 ( t , s ) char ∗s , ∗ t ; { while ( ( ∗ t ++ = ∗s ++) ! = ’ \ 0 ’ ) ; } strcpyv3 ( t , s ) char ∗s , ∗ t ; { while ( ∗ t ++ = ∗s + + ) ; } main ( ) { char t e s t _ s t r [ ] = " H e l l o World ! " ; char c o p y _ t o _ s t r [ 2 0 ] ; p r i n t f ( " O r i g i n a l s t r i n g : %s \ n " , t e s t _ s t r ) ; p r i n t f ( " Length o f s t r i n g : %d \ n " , m y s t r l e n ( t e s t _ s t r ) ) ; strcpyv3 ( copy_to_str , t e s t _ s t r ) ; p r i n t f ( " Copied s t r i n g : %s \ n " , c o p y _ t o _ s t r ) ; p r i n t f ( " Length o f s t r i n g : %d \ n " , m y s t r l e n ( c o p y _ t o _ s t r ) ) ; }

Manual Pages I

Usually the most accurate source of information for the system you’re working on

I

Accessed by the ’man’ command from the terminal, followed by section number, followed by the item you want information on Sections vary from system to system. You can see this by using the command ’man man’. Commonly, the sections are:

I

1. 2. 3. 4. 5. 6. 7. 8. I

User commands System calls Library routines Devices File formats Games Misc. System Administration

The ’info’ command is another option for GNU Software

C Standard I/O

I I

Different from C++ Manual pages available for specific functions: I I I I I

man 3 stdio (An overview) man 3 printf (Formatted output) man 3 scanf (Formatted input) man 3 getc (Character-based input macros) man 3 putc (Character-based output macros)

I

Default I/O Streams: stdin, stdout, stderr

I

Anything you open with the fopen function is also a stream.

I

All streams are of the (FILE *) data type.

Output in C++

I

C++ iostream methods « and » automatically format. # include < iostream > u s i n g namespace s t d ; main ( ) { float x ; int y ; char ∗ s t r ; x = 3.1; y = −20; s t r = " Characters " ; c o u t r i g h t = r ; r e t u r n ( tmp ) ; } void i n o r d e r ( r ) s t r u c t node ∗ r ; { i f ( r ! = NILNODE ) { i n o r d e r ( r −> l e f t ) ; p r i n t f ( "%c " , r −>data ) ; i n o r d e r ( r −> r i g h t ) ; } }

A Brief Look at Program Execution

I

Text is executable code (also some strings! Usually write-protected)

I

Data is global data (both initialized and uninitialized)

I

Heap is area from which dynamic allocations are made (malloc!) Stack is where function activation records pushed/popped.

I

I

I

I

Pushed (created) on stack when function invoked, removed on return May contain: function parameters, function locals, return address, temporaries, saved state, control link, access link

Usual to preallocate a block of storage for initial heap/stack

Problems to Avoid

I

I

It is always important to keep system programs as bug-free as possible Errant programs running in privileged mode can: I I I I

Access/modify system configuration files Erase user data Halt the system And so on!

Buffer Overflow I

Writing beyond allocated array bounds i n t getUserData ( ) { char copy [ 6 0 ] ; ... / ∗ User can i n p u t s t r i n g o f ANY l e n g t h ∗ / gets ( buf ) ; ... / ∗ Copies u n t i l s t r i n g t e r m i n a t i o n i n b u f ∗ / s t r c p y ( copy , b u f ) ; } main ( ) { ... char i n p u t [ 5 0 ] ; char ∗ s t r P t r ; ... getUserData ( ) ; / ∗ No s t r i n g memory a l l o c a t i o n f o r s t r P t r ∗ / strcpy ( strPtr , input ) ; }

Memory Leak I

Losing access to allocated memory segment - We can’t reclaim it! i n t func ( ) { void ∗ p t r ; / ∗ When f u n c t i o n r e t u r n s , v a l u e o f p t r i n a c c e s s i b l e ∗ / p t r = malloc ( 1 0 0 ) ; } main ( ) { char ∗ b p t r ; f o r ( i =1; i v a l u e == 0 ) f r e e ( n ) ; return ( 0 ) ; } main ( ) { node ∗p , ∗ q ; p = m a l l o c ( s i z e o f ( node ) ) ; p−>v a l u e = 1 0 ; p r i n t f ( " Node p v a l u e " , p−>v a l u e ) ; func ( p ) ; / ∗ p has a l r e a d y been f r e e d ∗ / p r i n t f ( " A f t e r f u n c p v a l u e \ n " , p−>v a l u e ) ; / ∗ q was never i n i t i a l i z e d ∗ / p r i n t f ( " Node q v a l u e \ n " , q−>v a l u e ) ; }