分配内存后是否需要初始化变量?
Does it need to initialise the variable after allocating memory?
我正在尝试用 C++ 实现矩阵乘法。我找到了使用 class 写入 .h
和 .cpp
文件的示例代码。这只是与我的问题相关的部分代码:
#include "Matrix.h"
// Constructor - using an initialisation list here
Matrix::Matrix(int rows, int cols, bool preallocate): rows(rows), cols(cols), size_of_values(rows * cols), preallocated(preallocate)
{
// If we want to handle memory ourselves
if (this->preallocated)
{
// Must remember to delete this in the destructor
this->values = new double[size_of_values];
}
}
void Matrix::matMatMult(Matrix& mat_left, Matrix& output)
{
// The output hasn't been preallocated, so we are going to do that
output.values = new double[this->rows * mat_left.cols];
// Set values to zero before hand
for (int i = 0; i < output.size_of_values; i++)
{
output.values[i] = 0;
}
我想知道为什么他们使用带有 0 的输出矩阵初始化 output.values[i] = 0;
而它之前已经分配了内存?
从 cppreference 到 new
表达式:
The object created by a new-expression is initialized according to the following rules:
- [...]
- If type is an array type, an array of objects is initialized.
- If initializer is absent, each element is default-initialized
- If initializer is an empty pair of parentheses, each element is value-initialized.
“默认初始化”int
通俗地说是未初始化。它们具有不确定的值。空括号指的是 Ted 在评论中提到的内容:
output.values = new double[this->rows * mat_left.cols]{};
描述了值初始化here。这里适用的案例是
- otherwise, the object is zero-initialized.
I wonder why they initialised using the output matrix with 0s output.values[i] = 0; while it has been allocated memory before?
分配内存和初始化对象是两个独立的步骤。是的,元素要初始化,分配内存不够
我正在尝试用 C++ 实现矩阵乘法。我找到了使用 class 写入 .h
和 .cpp
文件的示例代码。这只是与我的问题相关的部分代码:
#include "Matrix.h"
// Constructor - using an initialisation list here
Matrix::Matrix(int rows, int cols, bool preallocate): rows(rows), cols(cols), size_of_values(rows * cols), preallocated(preallocate)
{
// If we want to handle memory ourselves
if (this->preallocated)
{
// Must remember to delete this in the destructor
this->values = new double[size_of_values];
}
}
void Matrix::matMatMult(Matrix& mat_left, Matrix& output)
{
// The output hasn't been preallocated, so we are going to do that
output.values = new double[this->rows * mat_left.cols];
// Set values to zero before hand
for (int i = 0; i < output.size_of_values; i++)
{
output.values[i] = 0;
}
我想知道为什么他们使用带有 0 的输出矩阵初始化 output.values[i] = 0;
而它之前已经分配了内存?
从 cppreference 到 new
表达式:
The object created by a new-expression is initialized according to the following rules:
- [...]
- If type is an array type, an array of objects is initialized.
- If initializer is absent, each element is default-initialized
- If initializer is an empty pair of parentheses, each element is value-initialized.
“默认初始化”int
通俗地说是未初始化。它们具有不确定的值。空括号指的是 Ted 在评论中提到的内容:
output.values = new double[this->rows * mat_left.cols]{};
描述了值初始化here。这里适用的案例是
- otherwise, the object is zero-initialized.
I wonder why they initialised using the output matrix with 0s output.values[i] = 0; while it has been allocated memory before?
分配内存和初始化对象是两个独立的步骤。是的,元素要初始化,分配内存不够