mirror of
https://github.com/libretro/cpp-cheat.git
synced 2024-11-26 18:20:34 +00:00
gdb call
This commit is contained in:
parent
f133b1f294
commit
0f3f4a88f3
44
cpp/map.cpp
44
cpp/map.cpp
@ -37,7 +37,7 @@ std::string map_to_str(std::map<K,V> map) {
|
||||
class RangeSwitch {
|
||||
public:
|
||||
int method(int x) {
|
||||
static std::map<int,std::function<void(int&)>> m = {
|
||||
static const std::map<int,std::function<void(int&)>> m{
|
||||
{0, [&](int &x){
|
||||
x = -1;
|
||||
}},
|
||||
@ -51,8 +51,9 @@ public:
|
||||
x = 5;
|
||||
}}
|
||||
};
|
||||
static const auto end = m.end();
|
||||
auto it = m.upper_bound(x);
|
||||
if (it == m.end()) {
|
||||
if (it == end) {
|
||||
x = 7;
|
||||
} else {
|
||||
it->second(x);
|
||||
@ -95,7 +96,12 @@ int main() {
|
||||
/*
|
||||
# operator[]
|
||||
|
||||
get value from a given key.
|
||||
Get value from a given key.
|
||||
|
||||
Create if not present, so avoid this if possible and prefer the more restrictive methods:
|
||||
|
||||
- use at() or find () for fetching and updating
|
||||
- emplace() for putting new values
|
||||
*/
|
||||
{
|
||||
std::map<int,std::string> m{
|
||||
@ -110,7 +116,7 @@ int main() {
|
||||
assert(m[1] == "one2");
|
||||
|
||||
// WARNING: if the key does not exist, it is inserted with a value with default constructor.
|
||||
// This can be avoided by using `find` instead of `[]`.
|
||||
// This can be avoided by using `find` or `at` instead of `[]`.
|
||||
// Inserts `(2,"")` because `""` is the value for the default String constructor.
|
||||
// http://stackoverflow.com/questions/10124679/what-happens-if-i-read-a-maps-value-where-the-key-does-not-exist
|
||||
{
|
||||
@ -179,6 +185,25 @@ int main() {
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
# at()
|
||||
|
||||
A convenient version of find() that returns the item directly.
|
||||
|
||||
Throws if not present, so better when the key is supposed to be there.
|
||||
|
||||
C++11.
|
||||
*/
|
||||
{
|
||||
std::map<int,std::string> m{
|
||||
{0, "zero"},
|
||||
{1, "one"},
|
||||
};
|
||||
// Returns a reference, so we can modify it.
|
||||
m.at(1) = "one2";
|
||||
assert(m.at(1) == "one2");
|
||||
}
|
||||
|
||||
/*
|
||||
# insert
|
||||
|
||||
@ -278,7 +303,7 @@ int main() {
|
||||
// # Range switch case.
|
||||
// http://stackoverflow.com/questions/9432226/how-do-i-select-a-range-of-values-in-a-switch-statement/42331563#42331563
|
||||
{
|
||||
auto result = std::map<int, int>{
|
||||
const std::map<int, int> result{
|
||||
{-1, -1},
|
||||
{ 0, 0},
|
||||
{ 1, 0},
|
||||
@ -290,7 +315,7 @@ int main() {
|
||||
{ 7, 7},
|
||||
};
|
||||
int x;
|
||||
std::map<int,std::function<void()>> m = {
|
||||
const std::map<int,std::function<void()>> m{
|
||||
{0, [&](){
|
||||
x = -1;
|
||||
}},
|
||||
@ -304,20 +329,21 @@ int main() {
|
||||
x = 5;
|
||||
}}
|
||||
};
|
||||
const auto end = m.end();
|
||||
for (auto i = -1; i < 8; ++i) {
|
||||
auto it = m.upper_bound(i);
|
||||
if (it == m.end()) {
|
||||
if (it == end) {
|
||||
x = 7;
|
||||
} else {
|
||||
it->second();
|
||||
}
|
||||
assert(x == result[i]);
|
||||
assert(x == result.at(i));
|
||||
}
|
||||
|
||||
RangeSwitch rangeSwitch;
|
||||
for (auto i = -1; i < 8; ++i) {
|
||||
x = rangeSwitch.method(i);
|
||||
assert(x == result[i]);
|
||||
assert(x == result.at(i));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -20,6 +20,7 @@ Tested on 7.7.1 unless mentioned otherwise.
|
||||
1. [Multifile](multifile/)
|
||||
1. [define](define.c)
|
||||
1. [stderr ignore](stderr_ignore.c)
|
||||
1. [call](call.c)
|
||||
1. C++ features
|
||||
1. [Containers](container.cpp)
|
||||
1. [Overload](overload.cpp)
|
||||
|
26
gdb/call.c
Normal file
26
gdb/call.c
Normal file
@ -0,0 +1,26 @@
|
||||
/*
|
||||
- https://stackoverflow.com/questions/1354731/how-to-evaluate-functions-in-gdb
|
||||
- https://jvns.ca/blog/2018/01/04/how-does-gdb-call-functions/
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
/* Memory side effects are not reverted. */
|
||||
int global = 0;
|
||||
|
||||
int f(int x) {
|
||||
printf("%d\n", x);
|
||||
global++;
|
||||
return x + 1;
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
/* Try:
|
||||
* (gdb) call f(1)
|
||||
* (gdb) p global
|
||||
* (gdb) call f(2)
|
||||
* (gdb) p global
|
||||
* */
|
||||
printf("%d\n", f(1));
|
||||
return 0;
|
||||
}
|
Loading…
Reference in New Issue
Block a user