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

exception thrown: write access violation
access violation writing location c++
0x1110112
#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.

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.

c++, Definitely this will result in a segmentation fault or memory access violation using namespace std; int **dynamicArray ; int ROWS, COLUMNS;  Error 1053: The service did not respond to the start or control request in a timely fashion. The test server is running on Windows server 2012. This is my service start and stop methods: public void Start () { _logProvider.Info ("Service started.");

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'.

Exception thrown: read access violation. this was nullptr. · Issue , Above code when running notepad++, the error appeared in the Buffer* getCurrentBuffer() which shown Exception thrown: read access violation. An access violation that occurs in unsafe managed code can be expressed as either a NullReferenceException exception or an AccessViolationException exception, depending on the platform. An access violation in unmanaged code that bubbles up to managed code is always wrapped in an AccessViolationException exception.

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.

How to order the column value in sql? [duplicate], Exception thrown: read access violation. **dynamicArray** was 0x1118235. occurred Checking for valid enum types from protobufs · Request Access Token in Postman for Azure Function App protected by Azure AD B2C  It is legal to access this value, it's just not specified what its value will be. If it were a pointer, it would be a pointer to an unspecified memory address, so dereferencing it would be illegal, but firstgid is not a pointer, it's an int .

Exception thrown: read access violation - C++ Forum, Exception thrown: read access violation. Pages: 12. May 2, 2016 at 6:31am. May 2, 2016 at 1:31pm UTC. BonsaiSuperstar (51). So, I'm having a hell of a time  opencv is a c++ library, while your attempt at doing this is C, and it won't work. please AVOID using pointers to cv::Mat, it's already a refcounted smartpointer on it's own. also malloc will only allocate memory, not construct objects (and you also only allocated the outer array, not the inner ones).

Debug a C++ access violation, 02/05/2019; 2 minutes to read If you get an access violation on a line of code that dereferences multiple pointers, it can be difficult Starting in Visual Studio 2015 Update 1, the exception dialog box now explicitly names the  the Charts access violation is thrown. 3) [TV52010983] Fixed bug : SetNull doesn't work properly for FastLine Series. Setting the IgnoreNulls property of FastLineSeries to false in conjunction to TreatNulls property works fine now (Series1.IgnoreNulls:=false; Series1.TreatNulls:=tnDontPaint). 4) [TV52010079] Crossover funtion: if there is a crossover

std::~list() Exception thrown: read access violation. **_Pnode** was , Exception thrown: read access violation. **_Pnode** was 0xFFFFFFFFFFFFFFFF. occurred list line 1390 for (_Nodeptr _Pnext; _Pnode  If this happens, a new exception can be thrown before the catch-clause for an existing exception is reached, which will cause a call to terminate( ). If you call any functions inside a destructor that can throw exceptions, those calls should be within a try block in the destructor, and the destructor must handle all exceptions itself.

Comments
  • You allocate and then immediately free the array in input_matrix. Once you've free'd the array you should not touch/use it. But then at the end of that function you use the array which has been free'd.
  • You should free the memory of dynamicArray in main, after you are done with it.
  • You have a bunch of other minor issues: Why not use vector? Don't use global variables. using namespace std is considered bad. In print_matrix m will never equal COLUMNS.
  • The only reason I can come up with why the error is not obvious to you is that you do not realize what "freeing the memory" does -- you even have a comment in your code saying "free the allocated memory". Yes, you populated the matrix, but that doesn't mean "now I don't need the memory anymore, since the matrix is populated and is now holding my values". If I'm wrong, so be it, but again, that's the only plausible reason why you would believe that deallocating the memory before actually using the matrix would work.
  • Thanks a lot, you are great.
  • This should be a comment not an answer.
  • I think it's a perfect answer. OP is even asking 'What should I do?'
  • Still a suggestion, since we don't know if he is allowed to use std::vector or 3rd party libraries.
  • Yes we do, since the question was tagged c++.
  • I'm sure it's good advice, but it's a bit short as an explanation of what was wrong. (Actually, the question is better than many - it actually compiles if you remove the local include. Admittedly, it just blocks waiting for input, but you can't have everything!). This looks more like a good comment than an answer to me.