\`main' 中的错误:free():无效指针(尽管没有使用 free 或 delete)

新手上路,请多包涵

我有这个简单的代码,我试图在 tutorialspoint.com 中运行

#include <iostream>

using namespace std;

class Vehicle {
    string vehicleNo;
    string color;

    protected:
    string getVehicleNo()
    {
        return vehicleNo;
    }

    string setVehicleNo(string num)
    {
        vehicleNo = num;
    }

    string getColor()
    {
        return color;
    }

    string setColor(string someColor)
    {
        color = someColor;
    }
};

class Car: public Vehicle {
    private:
    int distance;

    public:
    void setDistance(int a)
    {
        distance = a;
    }

    int calculateFare(int)
    {
        return distance * 5;
    }

    void displayInformation()
    {
        cout << distance << endl;
    }

    void useSetVehicleNo(string num)
    {
        setVehicleNo(num);
    }

    string useSetColor(string someColor)
    {
        setColor(someColor);
    }

    string useGetVehicleNo()
    {
        return getVehicleNo();
    }

    string useGetColor()
    {
        return getColor();
    }
};

int main()
{
   //string exit=n;
   string vehicleNo;
   string color;
   Car car;

   cout << "Enter car number : " << endl;
   cin >> vehicleNo;
   car.useSetVehicleNo(vehicleNo);

   cout << "Enter car color : " << endl;
   cin >> color;
   car.useSetColor(color);

   //vehicleNo = car.useGetColor();
   //color = car.useGetVehicleNo();

   //cout << vehicleNo;
   //cout << color;

   return 0;
}

运行它会给我以下输出

输入车号:

1

输入汽车颜色:

G

\* `main’ 中的错误:free():无效指针:0x00007ff3aa205ba8 *

======= 回溯:========= /lib64/libc.so.6(+0x7850e)[0x7ff3a969450e]

/lib64/libc.so.6(cfree+0x5b5)[0x7ff3a96a0165]

主要[0x400e97]

主要[0x400c79]

/lib64/libc.so.6(__libc_start_main+0xf0)[0x7ff3a963bfe0]

主要[0x400ac9]

======= 内存映射:======== 00400000-00402000 r-xp 00000000 fd:11f 59330668

/home/cg/root/main

00601000-00602000 r–p 00001000 fd:11f 59330668

/home/cg/root/main

00602000-00603000 rw-p 00002000 fd:11f 59330668

/home/cg/root/main

0140c000-0142d000 rw-p 00000000 00:00 0

[堆]

7ff3a961c000-7ff3a97d0000 r-xp 00000000 fd:11f 37749826

/usr/lib64/l

ibc-2.20.so

7ff3a97d0000-7ff3a99cf000 —p 001b4000 fd:11f 37749826

/usr/lib64/l

ibc-2.20.so

7ff3a99cf000-7ff3a99d3000 r–p 001b3000 fd:11f 37749826

/usr/lib64/l

ibc-2.20.so

7ff3a99d3000-7ff3a99d5000 rw-p 001b7000 fd:11f 37749826

/usr/lib64/l

ibc-2.20.so

7ff3a99d5000-7ff3a99d9000 rw-p 00000000 00:00 0

7ff3a99d9000-7ff3a99ef000 r-xp 00000000 fd:11f 37749844

/usr/lib64/l

ibgcc_s-4.9.2-20141101.so.1 7ff3a99ef000-7ff3a9bee000 —p 00016000 fd:11f 37749844 /usr/lib64/l

ibgcc_s-4.9.2-20141101.so.1

7ff3a9bee000-7ff3a9bef000 r–p 00015000 fd:11f 37749844

/usr/lib64/l

ibgcc_s-4.9.2-20141101.so.1

7ff3a9bef000-7ff3a9bf0000 rw-p 00016000 fd:11f 37749844

/usr/lib64/l

ibgcc_s-4.9.2-20141101.so.1

7ff3a9bf0000-7ff3a9cf7000 r-xp 00000000 fd:11f 37749280

/usr/lib64/l

ibm-2.20.so

7ff3a9cf7000-7ff3a9ef6000 —p 00107000 fd:11f 37749280

/usr/lib64/l

ibm-2.20.so

7ff3a9ef6000-7ff3a9ef7000 r–p 00106000 fd:11f 37749280

/usr/lib64/l

ibm-2.20.so

7ff3a9ef7000-7ff3a9ef8000 rw-p 00107000 fd:11f 37749280

/usr/lib64/l

ibm-2.20.so

7ff3a9ef8000-7ff3a9fe8000 r-xp 00000000 fd:11f 37749829

/usr/lib64/l

ibstdc++.so.6.0.20

7ff3a9fe8000-7ff3aa1e8000 —p 000f0000 fd:11f 37749829

/usr/lib64/l

ibstdc++.so.6.0.20

7ff3aa1e8000-7ff3aa1f0000 r–p 000f0000 fd:11f 37749829

/usr/lib64/l

ibstdc++.so.6.0.20

7ff3aa1f0000-7ff3aa1f2000 rw-p 000f8000 fd:11f 37749829

/usr/lib64/l

ibstdc++.so.6.0.20

7ff3aa1f2000-7ff3aa207000 rw-p 00000000 00:00 0

7ff3aa207000-7ff3aa228000 r-xp 00000000 fd:11f 37856921

/usr/lib64/l d-2.20.so 7ff3aa412000-7ff3aa417000 rw-p 00000000 00:00 0

7ff3aa424000-7ff3aa428000 rw-p 00000000 00:00 0

7ff3aa428000-7ff3aa429000 r–p 00021000 fd:11f 37856921

/usr/lib64/l d-2.20.so 7ff3aa429000-7ff3aa42a000 rw-p 00022000 fd:11f 37856921

/usr/lib64/l 7ff3aa429000-7ff3aa42a000 rw-p 00022000 fd:11f 37856921 /usr/lib64/l

d-2.20.so

7ff3aa42a000-7ff3aa42b000 rw-p 00000000 00:00 0

7fff1dddb000-7fff1ddfc000 rw-p 00000000 00:00 0

[堆]

7fff1ddfe000-7fff1de00000 r-xp 00000000 00:00 0

[vdso]

ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0

[vsyscall] 中止(核心转储)

原文由 Umar 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 620
2 个回答

您有三个函数的返回类型不同于 void 您没有有效的 return 语句。

如果你提高警告级别,你的编译器会告诉你它们。使用 g++ -Wall 时,我收到以下消息:

 socc.cc: In member function ‘std::string Vehicle::setVehicleNo(std::string)’:
socc.cc:19:4: warning: no return statement in function returning non-void [-Wreturn-type]
    }
    ^
socc.cc: In member function ‘std::string Vehicle::setColor(std::string)’:
socc.cc:29:4: warning: no return statement in function returning non-void [-Wreturn-type]
    }
    ^
socc.cc: In member function ‘std::string Car::useSetColor(std::string)’:
socc.cc:60:7: warning: no return statement in function returning non-void [-Wreturn-type]
       }

   ^

调用此类函数会导致未定义的行为。

来自 C++11 标准:

6.6.3 return 声明

2 …

从函数的末尾流出相当于没有值的返回;这会导致值返回函数中的未定义行为。

您可以通过添加合适的 return 语句来解决此问题。例如

第一个功能:

 string setVehicleNo(string num)
{
   vehicleNo = num;
   return vehicleNo; // Add this, maybe?
}

第二个功能:

 string setColor(string someColor)
{
   color = someColor;
   return color;  // Add this, maybe
}

第三个功能:

 string useSetColor(string someColor)
{
   return setColor(someColor); // Add the return, maybe?
}

原文由 R Sahu 发布,翻译遵循 CC BY-SA 3.0 许可协议

在您的代码中进行以下更改:

第一的 -

 string setVehicleNo(string num)
// ^^^^^^
   {
        vehicleNo = num;
   }

第二 -

 string setColor(string someColor)
// ^^^^^^
    {
        color = someColor;
    }

第三 -

 string useSetColor(string someColor)
//^^^^^^
    {
        setColor(someColor);
    }

您已将返回类型分配给 string 但未返回任何值。将其更改为 void 。查看此处所做的更改 http://ideone.com/n1Wg15

原文由 roottraveller 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题