Access violation reading location error
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: Access violation reading location error

  1. #1
    Join Date
    Jul 2010
    Posts
    3

    Access violation reading location error

    Hey guys,

    I am writing this code that is basically supposed to implement a 4D array, with a variable 4th Dimension based on the space coordinate of the element. So, for example if you are at coordinate (1,2,3) you might have 4 values, and if you are at (2,3,4) you would have 8 values. If I run the code this way, then I will get an Access violation reading location error. If I am to move the variable declarations outside the class, then the program works. I don't like the idea of those variables to be declared globally as I need to have them as part of the class. Any ideas how I can fix the error?

    <code>

    #include <cstdlib>
    #include <conio.h>
    #include <iostream>


    /*
    Project Name: oneDundistributed.dev
    File Name: main.cpp
    Copyright:
    Author: Alex Blidaru
    Date: 05/07/10 15:50
    Description:
    */

    using namespace std;


    class MPIVDOFArray {

    public:
    double **** data;
    int *cDOF;
    int Nx, Ny, Nz;

    MPIVDOFArray(int Nx2, int Ny2, int Nz2, int *&DOFArr) {
    int Nx = Nx2, Ny = Ny2, Nz = Nz2,i,j,k,z;
    cDOF = new int[Nx * Ny * Nz + 1];
    data = new double ***[Nx];

    cDOF[0] = 0;
    for (i = 0; i < Nx * Ny * Nz; i++)
    cDOF[i + 1] = cDOF[i] + DOFArr[i];
    delete [] DOFArr;
    DOFArr=NULL;

    for (i = 0; i < Nx; i++) {
    data[i] = new double **[Ny];
    for (j = 0; j < Ny; j++) {
    data[i][j] = new double *[Nz];
    for (z = 0; z < Nz; z++) {
    cout<<*LinearIndex(i,j,z);
    data[i][j][z] = new double[*GetDOF(i,j,z)];
    for(k=0; k<*GetDOF(i,j,z); k++){
    data[i][j][z][k] = rand() % 7 + 1;
    cout << "data[i][j][z][k]=" << data[i][j][z][k]<<"\n";
    //cout << "DOFArr[LinearIndex(i, j, z)]" <<DOFArr[i + Nx * j + Ny * Nx * z]<<"\n\n\n";
    }
    }
    }
    }
    }
    //
    int *LinearIndex(int i, int j, int z) {
    int blah1=(i + Nx * j + Ny * Nx * z);
    int *blah=&blah1;
    return blah;
    }

    int *GetDOF(int i, int j, int z) {

    int ind = cDOF[*LinearIndex(i,j,z)] - cDOF[*LinearIndex(i,j,z)];
    int *blah= &ind;
    return blah;
    }

    void print(int Nx, int ny, int Nz) {
    for (int i = 0; i < Nx; i++)
    for (int j = 0; j < Ny; j++)
    for (int z = 0; z < Nx; z++)
    for (int k = 0; k < *GetDOF(i, j, k); k++)
    cout << data[i][j][z][k] << " ";
    }
    };

    //------------------------------------------------------------------------------
    void setDOFArr(int *DOFArr, int Nx, int Ny, int Nz) {
    for (int i = 0; i < Nx * Ny * Nz; i++)
    DOFArr[i] = rand() % 3 + 1;
    }

    int main(int argc, char *argv[]) {


    int Nx, Ny, Nz;
    Nx = Nz = Ny = 3;

    int *DOF = new int[Nx * Ny * Nz];
    //setDOFArr(DOF, Nx, Ny, Nz);
    for (int i = 0; i < Nx * Ny * Nz; i++)
    DOF[i] = rand() % 3 + 1;

    MPIVDOFArray a(Nx, Ny, Nz, DOF);

    delete [] DOF;
    DOF=NULL;

    a.print(Nx, Ny, Nz);

    int c;
    cin>>c;
    }

    </code>

    Cheers,
    Alex

  2. #2
    Join Date
    Apr 1999
    Posts
    27,426

    Re: Access violation reading location error

    Quote Originally Posted by alexsb92 View Post
    I am writing this code that is basically supposed to implement a 4D array,
    Next time, please use code tags properly. The code you posted is almost impossible to read.
    If I run the code this way, then I will get an Access violation reading location error. If I am to move the variable declarations outside the class, then the program works.
    Moving variables around means that you still have a problem, even though you say it "works". I can bet it has memory leaks galore with all of that memory allocation going on. C++ programs in this day and age of the language should not look like what you posted, i.e. practically a low-level 'C' program -- instead, please read below.
    I don't like the idea of those variables to be declared globally as I need to have them as part of the class. Any ideas how I can fix the error?
    Learn to use vectors, instead of fighting with dynamically allocated memory.
    Code:
    #include <vector>
    
    typedef std::vector<double> DoubleArray;
    typedef std::vector<DoubleArray> Double2DArray;
    typedef std::vector<Double2DArray> Double3DArray;
    typedef std::vector<Double3DArray> Double4DArray;
    
    class MPIVDOFArray 
    {
        public: 
    	Double4DArray data;
    //...
    Then you don't need all of that code allocating, deallocating, etc. You also will not leak memory using proper, safer constructs.

    Secondly,
    Last edited by Paul McKenzie; July 14th, 2010 at 10:06 AM.

  3. #3
    Join Date
    Apr 1999
    Posts
    27,426

    Re: Access violation reading location error

    And the entire constructor now looks like this:
    Code:
    #include <vector>
    //.. stuff from the previous post
    typedef std::vector<int> IntArray;
    //...
    class MPIVDOFArray 
    {
       public: 
           int Nx, Ny, Nz;
           Double4DArray data;
           IntArray cDOF;
    
           MPIVDOFArray(int Nx2, int Ny2, int Nz2, int *&DOFArr) : 
               data(Nx2, Double3DArray(Ny2, Double2DArray(Nz2, DoubleArray(*GetDOF(i,j,z))))), cDOF(Nx*Ny*Nz+1, 0),  Nx(Nx2), Ny(Ny2), Nz(Nz2)
    
        {  }
    };
    I didn't check the entire syntax, but that is what basically that entire constructor would look like. There are no memory allocation calls at all. Also note the usage of the initialization list that initializes the variables, and the empty constructor body. What you see above is basically everything your old code did.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; July 14th, 2010 at 10:18 AM.

  4. #4
    Join Date
    Jul 2010
    Posts
    3

    Re: Access violation reading location error

    Hello Paul,


    Thanks a lot for your help. I am very sorry about my code, except that I don't have any form editing buttons when I write a post, so I thought simply including the code tags would work. I forgot however that those were not the proper tags. Really sorry about it.

    I looked over a few webpages for vectors, but I still have one question. With the code you gave me, It simply makes a 4D vector with one of the Dimensions equal to GetDOF(i,j,z). The problem is that in this code, i,j,z are not changing so the value returned by GetDOF will always be the same. In my initial code, the size of the 4th dimension would change based on which element in 3D I would be at.

    Is there a way I could accomplish this with vectors (it is indeed a lot cleaner, and from what i read, easier to use too)? If your code already does this, and I just misunderstood it, could you explain me this part:
    Code:
    MPIVDOFArray(int Nx2, int Ny2, int Nz2, int *&DOFArr) : 
    	data(Nx2, Double3DArray(Ny2, Double2DArray(Nz2, DoubleArray(*GetDOF(i,j,z))))), cDOF(Nx*Ny*Nz+1, 0),  Nx(Nx2), Ny(Ny2), Nz(Nz2)
    
    	{  }

  5. #5
    Join Date
    Apr 1999
    Posts
    27,426

    Re: Access violation reading location error

    Look at the vector::resize() function. To resize element [i][j][k][z], so that [z] is 100 items.

    Code:
    data[i][j][k].resize(100);
    Note that the 4D vector is actually vectors within vectors, nested to 4 levels, so data[i][j][k] is that 1 dimensional vector at element [i][j][k].

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; July 14th, 2010 at 01:33 PM.

  6. #6
    Join Date
    Jul 2010
    Posts
    3

    Re: Access violation reading location error

    Wow... That is so much easier... Still proves there is a lot that I still have to learn in C++. University should be a lot of fun then (just finished grade 12). Again, thank you very much Paul.

    Cheers,
    Alex

  7. #7
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,712

    Re: Access violation reading location error

    Here's a good place to start to look up more C++ stuff that makes life a lot easier.

    http://www.cppreference.com/wiki/

    In fact it would be a poor C++ programmer that didn't have at least a working knowledge of the STL
    "It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
    Richard P. Feynman

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center