Sizeof array reference in gdb

int main()
{
    typedef unsigned char a4[4];
    a4 p1;
    a4& p2 = p1;
    p2[1]=1;
    cout<<sizeof(p2);
    return p2[1];
}

Compile, run gdb and set a breakpoint on return. If you type p sizeof(p2), gdb will print 8 instead of 4, which will be printed if you run the program. If you write in gdb p sizeof(*p2), then the result is 4 (array size). I think this is due to the fact that gdb treats p2 as a pointer (the link is executed behind the scene as a pointer).

Tested with GCC 4.8.2 and Clang 4.3 compilers on GDB 7.7 linux arch., Ubuntu 13.10,

Is this correct or a bug in gdb?

+4
source share
1 answer

​​ . 4 17, , . , , #include <iostream>, . .

#include <iostream>
int main()
{
    typedef unsigned char char17[17];
    char17 arr17;
    char17& arr17_ref = arr17;
    std::cout << "sizeof(arr17) = "
              << sizeof arr17
              << ", sizeof(arr17_ref) = "
              << sizeof(arr17_ref)
              << "\n";
    return 0;
}

, 17.

gdb, 8 ( ):

$ gdb ./c
GNU gdb (GDB) 7.5-ubuntu
[snip]
Reading symbols from /home/kst/c...done.
(gdb) b 12
Breakpoint 1 at 0x40097e: file c.cpp, line 12.
(gdb) r
Starting program: /home/kst/c 
sizeof(arr17) = 17, sizeof(arr17_ref) = 17

Breakpoint 1, main () at c.cpp:12
12          return 0;
(gdb) p sizeof(arr17)
$1 = 17
(gdb) p sizeof(arr17_ref)
$2 = 8
(gdb) c
Continuing.
[Inferior 1 (process 23420) exited normally]
(gdb) q
$ 

, gdb. gdb , ; .

( gcc 4.7.2 gdb 7.5 Linux Mint 14.)

:

: https://sourceware.org/bugzilla/show_bug.cgi?id=16675 . 2014-04-14. gdb 7.7.1, ​​ 7.11.1.

+4

Source: https://habr.com/ru/post/1530647/


All Articles