1

I'd like to use an array (very large) of pointer in a class:

char* myClass::_myArray[1200000];   // 1.2 million elements

If I new it in my constructor:

myClass::myClass()
{
   for (int n = 0; n < 1200000; n++)
   {
      _myArray[n] = new char[32];          
   }
}

then in my destructor, I dispose it:

myClass::~myClass()
{
   for (int n = 0; n < 1200000; n++)
   {
      if (NULL != _myArray[n])
      {
          delete[] _myArray[n];  // exception throws here
          _myArray[n] = NULL;
      }
   }
}

it always throw exception like following picture:

enter image description here

I have no idea why. I am wondering if I would like to pre-allocate a buffer for the class object until it quits, how should I do this? This buffer should work like a global variable, that can be used any time the function/methods within the class is called.

Thanks a lot. Appreciate any opinion and education.

Additional information:

I set the array size 1000 (char* myClass::_myArray[1000]; ), it works fine. When I set it to 5000, it throws exception like this after the app exit. When it is 1.2 million, the exception throws when app starts.

the delete[] _myArray[n] is where the exception happens, but it happens at random n number.

22
  • What is the value of n when the assertions fails? Commented Sep 11, 2014 at 21:07
  • Note that deleting a null pointer is safe to do in C++ - you do not have to check if the pointer is null before deleting it. But of course you should still set the deleted pointer to null afterwards. Commented Sep 11, 2014 at 21:09
  • It was strange. Initially it seems like random number. Then after I did some change it seems at 0; Commented Sep 11, 2014 at 21:09
  • Show as all your code. Commented Sep 11, 2014 at 21:10
  • 1
    @Kaiged I think you are right. It is probably somewhere else. It is just showing it is happening at the delete. Thanks! Commented Sep 11, 2014 at 22:24

2 Answers 2

4
 if (NULL != _myArray[n])
  {
      delete[] _myArray[n];
      _myArray = NULL;
  }

You do not want to set _myArray to NULL inside the loop. In fact, you never want to set _myArray to NULL. If _myArray is actually defined as you show here (instead of as char** myClass::_myArray = new char*[1200000];) then setting it to NULL should be a compiler error.

Sign up to request clarification or add additional context in comments.

5 Comments

I am sorry, I re-edit the thread. I actually set _myArray[n] = NULL;
It seems this answer still hints at the problem even with the edit. _myArray[n] is only a char, not a char*. _myArray should be a char**
_myArray[n] is a char*
@Ono oh so it is. my mistake
@Kaiged no problem I really appreciate you are trying to help.
2

You're calling delete[] on a single pointer instead of just delete:

for (int n = 0; n < 1200000; n++)
{
  if (NULL != _myArray[n])
  {
      delete _myArray[n];
      _myArray[n] = NULL;
  }
}

Alternatively you could just call delete[] on the array itself:

delete[] _myArray;

This should be safe because you are using this in a destructor and _myArray is going out of scope with your object and therefore none of the pointers with now invalid objects are going to be available once this instance of the class goes away. If that is not the

See http://www.cplusplus.com/reference/new/operator%20delete[]/ for reference

5 Comments

I thought I used delete[] because each _myArray[n] is created by new[]? It is an array.
So it is, my poor reading. I'll look at a little closer and get back
@Ono I'm attempting to recreate your bug, but with the code you've provided, and implementing the simplest class definition possible(see below). I get no errors. class myClass { public: myClass(); ~myClass(); char* _myArray[1200000]; }; Can you provide more details about your code?
@Ono If your program is large and complex and posting your code is prohibitive, then if possible you should update your question with example code that compiles and reproduces your bug. If nothing else it will help you narrow down which part of your code is causing the runtime error.
Thanks I will keep looking. Like @Kai said, the problem is probably somewhere else. I truly appreciate your help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.