在函数之间传递堆上的数组时,是什么导致了这个运行时错误,c++
What is causing this runtime error while passing arrays on the heap between functions, c++
当我在接受双指针的函数之间传递堆上声明的2d数组时,我无法理解为什么我的程序挂起并崩溃。
我有一种强烈的感觉,这与我选择的声明2d数组的方法有关。在我创建一个函数来分配数组之前,程序可以在传递给函数时处理数组中的数据。
这是分配函数,然后是它内部崩溃的函数:
void matrix_malloc(int **matrix, int m, int n);
void matrix_init(int **matrix, int m, int n);
int main(void)
{
int **matrix;
int m(3), n(2);
matrix_malloc(matrix, m, n);
matrix_init(matrix, m, n); // runtime error
}
void matrix_malloc(int **matrix, int m, int n)
{ // get heap memory
int i;
matrix = new int*[m];
for(i = 0; i < m; i++)
{
matrix[i] = new int[n];
}
}
void matrix_init(int **matrix, int m, int n)
{ // randomize matrix
int i, j;
for(i = 0; i < m; i++)
{
for(j = 0; j < n; j++)
{
matrix[i][j] = rand() % 10 + 1;
}
}
}
您必须通过引用传递矩阵指针。
void matrix_malloc(int **matrix, int m, int n)
这接受矩阵的副本。这意味着您在matrix_malloc
中对matrix
所做的任何操作都不会影响main
中的操作。
相反,它应该是
void matrix_malloc(int **& matrix, int m, int n)
^^^
但是,我建议您使用向量,而不是原始指针和分配。这样你就不需要担心分配和交易。
void matrix_malloc(vector<vector<int> >& matrix, int m, int n);
// You don't need this anymore.
// void matrix_init(int **matrix, int m, int n);
int main(void)
{
vector<vector<int> > matrix;
int m(3), n(2);
// matrix_malloc(matrix, m, n);
matrix_init(matrix, m, n);
}
void matrix_init(vector<vector<int> >& matrix, int m, int n)
{ // randomize matrix
int i, j;
for(i = 0; i < m; i++)
{
vector<int> row;
for(j = 0; j < n; j++)
{
row.push_back(rand() % 10 + 1);
// matrix[i][j] = rand() % 10 + 1;
}
matrix.push_back(row);
}
}
void matrix_malloc(int **&matrix, int m, int n);
void matrix_init(int **matrix, int m, int n);
void matrix_malloc(int **&matrix, int m, int n)
{ // get heap memory
int i;
matrix = new int*[m];
for(i = 0; i < m; i++)
{
matrix[i] = new int[n];
}
}
而且应该工作得很好。问题,因为在这个之后
matrix = new int*[m];
矩阵有新的地址,但由于它是指针的本地副本,所以main不知道它。
matrix_malloc()
需要通过引用获取指针:
void matrix_malloc(int **&matrix, int m, int n)
^
如果没有这一点,新分配的指针就不会传播回调用方。
也就是说,返回函数中新分配的指针可能更明确:
int** matrix_malloc(int m, int n)
最后,您不使用std::vector
的原因是什么?
2D数组的分配很好。但是。
int **matrix;
int m(3), n(2);
matrix_malloc(matrix, m, n);
在这里,矩阵不会改变-您正在复制它的值以将其传递给函数。我的意思是:
int **matrix = NULL; // matrix points to null
int m(3), n(2);
matrix_malloc(matrix, m, n); // copy the value contained in matrix and give it to the function
//matrix still points to null
您有多种解决方案:
- 矩阵malloc可以返回一个int**,您只需要写矩阵=矩阵_ malloc(m,n(
- 您的矩阵malloc可以使用指向int**的指针(int***-小心处理(
- 如其他答案中所述,对int的引用**
下面是一个带int***的matrix_malloc的样子。
//call it as follows:
matrix_malloc(&matrix, m, n);
void matrix_malloc(int ***matrix, int m, int n)
{
// matrix contains the address of the original variable, so *matrix is the original variable itself.
int i;
*matrix = new int*[m];
for(i = 0; i < m; i++)
{
(*matrix)[i] = new int[n];
}
}
相关文章:
- 为不同配置设置MSVC_RUNTIME_LIBRARY的正确方法是什么
- C++避免重复声明的语法是什么
- 在C++中,将大的无符号浮点数四舍五入为整数的最佳方法是什么
- 实现无开销push_back的最佳方法是什么
- C++从另一个类访问公共静态向量的正确方法是什么
- "throw expression code" 1e7 >返回 d 是什么?投掷标准::overflow_error( "too big" ) : d;意味 着?
- C++中名称篡改的目的是什么
- 在 c++ 中拥有一组结构的正确方法是什么?
- 这个指针和内存代码打印是什么?我不知道是打印垃圾还是如何打印我需要的值
- 是什么阻止DOMTimerCoordinator::NextID进入无休止的循环
- 派生类销毁的最佳实践是什么
- 这个语法std::class<>{}(arg1, arg2) 在C++中是什么意思?
- 通过JNI传递数据数组的最快方法是什么
- "using namespace std;"在C++的作用是什么?
- 在两台机器之间进行时间戳的最佳c++chrono函数是什么
- 文件系统:复制功能的速度秘诀是什么
- 用常见虚拟函数实现的任意组合来实现派生类的正确方法是什么
- 使用QQuickFramebufferObject时同步数据的最佳方式是什么
- 是什么原因导致它无法编译?它是声明签名还是在函数本身的实现中
- 使用不同的CRT将新的C++代码与旧的(二进制)组件隔离开来的最佳方法是什么