向量函数给出分割故障



hadamard_product()中标记的行给出了以下源代码中的分段错误。

#include <iostream>
#include <cstdlib>
#include <ctime>
class Vector
{
private:
int length;
float * elements;
bool is_column;//column-vector = single column, multiple rows.
//row-vector = singl row, multiple columns.
public:
Vector()
{
this->length = 0;
elements = NULL;
is_column = true;
}
void set_length(int length)
{
this->length = length;
elements = new float[length];
}
void set_row_vector(bool is_row)
{
is_column = !is_row;
}
float get_length() const
{
return length;
}
// Copy constructor
Vector(const Vector& other)
{
this->length = other.length;
this->elements = new float[length];
for (int i = 0; i < length; i++) 
{
this->elements[i] = other.elements[i];
}
}
// Destructor
~Vector()
{
this->length = 0;
if (elements != NULL) 
{
delete[] elements;
}
}

Vector Clone() const
{
Vector v(*this);
return v; 
}
// Overloaded assignment operator
Vector& operator=(const Vector& other)
{
if (this != &other) 
{
this->length = other.length;
delete[] this->elements;
this->elements = new float[length];
for (int i = 0; i < length; i++) 
{
this->elements[i] = other.elements[i];
}
}
return *this;
}
float& operator[](int index)//will modify the state of the object 
//when assigned a value
{
return elements[index];
}
bool operator==(const Vector& other) const//this function will not modify the state 
//of the object on which it is called
{
if (this->length != other.length) 
{
return false;
}
for (int i = 0; i < this->length; i++) 
{
if (this->elements[i] != other.elements[i]) 
{
return false;
}
}
return true;
}   
void scalar_product(float scalar)
{
for(int i=0 ; i<length ; i++)
{
this->elements[i] = this->elements[i] * scalar;
}
}
friend Vector operator*(float scalar, const Vector& vec)
//Vector operator*(float scalar, const Vector& vec) const
{
Vector result(vec);
result.scalar_product(scalar);
return result;
}
void display() const
{
if (length == 0) 
{
std::cout << "[]" << std::endl;
return;
}

if (is_column)
{
std::cout << "[";
for (int i = 0; i < length - 1; i++)
{
std::cout << elements[i] << std::endl;
}
std::cout << elements[length - 1] << "]" << std::endl;
}
else
{
std::cout << "[";
for (int i = 0; i < length - 1; i++)
{
std::cout << elements[i] << " ";
}
std::cout << elements[length - 1] << "]" << std::endl;
}       
}
bool is_column_vector() const
{
return is_column;
}

float dot_product(const Vector& other) const//inner product
{
if (!(!this->is_column && other.is_column_vector()))
{
throw std::runtime_error("Error: lhs must be row vector, rhs must be column vector");
}

if (this->length != other.length) 
{
throw std::runtime_error("Error: Vectors must have the same length to perform Dot product");
return 0.0;
}
float result = 0.0;
for (int i = 0; i < this->length; i++) 
{
result += this->elements[i] * other.elements[i];
}
return result;
}

void hadamard_product(const Vector& rhs, Vector ** returns)
{
if (!(this->is_column && !rhs.is_column_vector()))
{
throw std::runtime_error("Error: rhs must be a row-vector");
}
if (this->length != rhs.length)
{
throw std::runtime_error("Error: Vectors must have the same length to perform Hadamard product");
}
*returns = new Vector[this->length];//<<<segmentation fault
int index = 0;
for (int i = 0; i < this->length; i++)
{
Vector temp = rhs.Clone();
temp.display();
float val = this->elements[i];
temp.scalar_product(val);

(*returns)[index] = temp;
index++;
}
}
};
int main()
{
float array[] = {1.0, 2.0, 3.0};

Vector u;
u.set_length(3);
u[0] =-6;
u[1] = 1;
u[2] = 4;
u.display();

Vector v;
v.set_length(3);
v.set_row_vector(true);
v[0] = 3;
v[1] =-5;
v[2] = 2;
v.display();

try 
{
Vector ** returns;
u.hadamard_product(v, returns);
//for (int i=0 ; i<u.get_length() ; i++)
//{
//  returns[i]->display();
//  delete returns[i];
//}
//delete[] returns; // free memory
} 
catch (std::exception& e) 
{
std::cerr << "Error: " << e.what() << std::endl;
}
}

我做错了什么,我怎么解决它?

您想要为指向Vector的指针分配堆栈空间,然后发送指向堆栈上该指针的指针。当前,您正在堆栈上分配指向Vector的指针,然后在hadamard_product函数中对其解引用,这显然是段错误。

你想要Vector *returns; u.hadamard_product(v, &returns);

最新更新