Home c++ segmentation error (memory stack is reset to disk) C++

segmentation error (memory stack is reset to disk) C++

Author

Date

Category

I wrote this program:

// includes
#Include & lt; iostream & gt;
#Include & lt; Chrono & gt;
#Include & lt; String & GT;
#Include & lt; Ctime & gt;
// Namespaces.
Using Namespace STD;
Using Namespace Chrono;
// Defines.
#Define Return Return 0
// Main.
INT MAIN () {
  SetLocale (LC_ALL, "RU"); // Locale
  const auto _before_ = high_resolution_clock :: now (); // Start Timer (for Time Account)
  SRAND (TIME (0));
  // Create Array.
  INT COUNTARRAYIN_ARR2D = 2;
  INT COUNTELEEMENTSIN_ARR1D = 3;
  int ** arr2d = new int * [countarrayin_arr2d];
  // Generate Array.
  For (int i = 0; i & lt; countarrayin_arr2d; i ++) {
    For (int j = 0; j & lt; countlementsin_arr1d; j ++) {
      arr2d [i] [j] = rand ()% 30 + 1;
    }
  }
  // Print Array.
  For (int i = 0; i & lt; countarrayin_arr2d; i ++) {
    For (int j = 0; j & lt; countlementsin_arr1d; j ++) {
      COUT & LT; & lt; "\ 033 [34m" & lt; & lt; arr2d [i] [j] & lt; & lt; "\ 033 [0m \ t";
    }
    COUT & LT; & lt; Endl;
  }
  / * Clean Memory * /
  For (int i = 0; i & lt; countarrayin_arr2d; i ++) {
    delete [] arr2d [i];
  }
  delete [] arr2d;
  // Time Account
  const Auto _After_ = High_Resolution_Clock :: Now ();
  Const Float Time_FOR_PROGRAM = DURATION_CAST & LT; MilliseConds & GT; (_ After _-_ Before _). Count ();
  COUT & LT; & LT; "\ N \ n Programm Completed in" & lt; & lt; time_for_program & lt; & lt; "MS" & lt; & lt; endl;
  Return;
}

And writes:

I have 2 questions.
1) What does the stack be reset to the disk? on hard disk? Will it be deleted independently? If not, how to remove?
2) How to decide and why this happened at all?


Answer 1, Authority 100%

You have a clear memory error. Call New occurs once, and call DELETE ?


Here is the highlight of the array of pointers on int :

int ** arr2d = new int * [countarrayin_arr2d];

To get a two-dimensional array, you still need to select your memory block:

for (int i = 0; i & lt; countarrayin_arr2d; i ++)
{
  arr2d [i] = New int [Countelementsin_Arr1d];
// ^^^^^^^
  For (int j = 0; j & lt; countlementsin_arr1d; j ++)
  {
    // do what you need
  }
}

Answer 2, Authority 100%

“Stack of memory dropped to disk” – this [very free] translate phrase “Core dumped “. On the disk in the current directory, a file named “Core” is created. He himself will not be deleted, but he is the most ordinary file that you can delete when you want.

Core file is also in itself the shortest way to answer the question number 2. If your binary is assembled with the compiler option “-g”, then you can start the debugger with the command line in the form “GDB -C Core Main.exe” . The debugger will automatically appear on the line that caused segmentation fault. About working in GDB I recommend to read his documentation, it is extensive and exhaustive.


Answer 3, Authority 50%

people, you forgot to declare elements of arrays in the array)

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions