如何解决主函数中的矩阵到数组函数转换调用出现异常汇编代码错误
我想将Matrix转换为数组,并设法在主函数中进行操作,但是当需要将其放入函数中时,它停止工作,并且出现访问冲突错误,该错误没有不在主要范围内显示。
这是代码,我需要帮助才能完成转换。我需要从项目的类函数中强制转换。
#include <iostream>
const int rows = 3,cols = 4; //define constant rows and columns
int* MatrixToArray(static int* matrix[rows][cols]) {
int* array[rows*cols]{};//initialize the array
int* ptrResult=nullptr;//initialize the return variable
//First loop to fill the matrix with dissociated values to print out
for (int i = 0; i < rows-1; i++) {
for (int j = 0; j < cols-1; j++) {
if (matrix) {
*matrix[i][j] = i + j + i * (j + cols); ///exception violation access memory
}
if (matrix) {//debug warning C6011
std::cout << " [" << *matrix[i][j] << "]";
}
}
std::cout << ",\n";
}
//second reversed loop to get the unified array from the prevIoUs matrix
for (int u = 0; u < 1; u++) {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (array) {//debug warning C6011
array[u] = matrix[i][j];
if (ptrResult) {//debug warning C6011
ptrResult = array[u];
}
}
std::cout << "[" << *array[u] << "] ";
}
}
}
free(array);
return ptrResult;
}
int main()
{
int* matrix[rows][cols];
std::cout << MatrixToArray(matrix);
}
调整矩阵和数组的类型后,我得到以下代码:
#include <iostream>
const int rows = 3,cols = 4; //define constant rows and columns
int* MatrixToArray(int** matrix) {
int* array[rows*cols]{};//initialize the array
int* ptrResult=nullptr;//initialize the return variable
//allocate memory as requested in the subedit comment field answers
matrix = new int* [rows];
for (int i = 0; i < rows; ++i)
matrix[i] = new int[cols];
//First loop to fill the matrix with dissociated values to print out
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (matrix) {
matrix[i][j] = i + j + i * (j + cols); ///exception violation access memory disappear
}
if (matrix) {//debug warning C6011
std::cout << " [" << matrix[i][j] << "]";
}
}
std::cout << ",\n";
}
//second reversed loop to get the unified array from the prevIoUs matrix
if (array) {
for (int u = 0; u < 1; u++) {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (array) {//debug warning C6011
array[u] = &matrix[i][j];
if (ptrResult) {//debug warning C6011
ptrResult = array[u];
}
}
//std::cout << "[" << *array[u] << "] ";
}
}
}
free(array);//File: minkernel crts ucrt src appcrt heap debug_heap.cpp Line:904 expression: _CrtlsValidHeapPointer(block)
}
return ptrResult;
}
int main()
{
//memory allocation correction
int** matrix;
matrix = new int* [rows];
for (int i = 0; i < rows; ++i)
matrix[i] = new int[cols];
std::cout << MatrixToArray(matrix);
}
我仍然遇到异常错误,但在汇编代码中,该代码最终变成复制在引用行旁的注释中的消息。
解决方法
#include <iostream>
const int rows = 3,cols = 4; //define constant rows and columns
int* MatrixToArray(int** matrix) {
int* array= new int[rows * cols]{};//initialize the array (with new keyword)
int* ptrResult=nullptr;//initialize the return variable
matrix = new int* [rows];
for (int i = 0; i < rows; ++i)
matrix[i] = new int[cols];
std::cout << "[";
//First loop to fill the matrix with dissociated values to print out
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (matrix) {
matrix[i][j] = i + j + i * (j + cols); ///exception violation access memory disappeared
}
if (matrix) {//debug warning C6011
std::cout << " [" << matrix[i][j] << "]";
}
}
if (i != rows - 1) {
std::cout << ",\n";
}
}std::cout << "]";
//second reversed loop to get the unified array from the previous matrix
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (array) {//debug warning C6011
array[0] = matrix[i][j]; //modified with new initialization
if (ptrResult) {//debug warning C6011
*ptrResult = array[0];//modified with new initilization
}
}
}
}
delete[] array;
return ptrResult;
}
int main()
{
int** TheMatrix;
TheMatrix = new int* [rows];
for (int i = 0; i < rows; ++i)
TheMatrix[i] = new int[cols];
std::cout << MatrixToArray(TheMatrix);
for (int i = 0; i < rows; ++i) {
delete[] TheMatrix[i];
}
delete[] TheMatrix;
}
最后,我调整了指针,并在汇编模式下摆脱了异常,但是最后我突然发现了一系列的零...
,我想将Matrix转换为数组
鉴于您在问题中所说的话,无需将二维数组转换为一维数组,如果该二维数组为:
- “传统” C ++数组(即
int matrix[10][10];
)或 - 从
T**
以连续方式构建的二维数组。
以上两项均具有相同的属性,即数据位于连续内存中,与一维数组没什么不同。因此,二维数组已经是一维数组。
就连续性而言,以上第1项和第2项之间的唯一区别是,第1项是“预先构建的”,而第2项则要求您构建二维数组。
对于项目2)。答案here显示了一个完整的类,该类以连续的方式从任何T**
构建矩阵。构建二维数组的方式不会在连续内存中创建数据,因为每一行都是单独分配的。
一旦有了矩阵,以一维方式使用它的方式就是将项目的地址传递给需要指向一维数组的指针的任何函数或实体。
这里是一个示例(对于int**
,我们将在另一个链接的答案中使用该代码)。
#include <iostream>
#include <exception>
#include <numeric>
template <typename T>
T** create2DArray(unsigned nrows,unsigned ncols,const T& val = T())
{
if (nrows == 0)
throw std::invalid_argument("number of rows is 0");
if (ncols == 0)
throw std::invalid_argument("number of columns is 0");
T** ptr = nullptr;
T* pool = nullptr;
try
{
ptr = new T*[nrows]; // allocate pointers (can throw here)
pool = new T[nrows*ncols]{val}; // allocate pool (can throw here)
// now point the row pointers to the appropriate positions in
// the memory pool
for (unsigned i = 0; i < nrows; ++i,pool += ncols )
ptr[i] = pool;
// Done.
return ptr;
}
catch (std::bad_alloc& ex)
{
delete [] ptr; // either this is nullptr or it was allocated
throw ex; // memory allocation error
}
}
template <typename T>
void delete2DArray(T** arr)
{
delete [] arr[0]; // remove the pool
delete [] arr; // remove the pointers
}
// The MatrixToArray function takes a pointer to a buffer and prints the info
void MatrixToArray(int data[],int numItems)
{
for (int i = 0; i < numItems; ++i)
std::cout << data[i] << " ";
std::cout << "\n";
}
int main()
{
int test1[] = {1,2,3,4,5,6,7,8,9,10}; // a 1-dimensioanl array
int test2[2][5] = {{1,5},{6,10}}; // a 2-dimensional array
int **test3 = create2DArray<int>(2,5); // a dynamically created 2-d array
// quickly fill the above with the same data as the other arrays
std::iota(&test3[0][0],&test3[1][5],1);
// Now test each one to prove that the same MatrixToArray function works for
// 1-dimensional and 2-dimensional arrays.
std::cout << "Output 1:\n";
MatrixToArray(test1,10);
std::cout << "\nOutput 2:\n";
MatrixToArray(&test2[0][0],10);
std::cout << "\nOutput 3:\n";
MatrixToArray(&test3[0][0],10);
delete2DArray(test3);
}
输出:
Output 1:
1 2 3 4 5 6 7 8 9 10
Output 2:
1 2 3 4 5 6 7 8 9 10
Output 3:
1 2 3 4 5 6 7 8 9 10
MatrixToArray
函数会产生相同的输出,而不管该数组是一维数组,还是二维传统数组,还是动态构建的二维数组。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。