-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathtricky_questions.cpp
68 lines (54 loc) · 1.57 KB
/
tricky_questions.cpp
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
#include <iostream>
#include <string>
#include <vector>
void comparisonOfIntegerWithDifferentSignedness() {
int a = -1;
// if we define b unsigned we will get b<a
// unsigned int b=10;
std::size_t b = 10;
// change b to the signed one and the problem is gone
// int b=10;
std::cout << "a=" << a << " ,b=" << b << std::endl;
// in the comparision between singed and unsigned, the signed one will turn
// into unsigned!
if (a < b)
std::cout << "a<b" << std::endl;
else
std::cout << "b<a" << std::endl;
}
void roundingProblem() {
float a = 1.99999999999999999;
float b = 2;
// you expect that result is none zero so the if true while the if is false!
if (b - a)
std::cout << "**" << std::endl;
// std::cout<< b-a<<std::endl;
}
// TrackMemoryAllocations
void *operator new(size_t size) {
std::cout << "Allocating " << size << " bytes" << std::endl;
return malloc(size);
}
void operator delete(void *memory, size_t size) {
std::cout << "Freeing " << size << " bytes" << std::endl;
free(memory);
}
void operator delete(void *memory) {
std::cout << "Freeing " << std::endl;
free(memory);
}
void stlOnStack() {
std::string str;
std::cout << "allocating and freeing memory for strings" << std::endl;
for (std::size_t i = 0; i < 25; i++) {
str = str + std::to_string(i);
std::cout << "size of string is: " << str.size()
<< " bytes and string is: " << str << std::endl;
}
}
int main(int argc, char **argv) {
// comparisonOfIntegerWithDifferentSignedness();
// roundingProblem();
// stlOnStack();
return 0;
}