Mirror of the gdb mailing list
 help / color / mirror / Atom feed
From: Guinevere Larsen via Gdb <gdb@sourceware.org>
To: Li Guilin <262703720@qq.com>, gdb@sourceware.org
Subject: Re: Question about printing cpp std::map element using "print map1[0]"
Date: Thu, 6 Mar 2025 10:08:35 -0300	[thread overview]
Message-ID: <db723468-7144-4dec-9837-16dda41d151b@redhat.com> (raw)
In-Reply-To: <tencent_14EE7DDE78E98F2D81E8E4F63B574645A406@qq.com>

On 3/6/25 8:24 AM, Li Guilin via Gdb wrote:
> 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.

Hi!

This is, unfortunately, a known issue. There's a bug that has been 
opened about this, here: 
https://sourceware.org/bugzilla/show_bug.cgi?id=25957

I'll bump that bug, and thanks for asking here, I hope someone who has 
time can fix this soon!

-- 
Cheers,
Guinevere Larsen
She/Her/Hers

>
> Kind regards,
>
> liginity
>


      reply	other threads:[~2025-03-06 13:09 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-03-06 11:24 Li Guilin via Gdb
2025-03-06 13:08 ` Guinevere Larsen via Gdb [this message]

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=db723468-7144-4dec-9837-16dda41d151b@redhat.com \
    --to=gdb@sourceware.org \
    --cc=262703720@qq.com \
    --cc=guinevere@redhat.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