2 ljt326053002 ljt326053002 于 2015.05.27 22:06 提问

c++内存问题!!!!!!!

#include
#include
#include
using namespace std;
int main() {
string a, b;
int i, x, y;
vector v1, v2;
while (cin >> a >> b) {
for (i = 0; i < v1.size(); i++) {
if (v1[i] == a) {
x = i;
break;
}
}
if (i == v1.size())
x = i;
for (i = 0; i < v2.size(); i++) {
if (v2[i] == b) {
y = i;
break;
}
}
if (i == v2.size())
y = i;
if (x == v1.size() && y == v2.size()) {
cout << b << " registered." << endl;
v1.push_back(a);
v2.push_back(b);
}
else if (v1[y] == a && v2[x] == b) {
cout << b << " signed in." << endl;
}
else if (x != v1.size() && y == v2.size()) {
cout << "GOTCHA: MAC address already occupied by ";
cout << v2[x] << "." << endl;
}
else
{ cout << "GOTCHA: " << b << " MAC address not consistent." << endl;
}
}
return 0;
}
Invalid read of size 8
| at 0x3F8CC9C0D0: std::string::size() const (in /usr/lib64/libstdc++.so.6.0.13)
| by 0x40175D: gnu_cxx::enable_ifstd::__is_char<char::__value, bool>::__type std::operator==(std::basic_string, std::allocator > const&, std::basic_string, std::allocator > const&) (basic_string.h:2243)
| by 0x4012B5: main (main.cpp:30)
| Address 0x4c29498 is 8 bytes before a block of size 8 alloc'd
| at 0x4A075BC: operator new(unsigned long) (vg_replace_malloc.c:298)
| by 0x402184: gnu_cxx::new_allocatorstd::string::allocate(unsigned long, void const*) (new_allocator.h:89)
| by 0x401F80: std::_Vector_base >::_M_allocate(unsigned long) (stl_vector.h:140)
| by 0x401BBB: std::vector >::_M_insert_aux(
gnu_cxx::__normal_iterator > >, std::string const&) (vector.tcc:322)
| by 0x401877: std::vector >::push_back(std::string const&) (stl_vector.h:741)
| by 0x40122E: main (main.cpp:27)
|
|Invalid read of size 8
| at 0x3F8CC9C0D3: std::string::size() const (in /usr/lib64/libstdc++.so.6.0.13)
| by 0x40175D: gnu_cxx::enable_ifstd::__is_char<char::__value, bool>::__type std::operator==(std::basic_string, std::allocator > const&, std::basic_string, std::allocator > const&) (basic_string.h:2243)
| by 0x4012B5: main (main.cpp:30)
| Address 0xffffffffffffffe8 is not stack'd, malloc'd or (recently) free'd
|
|
|Process terminating with default action of signal 11 (SIGSEGV)
| Access not within mapped region at address 0xFFFFFFFFFFFFFFE8
| at 0x3F8CC9C0D3: std::string::size() const (in /usr/lib64/libstdc++.so.6.0.13)
| by 0x40175D: gnu_cxx::enable_ifstd::__is_char<char::__value, bool>::__type std::operator==(std::basic_string, std::allocator > const&, std::basic_string, std::allocator > const&) (basic_string.h:2243)
| by 0x4012B5: main (main.cpp:30)
| If you believe this happened as a result of a stack
| overflow in your program's main thread (unlikely but
| possible), you can try to increase the size of the
| main thread stack using the --main-stacksize= flag.
| The main thread stack size used in this run was 10485760.
|66 bytes in 2 blocks are possibly lost in loss record 3 of 4
| at 0x4A075BC: operator new(unsigned long) (vg_replace_malloc.c:298)
| by 0x3F8CC9C3C8: std::string::_Rep::_S_create(unsigned long, unsigned long, std::allocator const&) (in /usr/lib64/libstdc++.so.6.0.13)
| by 0x3F8CC9D19A: std::string::_Rep::_M_clone(std::allocator const&, unsigned long) (in /usr/lib64/libstdc++.so.6.0.13)
| by 0x3F8CC9D5EB: std::string::reserve(unsigned long) (in /usr/lib64/libstdc++.so.6.0.13)
| by 0x3F8CC7B05A: std::basic_istream >& std::operator>>, std::allocator >(std::basic_istream >&, std::basic_string, std::allocator >&) (in /usr/lib64/libstdc++.so.6.0.13)
| by 0x401431: main (main.cpp:8)
|
|114 bytes in 2 blocks are possibly lost in loss record 4 of 4
| at 0x4A075BC: operator new(unsigned long) (vg_replace_malloc.c:298)
| by 0x3F8CC9C3C8: std::string::_Rep::_S_create(unsigned long, unsigned long, std::allocator const&) (in /usr/lib64/libstdc++.so.6.0.13)
| by 0x3F8CC9D19A: std::string::_Rep::_M_clone(std::allocator const&, unsigned long) (in /usr/lib64/libstdc++.so.6.0.13)
| by 0x3F8CC9D5EB: std::string::reserve(unsigned long) (in /usr/lib64/libstdc++.so.6.0.13)
| by 0x3F8CC7B05A: std::basic_istream >& std::operator>>, std::allocator >(std::basic_istream >&, std::basic_string, std::allocator >&) (in /usr/lib64/libstdc++.so.6.0.13)
| by 0x40141F: main (main.cpp:8)
怎么破! 求大神

5个回答

oyljerry
oyljerry   Ds   Rxr 2015.05.27 22:54

应该是对vector,string的
误报。

ljt326053002
ljt326053002 谢谢回答!! 是越界了……自己傻逼了
2 年多之前 回复
jymfist
jymfist   2015.05.27 23:11

用的是什么编译器,vector v1, v2; 这个都编得过?
改成下面这样子,跑起来木有问题

#include
#include
#include
using namespace std;
int main() {
string a, b;
int i, x, y;
vector v1, v2;
while (cin >> a >> b) {
for (i = 0; i < v1.size(); i++) {
if (v1[i] == a) {
x = i;
break;
}
}
if (i == v1.size())
x = i;
for (i = 0; i < v2.size(); i++) {
if (v2[i] == b) {
y = i;
break;
}
}
if (i == v2.size())
y = i;
if (x == v1.size() && y == v2.size()) {
cout << b << " registered." << endl;
v1.push_back(a);
v2.push_back(b);
}
else if (v1[y] == a && v2[x] == b) {
cout << b << " signed in." << endl;
}
else if (x != v1.size() && y == v2.size()) {
cout << "GOTCHA: MAC address already occupied by ";
cout << v2[x] << "." << endl;
}
else
{ cout << "GOTCHA: " << b << " MAC address not consistent." << endl;
}
}
return 0;
}


jymfist
jymfist   2015.05.27 23:13

SORRY,贴错了,下面才是我的改动,我用的是老掉牙的VC6++

#include
#include
#include
using namespace std;
int main() {
string a, b;
int i, x, y;
vector v1, v2;
while (cin >> a >> b) {
for (i = 0; i < v1.size(); i++) {
if (v1[i] == a) {
x = i;
break;
}
}
if (i == v1.size())
x = i;
for (i = 0; i < v2.size(); i++) {
if (v2[i] == b) {
y = i;
break;
}
}
if (i == v2.size())
y = i;
if (x == v1.size() && y == v2.size()) {
cout << b << " registered." << endl;
v1.push_back(a);
v2.push_back(b);
}
else if (v1[y] == a && v2[x] == b) {
cout << b << " signed in." << endl;
}
else if (x != v1.size() && y == v2.size()) {
cout << "GOTCHA: MAC address already occupied by ";
cout << v2[x] << "." << endl;
}
else
{ cout << "GOTCHA: " << b << " MAC address not consistent." << endl;
}
}
return 0;
}

ljt326053002
ljt326053002 谢谢回答!! 编译没有问题 是那个 v1[y]那里可能造成越界。 总之谢谢你的回答!
2 年多之前 回复
u012271952
u012271952   2015.05.27 23:17

有bug,自己学会debug,其实也是挺有成就感的事情。

ljt326053002
ljt326053002 恩恩!! 自己搞定了!! 昨晚实在比较急……
2 年多之前 回复
jymfist
jymfist   2015.05.27 23:18

SORRY AGAIN,原来是这边贴代码会把尖括号干掉,你的代码,我的编译器编译后木有问题。
有可能是64bit ---》 32bit的问题,查查你的编译环境和执行环境







Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!