Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


Error when testing the value in 2Dim array

Posted on 2003-02-27
Medium Priority
Last Modified: 2010-04-01
In a class header file for Dohickey I have a private class data member declared as:
     int** Array;

I get a runtime error that says “unhandled exception in….Access Violation” when I try to fill the array through the use of a public member function in my implementation file (.cpp) holding all my class member functions:
     void Dohicky::setArray(int d1, int d2)
          if (Array[d1][d2] = = 0)

I had initialized Array in my constructor by saying :
     Array = NULL;
Could this have caused the error...perhaps?  Would it have made a difference if I had initialize Array by saying :
     for(int y = 0; y < num; y++)
          for(int x = 0; x < num; x++)
               Array[x][y] = 0;

I’m obviously not too good with Arrays and pointers, but I’m getting there (albeit slowly).  My program fails only during runtime.  Is it possible to know why this might be happening from what I've provided here in this post?  If needed, let me know whether I have not given enough information to allow an answer.

Thanks to anyone who can point me in the right direction.
Question by:Invariant

Accepted Solution

constructor earned 150 total points
ID: 8032287
You cannot dereference Array before you have ensured that it points to a valid piece of memory!

If num is a constant then declare
     int Array[num][num];
Otherwise (if num is handed to the constructor and therefore not known at compile-time) then use your current declaration
     int **Array;
and allocate the memory yourself using the new [] operator. Remember to clean up in the destructor using delete [] operator.

And yes, if you want to set everything to zero, you should either loop over the entire memory or use memset. Don't set the pointer to NULL.

Expert Comment

ID: 8032948
don't use dynamic arrays.  dynamic arrays are considered bad c++ and cause all sorts of troubles, just like the one you encountered.  try to use vector of vectors instead as in std::vector< std::vector<int> >.  std::vector not only manages its own memory but is also the container suggested as the default one by the c++ standard itself

Author Comment

ID: 8042682
That worked fine for me now.  Thanks for your help constructor and burcarpat.  I need to do more work on my pointers and arrays so I can understand them better.  It's good to have your great help.

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
Suggested Courses

571 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question