-
Notifications
You must be signed in to change notification settings - Fork 1
/
quiz03.tex
819 lines (636 loc) · 15.4 KB
/
quiz03.tex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
\documentclass[landscape]{slides}
\usepackage[margin=.5in]{geometry}
\usepackage{tabularx}
\usepackage[T1]{fontenc}
\usepackage[latin9]{inputenc}
\usepackage{listings}
\usepackage{color}
\usepackage{graphics}
\usepackage[english]{babel}
\definecolor{hellgelb}{rgb}{1,1,0.8}
\lstset{basicstyle={\tiny\ttfamily},backgroundcolor=\color{hellgelb},frame=single,numbers=left,numberstyle={\tiny\sffamily}}
\newcommand{\st}{Slide Title}
\onlyslides{0-999}
\begin{document}
%%%%%
\renewcommand{\st}{§7.1.2 Static Variables, [p145]}
\begin{slide}
\begin{lstlisting}
#include <iostream>
static int a = 0;
int b = 0;
void f(int n) {
static int c = 0;
while ( n-- ) {
static int d = 0;
int e = 0;
std::cout << a++ << b++ << c++ << d++ << e++ << std::endl;
}
}
int main() {
f(3);
f(3);
}
\end{lstlisting}
What might happen if you try to compile, link and run this program?
\begin{note}
\st
\begin{tiny}
\begin{verbatim}
g++ -Wall scratch.cpp && ./a.out
00000
11110
22220
33330
44440
55550
\end{verbatim}
What might happen if you do not init the variables? All static
variables, a,b,c,d will be set to 0, the initial value of e is
unknown, but e will probably increase for each invokation. Eg you
might get something like:
\begin{verbatim}
g++ -O1 -Wall scratch.cpp && ./a.out
scratch.cpp: In function 'void f(int)':
scratch.cpp:10: warning: 'e' is used uninitialized in this function
00002
11113
22224
33332
44443
55554
\end{verbatim}
When is a local variable initialized? (p145, A local variable is
initialized when the thread of execution reaches its definition.)
When is a static variable initialized? (p145, A static variable will
be initialized only the first time the thread of execution reaches its
definition.)
What is the difference between line 3 and 4? The static on line 3 does
not mean the same as the other static, it is a linkage directive. In
modern C++ you should not use static like this, consider an unnamed
namespace instead (p200).
\end{tiny}
\end{note}
\end{slide}
%%%%%
\renewcommand\st{§7.2, argument passing}
\begin{slide}
\begin{lstlisting}
#include <iostream>
void f( int a, int & b, int * c ) {
++a;
++b;
++c;
}
int main() {
int a = 3;
int b = 4;
int c = 5;
std::cout << a << ',' << b << ',' << c << std::endl;
f(a,b,c);
std::cout << a << ',' << b << ',' << c << std::endl;
}
\end{lstlisting}
What might happen if you try to compile, link and run this program?
\begin{note}
\st
\begin{tiny}
\begin{verbatim}
g++ -Wall scratch.cpp && ./a.out
scratch.cpp: In function 'int main()':
scratch.cpp:14: error: invalid conversion from 'int' to 'int*'
scratch.cpp:14: error: initializing argument 3 of 'void f(int, int&, int*)'
\end{verbatim}
Suppose you fix line 14 with
\begin{verbatim}
foo(a,b,&c);
\end{verbatim}
then the following is printed:
\begin{verbatim}
g++ -Wall scratch.cpp && ./a.out
3,4,5
3,5,5
\end{verbatim}
\end{tiny}
\end{note}
\end{slide}
%%%%%
\renewcommand\st{§7.2,§72, argument passing and value return}
\begin{slide}
\begin{lstlisting}
#include <iostream>
void p(int i) { std::cout << i; }
void p(char ch) { std::cout << ch; }
class A {
public:
A() { p(1); }
A(const A & a) { p(2); }
void operator=(const A & a) { p(3); }
A(int i) { p(4); }
~A() { p(5); }
};
void f(A & a) { p('f'); }
void g(const A & a) { p('g'); }
A h() { p('h'); return 1; }
const A & i() { p('i'); return 1; }
int main() {
A a;
p('-'); f(a);
p('-'); g(a);
p('-'); g(1);
p('-'); a = h();
p('-'); a = i();
p('-');
}
\end{lstlisting}
What might happen if you try to compile, link and run this program?
\begin{note}
\st
\begin{tiny}
\begin{verbatim}
g++ -Wall scratch.cpp && ./a.out
scratch.cpp: In function 'const A& i()':
scratch.cpp:18: warning: returning reference to temporary
1-f-g-4g5-h435-i453-5
\end{verbatim}
What do you thing about line 10? How to fix it? (return reference to this)
Why can't overloading distinguish on return values? Two reasons:
- ambiguities for invocations that does not assign to something
- destroys arithmetic properties (??? find ref)
\end{tiny}
\end{note}
\end{slide}
%%%%%
\renewcommand\st{§7.4, overloaded function names}
\begin{slide}
\begin{lstlisting}
#include <iostream>
#include <string>
void p(double) { std::cout << "p(double)" << std::endl; }
void p(float) { std::cout << "p(float)" << std::endl; }
void p(std::string) { std::cout << "p(std::string)" << std::endl; }
void p(const char *) { std::cout << "p(const char *)" << std::endl; }
void p(char *) { std::cout << "p(char *)" << std::endl; }
void p(long) { std::cout << "p(long)" << std::endl; }
void p(char) { std::cout << "p(char)" << std::endl; }
int main() {
p(1.9);
p('s');
p("hello");
p(1);
}
\end{lstlisting}
What might happen if you try to compile, link and run this program?
\begin{note}
\st
\begin{tiny}
\begin{verbatim}
g++ -Wall scratch.cpp && ./a.out
scratch.cpp: In function 'int main()':
scratch.cpp:16: error: call of overloaded 'p(int)' is ambiguous
scratch.cpp:4: note: candidates are: void p(double)
scratch.cpp:5: note: void p(float)
scratch.cpp:6: note: void p(std::string) <near match>
scratch.cpp:7: note: void p(const char*) <near match>
scratch.cpp:8: note: void p(char*) <near match>
scratch.cpp:9: note: void p(long int)
scratch.cpp:10: note: void p(char)
\end{verbatim}
How can you fix this by adding just one character? Eg, by changing line 16 to 'p(1L)'
Then it will print:
\begin{verbatim}
g++ -Wall scratch.cpp && ./a.out
p(double)
p(char)
p(const char *)
p(long)
\end{verbatim}
Keeping our new line 16. What will happen if you comment out line 7?
\begin{verbatim}
g++ -Wall scratch.cpp && ./a.out
p(double)
p(char)
p(char *)
p(long)
\end{verbatim}
\end{tiny}
A literal string in C++ is "magic", it is really a "const char *" but it will
be silently converted to a "char *" if needed. This is due to C compatibility.
Apparently this is about to change with the next version of C++. (??? is this true?)
\end{note}
\end{slide}
%%%%%
\renewcommand\st{§7.8, badly used macros}
\begin{slide}
\begin{lstlisting}
#include <iostream>
#define MIN(a,b) (((a)<(b))?(a):(b))
int main() {
int a=2;
int b=3;
int c=0;
std::cout << c << a << b << std::endl;
c = MIN(++a,++b);
std::cout << c << a << b << std::endl;
c = MIN(++a,++b);
std::cout << c << a << b << std::endl;
}
\end{lstlisting}
What might happen if you try to compile, link and run this program?
\begin{note}
\st
\begin{tiny}
\begin{verbatim}
g++ scratch.cpp && ./a.out
023
444
656
\end{verbatim}
\end{tiny}
\end{note}
\end{slide}
%%%%%
\renewcommand\st{§7.8, badly written macros}
\begin{slide}
\begin{lstlisting}
#include <iostream>
#define MAX(a,b) a > b ? a : b
int main() {
int a=2;
int b=3;
int c=0;
std::cout << c << a << b << std::endl;
c = 42 + MAX(a,b);
std::cout << c << a << b << std::endl;
}
\end{lstlisting}
What might happen if you try to compile, link and run this program?
\begin{note}
\st
\begin{tiny}
\begin{verbatim}
g++ -Wall scratch.cpp && ./a.out
023
223
\end{verbatim}
\end{tiny}
\end{note}
\end{slide}
%%%%%
\renewcommand\st{§8.2.5.1, Unnamed Namespaces}
\begin{slide}
\begin{lstlisting}
#include <iostream>
int a = 4;
namespace {
int b = 5;
};
int main() {
std::cout << a << std::endl;
std::cout << b << std::endl;
}
\end{lstlisting}
What might happen if you try to compile, link and run this program?
\begin{note}
\st
\begin{tiny}
\begin{verbatim}
g++ -Wall scratch.cpp && ./a.out
4
5
\end{verbatim}
\end{tiny}
\end{note}
\end{slide}
%%
\renewcommand\st{§8.2, Namespaces and scope resolution}
\begin{slide}
\begin{lstlisting}
#include <iostream>
template<typename T> void p(T x) { std::cout << x; }
void f() { p(1); }
namespace A {
void f() { p(2); }
}
void g() { p(3); }
namespace {
void g() { p(4); }
};
int main() {
f();
g();
}
\end{lstlisting}
What might happen if you try to compile, link and run this program?
\begin{note}
\st
\begin{tiny}
\begin{verbatim}
g++ -Wall scratch.cpp && ./a.out
scratch.cpp: In function 'int main()':
scratch.cpp:20: error: call of overloaded 'g()' is ambiguous
scratch.cpp:11: note: candidates are: void g()
scratch.cpp:14: note: void<unnamed>::g()
\end{verbatim}
How can you make this code print 13? eg, do ::g() on line 19, or comment out line 14
How can you make this code print 14? eg, comment out line 11 (is there a way to do scope resolution into an unnamed namespace?)
How can you make this code print 23? Use 'A::f()' on line 18, and '::g()' on line 19'
\end{tiny}
\end{note}
\end{slide}
%%%%%
\renewcommand\st{§8.2, Namespaces, defining declared members}
\begin{slide}
\tiny{foo.hpp}
\begin{lstlisting}
namespace Foo {
void f();
}
\end{lstlisting}
\tiny{foo.cpp}
\begin{lstlisting}
#include <iostream>
#include "foo.hpp"
namespace Foo {
void f() { std::cout << "f()" << std::endl; }
}
namespace {
void g() { Foo::f(); }
}
int main() {
g();
}
\end{lstlisting}
What might happen if you try to compile, link and run this program?
Please criticize.
\begin{note}
\st
\begin{tiny}
It will compile, link, run and print out f().
However, there are at least three tings to criticized:
- [p185] When defining a previously declared member of a namespace, it is
safer to define it outside, rather than reopen, the namespace.
- it is recomended to including your own headers first, then stable libraries
- include guards are missing on foo.hpp [9.3.3,p216]
\end{tiny}
\end{note}
\end{slide}
%%%%%
\renewcommand\st{§8.3, Exceptions}
\begin{slide}
\begin{lstlisting}
#include <iostream>
template<typename T> void p(T x) { std::cout << x; }
struct A {};
struct B : A {};
struct C : B {};
struct D : C {};
int main() {
try {
p(1);
throw B();
p(2);
} catch(A a) {
p(3);
throw C();
p(4);
} catch(B & b) {
p(5);
throw D();
p(6);
} catch(const C & c) {
p(7);
throw;
p(8);
} catch(...) {
p(9);
}
p(0);
}
\end{lstlisting}
What might happen if you try to compile, link and run this program?
Criticize the code.
\begin{note}
\st
\begin{tiny}
\begin{verbatim}
g++ scratch.cpp && ./a.out
scratch.cpp: In function 'int main()':
scratch.cpp:19: warning: exception of type 'B' will be caught
scratch.cpp:15: warning: by earlier handler for 'A'
scratch.cpp:23: warning: exception of type 'C' will be caught
scratch.cpp:19: warning: by earlier handler for 'B'
terminate called after throwing an instance of 'C'
13
\end{verbatim}
Exceptions should be caught by copy as on line 15. Although it will often work
it is recommended to catch by reference like on line 19 for several
reasons (???find ref)
\end{tiny}
\end{note}
\end{slide}
%%%%%
\renewcommand\st{§9.2, Internal and External Linkage}
\begin{slide}
\begin{tabular*}{\textwidth}{@{\extracolsep{\fill}}lr}
\begin{minipage}[t]{.6\linewidth}
\tiny{foo.hpp}
\begin{lstlisting}
int a;
const int b = 42;
int foo();
\end{lstlisting}
\end{minipage}
&
\begin{minipage}[t]{.3\linewidth}
\tiny{foo.cpp}
\begin{lstlisting}
#include "foo.hpp"
int a = 42;
int foo() {
return a;
}
\end{lstlisting}
\end{minipage}
\end{tabular*}
\tiny{bar.cpp}
\begin{lstlisting}
#include "foo.hpp"
#include <iostream>
int main() {
std::cout << foo() << " is " << b << std::endl;
}
\end{lstlisting}
What might happen if you try to compile, link and run this program using this
command line:
\begin{lstlisting}[numbers=none]
g++ -Wall foo.cpp bar.cpp && ./a.out
\end{lstlisting}
\begin{note}
\st
\begin{tiny}
\begin{verbatim}
g++ -Wall foo.cpp bar.cpp && ./a.out
foo.cpp:3: error: redefinition of 'int a'
foo.hpp:1: error: 'int a' previously declared here
\end{verbatim}
What happens if you comment out line 1 in foo.cpp? Link error
\begin{verbatim}
g++ -Wall foo.cpp bar.cpp && ./a.out
/usr/bin/ld: multiple definitions of symbol _a
/var/tmp//ccDvqXPk.o definition of _a in section (__DATA,__data)
/var/tmp//ccFahyOW.o definition of _a in section (__DATA,__common)
collect2: ld returned 1 exit status
\end{verbatim}
What happens if you comment out line 3 in foo.cpp? Still link error
Why do you get linkage error? Because "int a;" is a definition
Why doesn't the linker complain about line:2 in foo.hpp? b is also a definition, but it has internal linkage
How can you fix this problem? Use 'extern int a;' to make it a declaration, and then you get "42 is 42"
\end{tiny}
\end{note}
\end{slide}
%%%%%
\renewcommand\st{§9.2.3, The One-Definition Rule}
\begin{slide}
\begin{tabular*}{\textwidth}{@{\extracolsep{\fill}}lr}
\begin{minipage}[t]{.6\linewidth}
\tiny{foo.hpp}
\begin{lstlisting}
struct S {
char a;
int b;
};
S foo();
\end{lstlisting}
\end{minipage}
&
\begin{minipage}[t]{.3\linewidth}
\tiny{foo.cpp}
\begin{lstlisting}
struct S {
int a;
char b;
};
S foo() {
S s;
s.a = 64;
s.b = 'A';
return s;
}
\end{lstlisting}
\end{minipage}
\end{tabular*}
\tiny{bar.cpp}
\begin{lstlisting}
#include <iostream>
#include "foo.hpp"
S foo();
int main() {
S s = foo();
std::cout << s.a << s.b << std::endl;
}
\end{lstlisting}
What might happen if you try to compile, link and run this program using this
command line:
\begin{lstlisting}[numbers=none]
g++ -Wall foo.cpp bar.cpp && ./a.out
\end{lstlisting}
\begin{note}
\st
\begin{tiny}
\begin{verbatim}
g++ -Wall foo.cpp bar.cpp && ./a.out
@65
\end{verbatim}
What will happen if you switch linke 2 and 3 in foo.cpp?
\begin{verbatim}
g++ -Wall foo.cpp bar.cpp && ./a.out
A64
\end{verbatim}
What if you include "foo.hpp" in foo.cpp?
\begin{verbatim}
g++ -Wall foo.cpp bar.cpp && ./a.out
foo.cpp:3: error: redefinition of 'struct S'
foo.hpp:1: error: previous definition of 'struct S'
\end{verbatim}
What if you include "foo.hpp" in foo.cpp, and compile only bar.cpp?
\begin{verbatim}
g++ -Wall bar.cpp && ./a.out
64A
\end{verbatim}
\end{tiny}
\end{note}
\end{slide}
%%%%%
\renewcommand\st{§9.4, Initialization and termination}
\begin{slide}
\begin{lstlisting}
#include <iostream>
#include <ctype.h>
struct X {
char id;
X(char ch) : id(ch) { std::cout << (char)toupper(id); }
~X() { std::cout << id; }
};
struct A : X { A() : X('a') {} };
struct B : X { B() : X('b') {} };
struct C : X { C() : X('c') {} };
struct D : X { D() : X('d') {} };
struct E : X { E() : X('e') {} };
struct F : X { F() : X('f') {} };
A a;
static B b;
int main() {
C c;
static D d;
{
static E e;
F f;
}
return 0;
}
\end{lstlisting}
What might happen if you try to compile, link and run this program?
\begin{note}
\st
\begin{tiny}
\begin{verbatim}
g++ -Wall scratch.cpp && ./a.out
ABCDEFfcedba
What if you replace line 27 with 'exit(0);'?
ABCDEFfedba
Notice that C is not destroyed. (see p218)
What if you replace line 27 with 'abort();'?
g++ -Wall scratch.cpp && ./a.out
ABCDEFf
Critiques:
- in modern C++ you should use #include <cctype> on line 2
- in modern C++ you should use static_case<char> to do casting on line 6
- what does static on line 18 mean? local linkage, in modern C++ use
unnamed namespace instead
\end{verbatim}
\end{tiny}
\end{note}
\end{slide}
%% template
%% \renewcommand\st{§x.x, title}
%% \begin{slide}
%%
%% \begin{lstlisting}
%%
%% \end{lstlisting}
%%
%% what might happen if you try to compile, link and run this program?
%%
%% \begin{note}
%% \st
%% \begin{tiny}
%% \begin{verbatim}
%%
%% \end{verbatim}
%%
%% \end{tiny}
%% \end{note}
%%
%% \end{slide}
\end{document}