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 8406299
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T23:04:24+00:00 2026-06-09T23:04:24+00:00

For some reason, the following program crashes before i got here is printed. When

  • 0

For some reason, the following program crashes before “i got here” is printed. When I comment out the try-catch part, the program runs and exits normally.

#include <iostream>

int error_function () {

    throw 5;

    return 0;
}


int main () {

    double* b = new double[6];

    for (int i = 0; i < 6; i++) {
        b[i] = i;
    }

    double* c = new double(*b);

    for (int i = 0; i < 6; i++) {
        c[i] = i+1;
    }

    for (int i = 0; i < 6; i++) {
        std::cout << b[i] << " " << c[i] << std::endl;
    }

    try {
        error_function();
    }
    catch (int t) {
        std::cout << "catched an int: " << t << std::endl;
    }

    std::cout << "i got here" << std::endl;
    return 0;
}

This is the entire output I get when the program crashes:

0 1
1 2
2 3
3 4
4 5
5 6
catched an int: 5
*** glibc detected *** ./main: free(): invalid next size (fast): 0x0000000001f22070 ***
======= Backtrace: =========
/lib/libc.so.6(+0x77806)[0x7f2b273a0806]
/lib/libc.so.6(cfree+0x73)[0x7f2b273a70d3]
./main[0x400d92]
(a bunch of stuff)
Aborted

I have no idea why this is happening. Any help would be greatly appreciated!

  • 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-09T23:04:25+00:00Added an answer on June 9, 2026 at 11:04 pm

    It’s crashing because you’ve only allocated one double when you allocate c and then proceed to overrun the bounds by accessing elements after the first. It looks as if in this case after the exception has been handled there is some cleanup and the glibc has detected corrupt memory.

    The problem lines are these:

    double* c = new double(*b);
    
    for (int i = 0; i < 6; i++) {
            c[i] = i+1;
    }
    

    It allocates a new double that copies the value of *b (or b[0] if you like) because b is just a pointer so dereferencing it does not invoke the copy constructor that copies an array.

    You would be better off using a std::vector as it would automatically take care of any memory allocation and deallocation for you as well as being exception safe. In the case your error_function threw an exception that you did not handle it would still clean up correctly, whereas your new‘d memory does not as it stands.

    Technically it’s undefined behaviour, so anything could happen.

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

Sidebar

Related Questions

For some reason when I run the following code the program prints out JDN4
I have a following program but for some reason it is throwing an error
For some reason the following doesn't crash like my program does, but I'm pretty
For some reason the following C# Console program always outputs: 32 False wtf=0 What
I have the following shell script. For some reason the java program's standard error
For some reason my Heapsort is not working correctly. Using the following test program:
For some reason, when I execute this program with the following expression 12 12
For some reason the following code doesn't work on Windows XP. new URL(file:// +
For some reason the following form isn't submitting the file like it should and
For some reason it looks like constructor delegation doesn't work in the following snippet:

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.