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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
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 user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

764 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