Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8524865
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T07:48:30+00:00 2026-06-11T07:48:30+00:00

My program seems to always produce ridiculous errors. Please provide directions for me. The

  • 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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T07:48:32+00:00Added an answer on June 11, 2026 at 7:48 am
    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been attempting to debug my program, but it always seems to get
I am basically doing a tutorial but my program seems to have errors that
I coded the code below. I've used it in my program and it seems
Ok, so, im having trouble with this program im writing, it all seems to
I'm working on a socket program and everything seems fine when compiling. First I
Ant seems to be pretty bad as running interactive program. So for instance, a
I have a program in C using Solaris with VERY ancient compatibility it seems.
I need to understand the working of this particular program, It seems to be
How ASP.NET compiles its assemblies generally confuses me. It seems that I cannot program
In my Maven project, I have the following code in the main method: FileInputStream

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.