Pointers Dynamic Memory Allocation Structs—n-Tuples Arrays Bubble Sort Strings 6. Pointers, Structs, and Arrays March 14 & 15, 2011 6. Pointers, Structs, and Arrays Einf ¨ uhrung in die Programmierung—Introduction to C/C++, Tobias Weinzierl page 1 of 47
52
Embed
6. Pointers, Structs, and Arrays...double b; 6. Pointers, Structs, and Arrays Einfuhrung¨ in die Programmierung—Introduction to C/C++, Tobias Weinzierl page 4 of 47 PointersDynamic
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
vo id foo ( i n t ∗ a ) {(∗a )++;. . .a++; / / That could not have happened wi th re ferences
}
• Pointers are difficult to handle (lots of syntactic overhead).• We know how to do it, but do collaborators know?• Avoid it whenever possible. Use C++ references instead.
6. Pointers, Structs, and Arrays
Einfuhrung in die Programmierung—Introduction to C/C++, Tobias Weinzierl page 10 of 47
The pointer operator binds to the right neighbour.
i n t a ;i n t ∗ b1 ;i n t ∗ b2 ;i n t ∗b3 ;i n t ∗c1 , c2 ;i n t ∗c3 , ∗c4 ;i n t ∗ c5 , c6 ;i n t ∗ c7 , ∗c8 ;i n t ∗∗ c9 ;
c7 = c8 ;c7 = ∗c8 ;∗c7 = c8 ;∗c7 = ∗c8 ;
By the way, often people write int* p=0; to make the pointer point to nothing.However, also int* p = 20; would be fine (and for almost 100 percent is a bug).
6. Pointers, Structs, and Arrays
Einfuhrung in die Programmierung—Introduction to C/C++, Tobias Weinzierl page 11 of 47
}s td : : cout << a << s td : : endl ; / / does t h i s work?s td : : cout << ∗p << s td : : endl ; / / does t h i s work?
With pointers, we can violate the end-of-scope rules, i.e. we can access variables thatdo not exist anymore. This is the principle of all these buffer overrun malware.
6. Pointers, Structs, and Arrays
Einfuhrung in die Programmierung—Introduction to C/C++, Tobias Weinzierl page 12 of 47
}s td : : cout << a << s td : : endl ; / / does t h i s work?s td : : cout << ∗p << s td : : endl ; / / does t h i s work?
With pointers, we can violate the end-of-scope rules, i.e. we can access variables thatdo not exist anymore. This is the principle of all these buffer overrun malware.
6. Pointers, Structs, and Arrays
Einfuhrung in die Programmierung—Introduction to C/C++, Tobias Weinzierl page 12 of 47
f o r ( i n t i =0; i <20; i ++) {p = new double ;
}
de le te p ;
f o r ( i n t i =0; i <20; i ++) {double ∗p = new double ;
}
The upper operation creates 20 doubles on the heap, but it destroys only one double inthe end. Consequently, the remaining 19 doubles are lost for the future programexecution. Let’s sketch the memory layout! The second one destroys the pointer but notthe space where the pointer is pointing to. This is why many applications crash afterseveral hours of execution.
6. Pointers, Structs, and Arrays
Einfuhrung in die Programmierung—Introduction to C/C++, Tobias Weinzierl page 14 of 47
f o r ( i n t i =0; i <20; i ++) {p = new double ;
}
de le te p ;
f o r ( i n t i =0; i <20; i ++) {double ∗p = new double ;
}
The upper operation creates 20 doubles on the heap, but it destroys only one double inthe end. Consequently, the remaining 19 doubles are lost for the future programexecution. Let’s sketch the memory layout! The second one destroys the pointer but notthe space where the pointer is pointing to. This is why many applications crash afterseveral hours of execution.
6. Pointers, Structs, and Arrays
Einfuhrung in die Programmierung—Introduction to C/C++, Tobias Weinzierl page 14 of 47
s t r u c t I n tege rEn t r y {i n t value ;I n tege rEn t r y∗ next ;
} ;
Take a sheet of paper and sketch the memory layout for an instance of IntegerEntryreferencing another instance of IntegerEntry. What could be the reasoning behind sucha data structure?
6. Pointers, Structs, and Arrays
Einfuhrung in die Programmierung—Introduction to C/C++, Tobias Weinzierl page 19 of 47
/ / T e l l s compi ler t h a t something l i k e a StudentID does e x i s ts t r u c t I n tege rEn t r y {
i n t value ;I n tege rEn t r y∗ next ;
} ;
vo id p r i n t L i s t ( I n tege rEn t r y∗ f i r s t E n t r y ) {whi le ( f i r s t E n t r y !=0 ) {
s td : : cout << f i r s t E n t r y−>value << ” ” ;f i r s t E n t r y = f i r s t E n t r y−>next ;
}}
vo id append ( In tege rEn t r y∗ f i r s t E n t r y , i n t value ) {whi le ( f i r s t E n t r y−>next !=0 ) {
f i r s t E n t r y = f i r s t E n t r y−>next ;}I n tege rEn t r y∗ newEntry = new In tege rEn t r y ;f i r s t E n t r y−>next = newEntry ;newEntry−>next = 0 ;newEntry−>value = value ;
}
Can you rewrite append() recursively?
6. Pointers, Structs, and Arrays
Einfuhrung in die Programmierung—Introduction to C/C++, Tobias Weinzierl page 22 of 47
An Excursus on Lists and Complexity—the big-O notation
f ∈ O(nk ) Doesn’t grow faster than nk .
• Faster or slower refers to the leading term of the runtime polynomial.• Constants or polynomials of lower order are not considered.• Often, one writes = or says is of instead of is contained in.• What does this mean for our list? What is the n there?
• Study the list in terms of n list entries.• If we remove the first element, this is a fixed number of operations (O(1)).• If we append an element, our while loop has to run over all n element before we
can append an element (O(n)).• If we search for an element, we have to study each element exactly once (O(n)).
6. Pointers, Structs, and Arrays
Einfuhrung in die Programmierung—Introduction to C/C++, Tobias Weinzierl page 24 of 47
An Excursus on Lists and Complexity—the big-O notation
f ∈ O(nk ) Doesn’t grow faster than nk .
• Faster or slower refers to the leading term of the runtime polynomial.• Constants or polynomials of lower order are not considered.• Often, one writes = or says is of instead of is contained in.• What does this mean for our list? What is the n there?
• Study the list in terms of n list entries.• If we remove the first element, this is a fixed number of operations (O(1)).• If we append an element, our while loop has to run over all n element before we
can append an element (O(n)).• If we search for an element, we have to study each element exactly once (O(n)).
6. Pointers, Structs, and Arrays
Einfuhrung in die Programmierung—Introduction to C/C++, Tobias Weinzierl page 24 of 47
An Excursus on Lists and Complexity—the big-O notation
f ∈ O(nk ) Doesn’t grow faster than nk .
• Faster or slower refers to the leading term of the runtime polynomial.• Constants or polynomials of lower order are not considered.• Often, one writes = or says is of instead of is contained in.• What does this mean for our list? What is the n there?
• Study the list in terms of n list entries.• If we remove the first element, this is a fixed number of operations (O(1)).• If we append an element, our while loop has to run over all n element before we
can append an element (O(n)).• If we search for an element, we have to study each element exactly once (O(n)).• What could we do to make the append operation run faster?
6. Pointers, Structs, and Arrays
Einfuhrung in die Programmierung—Introduction to C/C++, Tobias Weinzierl page 25 of 47
An Excursus on Lists and Complexity—the big-O notation
f ∈ O(nk ) Doesn’t grow faster than nk .
• Faster or slower refers to the leading term of the runtime polynomial.• Constants or polynomials of lower order are not considered.• Often, one writes = or says is of instead of is contained in.• What does this mean for our list? What is the n there?
• Study the list in terms of n list entries.• If we remove the first element, this is a fixed number of operations (O(1)).• If we append an element, our while loop has to run over all n element before we
can append an element (O(n)).• If we search for an element, we have to study each element exactly once (O(n)).• What could we do to make the append operation run faster?
6. Pointers, Structs, and Arrays
Einfuhrung in die Programmierung—Introduction to C/C++, Tobias Weinzierl page 25 of 47
—6.4. Arrays—Application example: At the university, we wanna keep track of four grades a studentdid throughout his Master’s studies.
double ana lys is1 ;double ana lys is2 ;double l i nea rA lgeb ra ;double s tochas t i c s ;
. . .
/∗∗∗ Takes grades , computes the average , and re tu rns t h i s value .∗ /
double computeAverage ( const double& g1 , const double& g2 , . . . ) {double r e s u l t = g1+g2+g3+g4 ;r e s u l t /= 4 . 0 ;r e t u r n r e s u l t ;
}
Is this impractical, if we wanna store more than four values.
6. Pointers, Structs, and Arrays
Einfuhrung in die Programmierung—Introduction to C/C++, Tobias Weinzierl page 26 of 47
double∗ p = grade ;i f ( grade [0]==∗p ) . . . / / always t ruei f ( grade [1 ]==∗ ( p +1) ) . . . / / always t rue
• An array variable is basically a pointer to the first element of the array.• An array element access internally implies pointer arithmetics and dereferencing.• Again, we thus have to range checks (size of array) at hand at all.
6. Pointers, Structs, and Arrays
Einfuhrung in die Programmierung—Introduction to C/C++, Tobias Weinzierl page 29 of 47
gradesMSc [ 5 ] = 2 . 3 ;gradesMSc [ 3 ] + + ;i n t ∗ p0 = gradesMSc ;i n t ∗ p1 = &gradesMSc [ 0 ] ;i n t ∗ p2 = &gradesMSc [ 1 ] ;gradesMSc++; / / t h a t does not workp2++; / / a r r rgh
6. Pointers, Structs, and Arrays
Einfuhrung in die Programmierung—Introduction to C/C++, Tobias Weinzierl page 32 of 47
double∗ createThreeRandomGrades ( ) {double r e s u l t [ 3 ] ;r e s u l t [ 0 ] = 1 . 3 ; r e s u l t [ 1 ] = 2 . 7 ; r e s u l t [ 2 ] = 1 . 0 ;r e t u r n r e s u l t ;
}
double∗ createThreeRandomGrades ( ) {double∗ r e s u l t = new double [ 3 ] ;r e s u l t [ 0 ] = 1 . 3 ; r e s u l t [ 1 ] = 2 . 7 ; r e s u l t [ 2 ] = 1 . 0 ;r e t u r n r e s u l t ;
}
• At the end of the scope, the pointer is destroyed always.
• Arrays on the heap are not destroyed.
• This is called a factory mechanism.
• Someone else invoking us has to delete the array.
6. Pointers, Structs, and Arrays
Einfuhrung in die Programmierung—Introduction to C/C++, Tobias Weinzierl page 34 of 47
double∗ createThreeRandomGrades ( ) {double r e s u l t [ 3 ] ;r e s u l t [ 0 ] = 1 . 3 ; r e s u l t [ 1 ] = 2 . 7 ; r e s u l t [ 2 ] = 1 . 0 ;r e t u r n r e s u l t ;
}
double∗ createThreeRandomGrades ( ) {double∗ r e s u l t = new double [ 3 ] ;r e s u l t [ 0 ] = 1 . 3 ; r e s u l t [ 1 ] = 2 . 7 ; r e s u l t [ 2 ] = 1 . 0 ;r e t u r n r e s u l t ;
}
• At the end of the scope, the pointer is destroyed always.
• Arrays on the heap are not destroyed.
• This is called a factory mechanism.
• Someone else invoking us has to delete the array.
6. Pointers, Structs, and Arrays
Einfuhrung in die Programmierung—Introduction to C/C++, Tobias Weinzierl page 35 of 47
• Idea of bubble sort: Run over all elements in the list.• Compare two subsequent elements whether they are in the correct order. Swap
them if necessary.• If a swap still had been necessary, run over list again.• How “expensive” is th sorting?
• The number of comparisons is a good metric. So, let n be the number of elementsin our list.
• At most (worst case), we’ll need n runs over the list.• In the average case, we’ll need n/2 runs over the list.• In each run, we have to do n − 1 comparisons.• Overall, the complexity is O(n2).• More intelligent (but more complex) sorting algorithms need only O(n log n)
comparisons.
6. Pointers, Structs, and Arrays
Einfuhrung in die Programmierung—Introduction to C/C++, Tobias Weinzierl page 40 of 47
• Idea of bubble sort: Run over all elements in the list.• Compare two subsequent elements whether they are in the correct order. Swap
them if necessary.• If a swap still had been necessary, run over list again.• How “expensive” is th sorting?
• The number of comparisons is a good metric. So, let n be the number of elementsin our list.
• At most (worst case), we’ll need n runs over the list.• In the average case, we’ll need n/2 runs over the list.• In each run, we have to do n − 1 comparisons.• Overall, the complexity is O(n2).• More intelligent (but more complex) sorting algorithms need only O(n log n)
comparisons.
6. Pointers, Structs, and Arrays
Einfuhrung in die Programmierung—Introduction to C/C++, Tobias Weinzierl page 40 of 47
• C has no string concept.• C++ has a nice string concept.• C has chars.• And C has arrays.• Thus, C has arrays of chars.• And C has a mapping of numbers to chars.
6. Pointers, Structs, and Arrays
Einfuhrung in die Programmierung—Introduction to C/C++, Tobias Weinzierl page 41 of 47
i n t leng th ( const char [ ] s ) {i n t leng th = 0;wh i le ( s [ leng th ]==0) {
l eng th ++;}r e t u r n leng th ;
}
char∗ myStr ing = ” Ha l lo ” ;i n t lengthOfMyStr ing = leng th ( myStr ing ) ;
• What is copied here (call-by-value)!• What happens, if we write
char∗ ano therS t r ing = myStr ing ;
• How can we concatenate strings or add additional characters?• How can we copy strings?• How long is the string abc”, ”defg”, and the concatenation of both of them in terms
of a char array.
6. Pointers, Structs, and Arrays
Einfuhrung in die Programmierung—Introduction to C/C++, Tobias Weinzierl page 47 of 47