C++ Valgrind大小为1的读取无效

C++ Valgrind大小为1的读取无效,c++,debugging,valgrind,C++,Debugging,Valgrind,我一直在纠结,但似乎找不到以下代码的错误。下面是它生成的valgrind输出的一小部分 valgrind --leak-check=full --show-reachable=yes --track-origins=yes ./main ==9968== Memcheck, a memory error detector ==9968== Copyright (C) 2002-2012, and GNU GPL'd, by Julian Seward et al. ==9968== Using

我一直在纠结,但似乎找不到以下代码的错误。下面是它生成的valgrind输出的一小部分

valgrind --leak-check=full --show-reachable=yes --track-origins=yes ./main
==9968== Memcheck, a memory error detector
==9968== Copyright (C) 2002-2012, and GNU GPL'd, by Julian Seward et al.
==9968== Using Valgrind-3.8.1 and LibVEX; rerun with -h for copyright info
==9968== Command: ./main
==9968== 
==9968== Invalid read of size 1
==9968==    at 0x404B77: operator*(float, Color const&) (Color.h:30)
==9968==    by 0x403532: Image<Color>::bilinearScale(unsigned short, unsigned short) const (Image.h:533)
==9968==    by 0x404109: main (main.cpp:86)
==9968==  Address 0x5dce722 is 2 bytes after a block of size 14,400 alloc'd
==9968==    at 0x4C2AFE7: operator new[](unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==9968==    by 0x404CD7: Image<Color>::Image(unsigned short const&, unsigned short const&) (Image.h:173)
==9968==    by 0x402E20: Image<Color>::readPPM(std::istream&) (Image.h:456)
==9968==    by 0x4040D3: main (main.cpp:85)
==9968== 
==9968== Invalid read of size 1
==9968==    at 0x404B92: operator*(float, Color const&) (Color.h:30)
==9968==    by 0x403532: Image<Color>::bilinearScale(unsigned short, unsigned short) const (Image.h:533)
==9968==    by 0x404109: main (main.cpp:86)
==9968==  Address 0x5dce721 is 1 bytes after a block of size 14,400 alloc'd
==9968==    at 0x4C2AFE7: operator new[](unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==9968==    by 0x404CD7: Image<Color>::Image(unsigned short const&, unsigned short const&) (Image.h:173)
==9968==    by 0x402E20: Image<Color>::readPPM(std::istream&) (Image.h:456)
==9968==    by 0x4040D3: main (main.cpp:85)
这是我的班级颜色:

typedef unsigned char ubyte;

class Color
{
public:

    ubyte red, green, blue;

    inline Color(ubyte r=0, ubyte g=0, ubyte b = 0) : red(r), green(g), blue(b)
    {}

    friend Color operator + ( float d, const Color& v )
    {
        return Color( ubyte(v.red+d), ubyte(v.green+d), ubyte(v.blue+d));
    }

    friend Color operator * ( float d, const Color& v )
    {
        return Color( ubyte(v.red*d), ubyte(v.green*d), ubyte(v.blue*d) );
    }

    Color operator + ( const Color& v );




};

Color Color::operator + ( const Color& c )
{
    return Color( red+c.red, green+c.green, blue+c.blue );
}

你能发布
Color::operator*(float,const Color&)的代码吗?
?将有问题的行分成单独的语句,找出问题的原因。@FrédéricHamidi是的,我编辑了我的帖子。@Pépito,它看起来像
pixel()
方法有时会返回一个对
Color
实例的悬空引用(可能是本地分配的实例)。在执行缩放计算之前,我会仔细检查
p1
p4
是否始终包含有效数据。我以前使用过pixel()方法,它看起来不错。
template const T&Image::pixel(ushort x,ushort y)const{//check exception return array[width*y+x];}模板T&Image::pixel(ushort x,ushort y){//检查异常返回数组[width*y+x];}
gi->pixel(_x, _y) = (1-lambda) * ( ((1-mu)* p1)+ mu*p3 ) + lambda * ( ((1-mu) * p2) + mu * p4);
typedef unsigned char ubyte;

class Color
{
public:

    ubyte red, green, blue;

    inline Color(ubyte r=0, ubyte g=0, ubyte b = 0) : red(r), green(g), blue(b)
    {}

    friend Color operator + ( float d, const Color& v )
    {
        return Color( ubyte(v.red+d), ubyte(v.green+d), ubyte(v.blue+d));
    }

    friend Color operator * ( float d, const Color& v )
    {
        return Color( ubyte(v.red*d), ubyte(v.green*d), ubyte(v.blue*d) );
    }

    Color operator + ( const Color& v );




};

Color Color::operator + ( const Color& c )
{
    return Color( red+c.red, green+c.green, blue+c.blue );
}