Home > Back-end >  Memory transfinite how to solve
Memory transfinite how to solve

Time:10-03

# include & lt; Iostream>
# include & lt; string.h>
using namespace std;

Class Vector {
int n;
Int * arr;
Public:
The Vector (int num=0) : n (num)
{
Arr=new int [n].
for(int i=0; i{
Arr [I]=0;
}
}
The Vector (const Vector & amp; Vec) : n (Vec) n)
{
Arr=new int [n].
Memcpy (arr, Vec. Arr, n * sizeof (int));
}
~ the Vector () {delete [] arr; }
Friend ostream & amp; Operator<(ostream & amp; OS, const Vector & amp; Vec);
Friend cost & amp; Operator> (cost & amp; Is that the Vector & amp; Vec);
};

Ostream & amp; Operator<(ostream & amp; OS, const Vector & amp; Vec)
{
Int I;
for(i=0; i{
OsOs<" ";
}
OsReturn the OS;
}
Cost & amp; Operator> (cost & amp; Is that the Vector & amp; Vec)
{

for(int i=0; i{
Is> Vec. Arr [I];
}
The return is;
}
Int main ()
{
int num;
Cin> Num.

The Vector vec1 (num);

cout
Cin> Vec1;

cout
Const Vector vec2=vec1;
cout
The Vector vec3 (num);

Vec3=vec2;
cout
return 0;
}


There is something wrong with
?What is memory transfinite... ...

CodePudding user response:

 
The Vector (int num=0) : n (num)
{
Arr=new int [n].//n==0, meaningless
for(int i=0; i{
Arr [I]=0;
}
}
The Vector (const Vector & amp; Vec) : n (Vec) n)
{
Arr=new int [n].//n==0, meaningless
Memcpy (arr, Vec. Arr, n * sizeof (int));
}


CodePudding user response:

You did not provide operator=, so wrong

CodePudding user response:

refer to the second floor truth is right or wrong response:
you did not provide operator=, so error

Vec1=vec2 isn't directly copy function called it, reload the operator, please

CodePudding user response:

reference 1st floor GKatHere response:
 
The Vector (int num=0) : n (num)
{
Arr=new int [n].//n==0, meaningless
for(int i=0; i{
Arr [I]=0;
}
}
The Vector (const Vector & amp; Vec) : n (Vec) n)
{
Arr=new int [n].//n==0, meaningless
Memcpy (arr, Vec. Arr, n * sizeof (int));
}



But change after will show memory transfinite

CodePudding user response:

reference qq_45702129 reply: 3/f
Quote: refer to the second floor truth is right or wrong response:
you did not provide operator=, so error

Vec1=vec2 not directly call copy function, but also overloaded the operator, please

Initialization calls the constructor, the assignment is called operstor=

CodePudding user response:

This will cause memory overrun? I don't really understand the connection between the two

CodePudding user response:

Super super? Are you always say, exactly how, I don't know.
I know you don't realize the operator=, is bound to go wrong,

CodePudding user response:

refer to 7th floor truth is right or wrong response:
not ultra? Are you always say, exactly how, I don't know.
I know you don't realize the operator=, is bound to go wrong,

Well, ok, I changed the place to try again, thank you??

CodePudding user response:

The new x [n] is what version of the c + + syntax?
  • Related