From: Li Guilin via Gdb <gdb@sourceware.org>
To: gdb@sourceware.org
Subject: Question about printing cpp std::map element using "print map1[0]"
Date: Thu, 6 Mar 2025 19:24:41 +0800 [thread overview]
Message-ID: <tencent_14EE7DDE78E98F2D81E8E4F63B574645A406@qq.com> (raw)
Hello,
I want to print individual std::map element using operator[] instead of
printing the whole map during debugging in gdb, but it is not working.
In contrast, calling operator[] on std::vector and std::deque variables
works fine.
```
(gdb) p map1
$1 = std::map with 1 element = {[0] = 2}
(gdb) p map1[0]
Attempt to take address of value not located in memory.
```
Besides, disabling xmethod and pretty printer does not affet the overall
behavior.
And through "info functions" output, I think gdb knows information about
std::map::operator[], like this (shortened):
```
(gdb) info functions std::map.*operator\[\]
All functions matching regular expression "std::map.*operator\[\]":
File /usr/include/c++/14/bits/stl_map.h:
504: std::map<int, int, ...>::mapped_type &std::map<int, int,
...>::operator[](int const&);
524: std::map<int, int, ...>::mapped_type &std::map<int, int,
...>::operator[](int&&);
```
Below is the sample cpp code.
```
#include <deque>
#include <iostream>
#include <map>
#include <vector>
// instantiate all member functions.
template class std::deque<int>;
template class std::map<int, int>;
template class std::vector<int>;
int main() {
std::deque<int> deque1;
std::map<int, int> map1;
std::vector<int> vector1;
deque1.push_back(1);
std::cout << "deque1[0] = " << deque1[0] << "\n";
map1[0] = 2;
std::cout << "map1[0] = " << map1[0] << "\n";
vector1.push_back(1);
std::cout << "vector1[0] = " << vector1[0] << "\n";
}
```
How to make "print map1[0]" work? Where is the problem?
Thanks a lot for reading this.
Kind regards,
liginity
next reply other threads:[~2025-03-06 11:25 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-03-06 11:24 Li Guilin via Gdb [this message]
2025-03-06 13:08 ` Guinevere Larsen via Gdb
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=tencent_14EE7DDE78E98F2D81E8E4F63B574645A406@qq.com \
--to=gdb@sourceware.org \
--cc=262703720@qq.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox