0

I have a piece of C++ code that generates a bunch of sorted .txt files of varying sizes. The maximum size is 1 million. After I compile this with g++ and run it, the code generates all the files but crashes with a segmentation fault. This happens for ListSize=20 or greater. Why is this happening?

#include<iostream>
#include<cmath>
#include<fstream>
#include<cstdlib>
using namespace std;
void mergeSort(int a[], int a_tmp[], int l, int r)
{
    int i, j, k, m;
    if (r > l)
      {
        m = (r+l)/2;
        mergeSort(a, a_tmp, l, m);
        mergeSort(a,a_tmp, m+1, r);
        for (i = m+1; i > l; i--){
          a_tmp[i-1] = a[i-1];
        }
        for (j = m; j < r; j++){
          a_tmp[r+m-j] = a[j+1];
        }
        for (k = l; k <= r; k++){
          a[k] = (a_tmp[i] < a_tmp[j]) ? a_tmp[i++] : a_tmp[j--];
        }
      }
  }
int main()
{
    srand(time(0));
    int listsize[22];
    int i=0;
    int j=0;
    for (i=0;i<22;i++) {
        listsize[i]=(pow((i%3),2) + 1)pow(10,floor((i/3)));
    }
    for (i=0;i<22;i++) {
        int temporary=listsize[i];
        int a[temporary];
        int a_tmp[temporary];
        char FileName[80];
        sprintf(FileName,"testSorted%d.txt",temporary);
        ofstream outfile(FileName);
        for(j = 0;j<temporary;j++){
        a[j] = (rand() +1)%10000000;
        }
     mergeSort(a,  a_tmp, 0, temporary-1);
    for (i=0;i<22;i++) {
        listsize[i]=(pow((i%3),2) + 1)pow(10,floor((i/3)));
    }
    for (i=0;i<22;i++) {
        int temporary=listsize[i];
        int a[temporary];
        int a_tmp[temporary];
        char FileName[80];
        sprintf(FileName,"testSorted%d.txt",temporary);
        ofstream outfile(FileName);
        for(j = 0;j<temporary;j++){
        a[j] = (rand() +1)%10000000;
        }
     mergeSort(a,  a_tmp, 0, temporary-1);
    outfile << temporary << endl;
    for(j = 0;j<temporary;j++)
        outfile << a[j] << endl;
    outfile.close();
    }
    return 0;
}
}
requiemman
  • 61
  • 6
  • 4
    Most likely that means there is undefined behavior in your code caused by 1 or more bugs. – drescherjm Oct 02 '22 at 23:18
  • 2
    If you want to write C++, starting using `std::vector` instead of old-style arrays. Those support bound checking which will probably tell you where your code is wrong. – Victor Eijkhout Oct 02 '22 at 23:19
  • 1
    `int a[temporary];` <-- this only [works in C](https://stackoverflow.com/questions/34975342/creating-dynamic-array-without-malloc), not C++. In C++, you need to either use the `new` keyword or use `std::vector` – smac89 Oct 02 '22 at 23:22
  • Why are you writing your own sort algorithm, anyway? – Mike Robinson Oct 02 '22 at 23:25
  • `pow((i%3),2) + 1)pow(...` is just broken code – Mikael Öhman Oct 02 '22 at 23:25
  • 2
    `(pow((i%3),2) + 1)pow(10,floor((i/3)));` doesn't even compile, much less run to a segfault. You also cookie-cutter that broken code after your list-gen loop. What was wrong with the prior (albeit broken) content? What is the purpose of `floor((i/3))` ? `i` is integer; you get the floor for free via integer-division rules. This code is just all kinds of wtf. – WhozCraig Oct 02 '22 at 23:28
  • 1
    You have several arrays and access their elements using computed indices. Even worse, some indices are obtained as elements of other arrays, without checking. Odds are, one or more of the array accesses runs out of bounds. Either (1) use a debugger, and check each input manually as you step through the program or (2) modify the code so it checks EVERY index for validity BEFORE using it. Also - `int a[temporary]` where `temporary` is a variable is NOT valid C++ (it is a non-standard extension offered by some compilers, and can give strange behaviour if array size exceeds available stack). – Peter Oct 02 '22 at 23:39
  • https://en.cppreference.com/w/cpp/language/ub – Jesper Juhl Oct 03 '22 at 01:09
  • Which line triggers the seg fault? What are the values of your variables when the seg fault occurs? – JaMiT Oct 03 '22 at 03:29

0 Answers0