Printing sum of double vector in gdb C++

Is it possible to print sum of a vector of doubles in while debugging in GDB? I know the values of a vector can be printed by *(vec._M_impl._M_start)@N, but I just want summation of huge array what can be done? One solution is to write C++ function and call it in GDB. Any other easier solution?


It can be done via gdb script, but I think writing c++ function and calling it is better solution.

Nonetheless, the script (created by taking STL support tools and modifying it) goes like this:

define pvecsum
    set $sum = 0
    set $size = $arg0._M_impl._M_finish - $arg0._M_impl._M_start
    if $argc == 1
        set $i = 0
            while $i < $size
                set $sum = $sum + *($arg0._M_impl._M_start + $i)
                set $i++
            end
        end
    printf "sum of elements: %fn", $sum
end

So, you need to start gdb by something like gdb -x abovesctipt.gdb myexecutable, then to print sum do pvecsum name_of_vector.

Disclaimer: I have not idea how close the printed result is to the one obtained by similar c++ function.

链接地址: http://www.djcxy.com/p/24378.html

上一篇: 复数的实部和虚部

下一篇: 在gdb C ++中打印双向量的和