gpt4 book ai didi

c++ - 错误在 `main' : free(): invalid pointer (though no free or delete used)

转载 作者:太空宇宙 更新时间:2023-11-04 14:36:16 27 4
gpt4 key购买 nike

我有这个简单的代码,我想在 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;
}

运行它会给我以下输出

Enter car number :
1
Enter car color :
g

* Error in `main': free(): invalid pointer: 0x00007ff3aa205ba8 *
======= Backtrace: ========= /lib64/libc.so.6(+0x7850e)[0x7ff3a969450e]
/lib64/libc.so.6(cfree+0x5b5)[0x7ff3a96a0165]
main[0x400e97]
main[0x400c79]
/lib64/libc.so.6(__libc_start_main+0xf0)[0x7ff3a963bfe0]
main[0x400ac9]

======= Memory map: ======== 00400000-00402000 r-xp 00000000 fd:11f 59330668
/home/cg/roo t/main
00601000-00602000 r--p 00001000 fd:11f 59330668
/home/cg/roo t/main
00602000-00603000 rw-p 00002000 fd:11f 59330668
/home/cg/roo t/main
0140c000-0142d000 rw-p 00000000 00:00 0
[heap]
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
[stack]
7fff1ddfe000-7fff1de00000 r-xp 00000000 00:00 0
[vdso]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0
[vsyscall] Aborted (core dumped)

最佳答案

您有三个返回类型不同于 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 statement

2 ...
...
Flowing off the end of a function is equivalent to a return with no value; this results in undefined behavior in a value-returning function.

您可以通过添加合适的 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?
}

关于c++ - 错误在 `main' : free(): invalid pointer (though no free or delete used),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34714541/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com