Mirror of the gdb mailing list
 help / color / mirror / Atom feed
* Question about printing cpp std::map element using "print map1[0]"
@ 2025-03-06 11:24 Li Guilin via Gdb
  2025-03-06 13:08 ` Guinevere Larsen via Gdb
  0 siblings, 1 reply; 2+ messages in thread
From: Li Guilin via Gdb @ 2025-03-06 11:24 UTC (permalink / raw)
  To: gdb

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


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: Question about printing cpp std::map element using "print map1[0]"
  2025-03-06 11:24 Question about printing cpp std::map element using "print map1[0]" Li Guilin via Gdb
@ 2025-03-06 13:08 ` Guinevere Larsen via Gdb
  0 siblings, 0 replies; 2+ messages in thread
From: Guinevere Larsen via Gdb @ 2025-03-06 13:08 UTC (permalink / raw)
  To: Li Guilin, gdb

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
>


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2025-03-06 13:09 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-03-06 11:24 Question about printing cpp std::map element using "print map1[0]" Li Guilin via Gdb
2025-03-06 13:08 ` Guinevere Larsen via Gdb

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox