Exception thrown: read access violation. **dynamicArray** was 0x1118235. occurred

别说谁变了你拦得住时间么 提交于 2019-12-08 15:33:37

问题


#include "pch.h"
#include <iostream>
#include <string>
using namespace std;
int **dynamicArray ;
int ROWS, COLUMNS;

//---------------------------------
int input_matrix(int ROWS, int COLUMNS)
{

    //---------------------------------------
    //memory allocated for elements of rows.
    int **dynamicArray = new int *[ROWS];

    //memory allocated for  elements of each column.
    for (int i = 0; i < ROWS; i++)
        dynamicArray[i] = new int [COLUMNS];

    //free the allocated memory
    for (int i = 0; i < ROWS; i++)
        delete[] dynamicArray[i];
    delete[] dynamicArray;
    //-------------------------------------

    for (int i = 0; i < ROWS; i++)
    {
        for (int j = 0; j < COLUMNS; j++)
        {
            cin >> dynamicArray[i][j];
        }
    }
    return 0;
}
//---------------------------------------------
int print_matrix(int **Array)
{
    for (int k = 0; k < ROWS; k++)
    {
        for (int m = 0; m < COLUMNS; m++)
        {
            cout << Array[k][m];
            if (m == COLUMNS)
            {
                cout << "\n";
            }
        }
    }

    return 0;

}

//---------------------------------
int main()
{
    cin >> ROWS;
    cin >> COLUMNS;
    input_matrix(ROWS, COLUMNS);
    print_matrix(dynamicArray);

}

This code defines a matrix and get inputs and puts them in the members of the matrix but Every time I run this code I get read access violation error on the line:

cin >> dynamicArray[i][j];

here are the full details: Exception thrown: read access violation. dynamicArray was 0x1118235. occurred

What should I do?

Thank you in advance.


回答1:


There are multiple issues with your program. Let me list all of them one by one.

  1. As mentioned in one of the comments, You are immediately deallocating memory just after you allocated it. Definitely this will result in a segmentation fault or memory access violation when you access deallocated memory.
  2. When you allocate the memory you are not assigning the allocated memory pointers to global pointer dynamicArray instead you are creating a local variable with the same name inside the function input_matrix. As this pointer variable scope ends inside the function you are losing the memory allocated. Hence again you will face segmentation fault or memory access violation inside print_matrix function.
  3. Inside print_matrix function in inner for loop you are checking if m==COLUMNS to print new line, this will never happen since m is always less than COLUMNS.
  4. Finally, as the previous answer suggests when you are using C++, using a vector with smart pointers is a better choice than using array and raw pointers for better memory management.

Following snippet resolves those issues.

#include <iostream>
#include <string>
using namespace std;
int **dynamicArray ;
int ROWS, COLUMNS;

//---------------------------------
int input_matrix(int ROWS, int COLUMNS)
{
    //---------------------------------------
    //memory allocated for elements of rows.
    dynamicArray = new int *[ROWS];

    //memory allocated for  elements of each column.
    for (int i = 0; i < ROWS; i++)
        dynamicArray[i] = new int [COLUMNS];

//    cout<<"Input array values\n";

    for (int i = 0; i < ROWS; i++)
    {
        for (int j = 0; j < COLUMNS; j++)
        {
            cin>>dynamicArray[i][j];
        }
    }
    return 0;
}

void free_matrix_memory()
{
    cout<<"freeing allocated memory\n";
    //free the allocated memory
    for (int i = 0; i < ROWS; i++)
        delete[] dynamicArray[i];
    delete[] dynamicArray;
    //-------------------------------------
}

//---------------------------------------------
int print_matrix(int **Array)
{
    cout<<"printing matrix\n";
    for (int k = 0; k < ROWS; k++)
    {
        for (int m = 0; m < COLUMNS; m++)
            cout << Array[k][m];
        cout << "\n";
    }
    return 0;
}

//---------------------------------
int main()
{
    cout<<"Row and column values\n";
    cin>> ROWS;
    cin>> COLUMNS;
    input_matrix(ROWS, COLUMNS);
    print_matrix(dynamicArray);
    free_matrix_memory();
}

Still many improvements can be done for your such as avoiding global variables etc., I am leaving it up to you to do those improvements.




回答2:


There is no reason to hand-roll your memory-management in this case. Use std::vector (which is a dynamic array) instead or even an actual Matrix library for example 'Eigen'.




回答3:


If you run the program under Valgrind, it tells you exactly what's wrong:

==6939== Invalid read of size 8
==6939==    at 0x1092C9: input_matrix(int, int) (53083248.cpp:30)
==6939==    by 0x1093FA: main (53083248.cpp:59)
==6939==  Address 0x4d7ecc0 is 0 bytes inside a block of size 16 free'd
==6939==    at 0x48373EB: operator delete[](void*) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==6939==    by 0x109296: input_matrix(int, int) (53083248.cpp:23)
==6939==    by 0x1093FA: main (53083248.cpp:59)
==6939==  Block was alloc'd at
==6939==    at 0x483654F: operator new[](unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==6939==    by 0x1091D3: input_matrix(int, int) (53083248.cpp:14)
==6939==    by 0x1093FA: main (53083248.cpp:59)
==6939== 
==6939== Invalid write of size 4
==6939==    at 0x496FFF0: std::istream::operator>>(int&) (istream.tcc:194)
==6939==    by 0x1092EA: input_matrix(int, int) (53083248.cpp:30)
==6939==    by 0x1093FA: main (53083248.cpp:59)
==6939==  Address 0x4d7ed10 is 0 bytes inside a block of size 8 free'd
==6939==    at 0x48373EB: operator delete[](void*) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==6939==    by 0x10927D: input_matrix(int, int) (53083248.cpp:22)
==6939==    by 0x1093FA: main (53083248.cpp:59)
==6939==  Block was alloc'd at
==6939==    at 0x483654F: operator new[](unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==6939==    by 0x10922A: input_matrix(int, int) (53083248.cpp:18)
==6939==    by 0x1093FA: main (53083248.cpp:59)
==6939== 
==6939== Invalid read of size 8
==6939==    at 0x10934D: print_matrix(int**) (53083248.cpp:42)
==6939==    by 0x10940C: main (53083248.cpp:60)
==6939==  Address 0x0 is not stack'd, malloc'd or (recently) free'd
==6939== 
==6939== 
==6939== Process terminating with default action of signal 11 (SIGSEGV)
==6939==  Access not within mapped region at address 0x0
==6939==    at 0x10934D: print_matrix(int**) (53083248.cpp:42)
==6939==    by 0x10940C: main (53083248.cpp:60)

There's both read and write to memory that's been freed, and a dereference of dynamicArray that was never assigned.

To fix these, you'll need to ensure that your memory lifetimes are appropriate for when you use them, and avoid shadowing dynamicArray within input_matrix().

I advise you to avoid raw new[] and delete[] - prefer to use containers and smart pointers that own resources for you, and automatically release it from their destructors.



来源:https://stackoverflow.com/questions/53083248/exception-thrown-read-access-violation-dynamicarray-was-0x1118235-occurre

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!