Skip to content

Commit 0f3f4a8

Browse files
committed
gdb call
1 parent f133b1f commit 0f3f4a8

3 files changed

Lines changed: 62 additions & 9 deletions

File tree

‎cpp/map.cpp‎

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ std::string map_to_str(std::map<K,V> map) {
3737
class RangeSwitch {
3838
public:
3939
int method(int x) {
40-
static std::map<int,std::function<void(int&)>> m = {
40+
static const std::map<int,std::function<void(int&)>> m{
4141
{0, [&](int &x){
4242
x = -1;
4343
}},
@@ -51,8 +51,9 @@ class RangeSwitch {
5151
x = 5;
5252
}}
5353
};
54+
static const auto end = m.end();
5455
auto it = m.upper_bound(x);
55-
if (it == m.end()) {
56+
if (it == end) {
5657
x = 7;
5758
} else {
5859
it->second(x);
@@ -95,7 +96,12 @@ int main() {
9596
/*
9697
# operator[]
9798
98-
get value from a given key.
99+
Get value from a given key.
100+
101+
Create if not present, so avoid this if possible and prefer the more restrictive methods:
102+
103+
- use at() or find () for fetching and updating
104+
- emplace() for putting new values
99105
*/
100106
{
101107
std::map<int,std::string> m{
@@ -110,7 +116,7 @@ int main() {
110116
assert(m[1] == "one2");
111117

112118
// WARNING: if the key does not exist, it is inserted with a value with default constructor.
113-
// This can be avoided by using `find` instead of `[]`.
119+
// This can be avoided by using `find` or `at` instead of `[]`.
114120
// Inserts `(2,"")` because `""` is the value for the default String constructor.
115121
// http://stackoverflow.com/questions/10124679/what-happens-if-i-read-a-maps-value-where-the-key-does-not-exist
116122
{
@@ -179,6 +185,25 @@ int main() {
179185
}
180186
}
181187

188+
/*
189+
# at()
190+
191+
A convenient version of find() that returns the item directly.
192+
193+
Throws if not present, so better when the key is supposed to be there.
194+
195+
C++11.
196+
*/
197+
{
198+
std::map<int,std::string> m{
199+
{0, "zero"},
200+
{1, "one"},
201+
};
202+
// Returns a reference, so we can modify it.
203+
m.at(1) = "one2";
204+
assert(m.at(1) == "one2");
205+
}
206+
182207
/*
183208
# insert
184209
@@ -278,7 +303,7 @@ int main() {
278303
// # Range switch case.
279304
// http://stackoverflow.com/questions/9432226/how-do-i-select-a-range-of-values-in-a-switch-statement/42331563#42331563
280305
{
281-
auto result = std::map<int, int>{
306+
const std::map<int, int> result{
282307
{-1, -1},
283308
{ 0, 0},
284309
{ 1, 0},
@@ -290,7 +315,7 @@ int main() {
290315
{ 7, 7},
291316
};
292317
int x;
293-
std::map<int,std::function<void()>> m = {
318+
const std::map<int,std::function<void()>> m{
294319
{0, [&](){
295320
x = -1;
296321
}},
@@ -304,20 +329,21 @@ int main() {
304329
x = 5;
305330
}}
306331
};
332+
const auto end = m.end();
307333
for (auto i = -1; i < 8; ++i) {
308334
auto it = m.upper_bound(i);
309-
if (it == m.end()) {
335+
if (it == end) {
310336
x = 7;
311337
} else {
312338
it->second();
313339
}
314-
assert(x == result[i]);
340+
assert(x == result.at(i));
315341
}
316342

317343
RangeSwitch rangeSwitch;
318344
for (auto i = -1; i < 8; ++i) {
319345
x = rangeSwitch.method(i);
320-
assert(x == result[i]);
346+
assert(x == result.at(i));
321347
}
322348
}
323349
}

‎gdb/README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ Tested on 7.7.1 unless mentioned otherwise.
2020
1. [Multifile](multifile/)
2121
1. [define](define.c)
2222
1. [stderr ignore](stderr_ignore.c)
23+
1. [call](call.c)
2324
1. C++ features
2425
1. [Containers](container.cpp)
2526
1. [Overload](overload.cpp)

‎gdb/call.c‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
- https://stackoverflow.com/questions/1354731/how-to-evaluate-functions-in-gdb
3+
- https://jvns.ca/blog/2018/01/04/how-does-gdb-call-functions/
4+
*/
5+
6+
#include <stdio.h>
7+
8+
/* Memory side effects are not reverted. */
9+
int global = 0;
10+
11+
int f(int x) {
12+
printf("%d\n", x);
13+
global++;
14+
return x + 1;
15+
}
16+
17+
int main(void) {
18+
/* Try:
19+
* (gdb) call f(1)
20+
* (gdb) p global
21+
* (gdb) call f(2)
22+
* (gdb) p global
23+
* */
24+
printf("%d\n", f(1));
25+
return 0;
26+
}

0 commit comments

Comments
 (0)