0

My program seems to always produce ridiculous errors. Please provide directions for me. The following code segment cutout all irrelevant parts. Thanks.

Part A of the code segment seems failed to initialize the array correctly, how to debug? Part B of the code segment always crash, is there anything i missed?


typedef unsigned long T_PSIZE;
int main()
{
   int AG_TOTAL = 6 ;
   /* part A1 */
   T_PSIZE* cntPeopleByAge = new T_PSIZE[AG_TOTAL + 1];
   /* part A2 - originally i use static array like this, but it also fails */
   //T_PSIZE cntPeopleByAge T_PSIZE[AG_TOTAL + 1];
   for (int i = 0; i < (AG_TOTAL + 1); i++)
   {
     std::cout << i << ":" << cntPeopleByAge[i] << "\t";
     cntPeopleByAge[i] = 0;
     std::cout << cntPeopleByAge[i] << "\n";
   }
   std::cout << "cntPeopleByAge:" << cntPeopleByAge[ AG_TOTAL + 1 ] << "\n";
   /* part B */
   delete [] cntPeopleByAge;
   return 0; // <---  crash here!
}

Sample Output

0:200320        0
1:201581        0
2:201582        0
3:201583        0
4:0     0
5:0     0
cntPeopleByAge:1799119387:0:0

  • Platform: win 7 x64
  • Compiler: TDM-GCC x64
1
  • Array indices are numbered starting from 0. So cntPeopleByAge[ AG_TOTAL + 1] is a non-existing element. Commented Sep 16, 2012 at 7:55

2 Answers 2

5
for (int i = 0; i < (AG_TOTAL + 1); i++)
   {
     std::cout << i << ":" << cntPeopleByAge[i] << "\t";
     //                       ^^^^^^^^^^^^^^^^
     // You're reading uninitialized memory here

     cntPeopleByAge[i] = 0;
     std::cout << cntPeopleByAge[i] << "\n";
   }

And here

std::cout << "cntPeopleByAge:" << cntPeopleByAge[ AG_TOTAL + 1 ] << "\n";

you're going out of bounds. The last valid index is AG_TOTAL.

You've got undefined behaviour (UB). The errors are only as ridiculous as UB can be.

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

2 Comments

Thanks a lot. I'm so dump for this extremely newbie question.
This is not correct! The array is declared a length and the loop limit correctly test for < length. Please see my post below.
-1

/* Sorry. but that earlier answer is not correct. The loop correctly starts at zero and ends at < the limit. The problem is that you are declaring an array of pointers, but never allocating memory to the objects that they point to. Your output is showing you addresses not numbers. One way is to allocate the objects as you use them (you must delete them individually as well) */

T_PSIZE* cntPeopleByAge = new T_PSIZE[AG_TOTAL + 1];    
for (int i = 0; i < (AG_TOTAL + 1); i++)    
{ 
cntPeopleByAge[i] = new T_PSIZE();
}

What you really want to use is the vector class in the standard library which handles all of this for you:

#include <vector>

std:vector<T_PSIZE *> cntPeopleByAge;
cntPeopleByAgex.resize(AG_TOTAL + 1);

Good luck ...

1 Comment

No, you've got this all wrong. OP is allocating an array of unsigned longs, not pointers. The loop condition is correct, sure, and I never said it isn't. The problem is that he's reading those unsigned longs uninitialized (new doesn't zero out memory or anything). The values are unspecified (but surely they're not adresses) and reading them is undefined behaviour.

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.