为什么会出现内存溢出
#include"Vector.hpp"
#include
using namespace std;
#include
#include
Vector::Vector(string s1, int n, int p[])
{
name=s1;
cout<<"construct a vector called "<"."<new int[n];
for(int i=0;iVector(const Vector &otherVec)
{
name=otherVec.name;
dimension=otherVec.dimension;
param =new int[dimension];
for(int i=0;i"copy a vector called "<"."<Vector()
{
delete []param;
cout<<"release memory from a vector called "<"."<void Vector::isEqual(const Vector &p)
{
int j=0,k=0;
if(this->name==p.name) j=1;
for(int i=0;iif(this->param[i]!=p.param[i])
{
k=1;
if(j==0) cout<< "different name,different value"<else cout<<"same name, different value."<break;
}
}
if(k==0)
{
if(j==0) cout<<"different name, same value."<else cout<<"same name, same value."<void Vector::setName(string a)
{
name=a;
}
void Vector::print()
{
cout<"(";
for(int i=0;i<(dimension-1);i++)
{
cout<", ";
}
cout<-1]<<")"<#ifndef VECTOR_H
#define VECTOR_H
#include
using namespace std;
class Vector {
public:
Vector() {
name = "";
dimension = 0;
param = NULL;
}
Vector(string, int, int[]);
Vector(const Vector &otherVec);
~Vector();
void isEqual(const Vector &);
void setName(string);
void print();
private:
string name;
int dimension, *param;
};
#endif
#include
#include
#include "Vector.hpp"
#define MAX 51
using namespace std;
int main() {
string name = "";
int dim, num[MAX];
cin >> name >> dim;
for (int i = 0; i < dim; i++) {
cin >> num[i];
}
Vector vec1(name, dim, num);
vec1.print();
Vector vec2(vec1);
vec2.print();
vec1.isEqual(vec2);
int vec3_num[] = {1, 2, 3};
Vector vec3(name, 3, vec3_num);
vec1.isEqual(vec3);
vec2.setName("helloWorld!");
vec1.isEqual(vec2);
return 0;
}
AddressSantizer 报告全文
=================================================================
==2==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x60200000001c at pc 0x55dab4017cb5 bp 0x7ffdd49d1b60 sp 0x7ffdd49d1b58
READ of size 4 at 0x60200000001c thread T0
#0 0x55dab4017cb4 in Vector::isEqual(Vector const&) /judge/Vector.cpp:43
#1 0x55dab4014a17 in main /judge/main.cpp:20
#2 0x14a20928cd09 in __libc_start_main ../csu/libc-start.c:308
#3 0x55dab4014df9 in _start (/judge/run-asan+0x7df9)
0x60200000001c is located 0 bytes to the right of 12-byte region [0x602000000010,0x60200000001c)
allocated by thread T0 here:
#0 0x14a20a0277a7 in operator new[](unsigned long) ../../../../src/libsanitizer/asan/asan_new_delete.cpp:102
#1 0x55dab4015619 in Vector::Vector(std::__cxx11::basic_string, std::allocator >, int, int*) /judge/Vector.cpp:11
#2 0x55dab40149f6 in main /judge/main.cpp:19
#3 0x14a20928cd09 in __libc_start_main ../csu/libc-start.c:308
SUMMARY: AddressSanitizer: heap-buffer-overflow /judge/Vector.cpp:43 in Vector::isEqual(Vector const&)
Shadow bytes around the buggy address:
0x0c047fff7fb0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x0c047fff7fc0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x0c047fff7fd0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x0c047fff7fe0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x0c047fff7ff0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
=>0x0c047fff8000: fa fa 00[04]fa fa fa fa fa fa fa fa fa fa fa fa
0x0c047fff8010: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
0x0c047fff8020: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
0x0c047fff8030: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
0x0c047fff8040: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
0x0c047fff8050: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
Shadow byte legend (one shadow byte represents 8 application bytes):
Addressable: 00
Partially addressable: 01 02 03 04 05 06 07
Heap left redzone: fa
Freed heap region: fd
Stack left redzone: f1
Stack mid redzone: f2
Stack right redzone: f3
Stack after return: f5
Stack use after scope: f8
Global redzone: f9
Global init order: f6
Poisoned by user: f7
Container overflow: fc
Array cookie: ac
Intra object redzone: bb
ASan internal: fe
Left alloca redzone: ca
Right alloca redzone: cb
Shadow gap: cc
==2==ABORTING
尝试了先把指针置空,但也还是显示溢出,为什么会溢出且怎么样才能避免溢出
请提供你的输入参数