0

My C++ code (shown below) works on this site: GDB Online but not in Visual Studio, where it crashes at

iterations[imag_times][real_times] = i % (iter / 2);

when imag_times is 1 and real_times is 0 with the exception being Exception has occurred. Segmentation fault

I have installed GDB version 7.6.1.

My Question: Does anybody know how to fix that and why this is happening?

#include <iostream>
using namespace std;

int main() 
{
    // initialization
    const double real_min = -1;
    const double real_max = 1;
    const double imag_min = -1;
    const double imag_max = 1;

    const int iter = 30;
    const double real_offs = 0.01;
    const double imag_offs = 0.01;

    double z_real = 0;
    double z_imag = 0;
    double c_real = real_min;
    double c_imag = imag_max;

    int real_times = 0;
    int imag_times = 0;
    int** iterations = new int*[1];
    iterations[0] = new int;
    int i = 0;

    // start
    while(c_imag >= imag_min)
    {
        iterations = (int**)realloc(iterations, sizeof(int*) * (imag_times + 1));

        real_times = 0;
        c_real = real_min;

        while(c_real <= real_max)
        {
            iterations[imag_times] = (int*)realloc(iterations[imag_times], sizeof(int) * (real_times + 1));

            z_real = 0;
            z_imag = 0;
            for(i = 0; i < iter; i++)
            {
                double z_imag2 = z_imag * z_imag;
                z_imag = 2 * z_real * z_imag + c_imag;
                z_real = z_real * z_real - z_imag2 + c_real;

                if(z_real * z_real + z_imag * z_imag > 4)
                {
                    break;
                }
            }

            iterations[imag_times][real_times] = i % (iter / 2);

            real_times++;
            c_real = real_min + real_offs *  real_times;
        }

        imag_times++;
        c_imag = imag_max - imag_offs * imag_times;
    }

    // output
    for(int i = 0; i < imag_times; i++)
    {
        for(int j = 0; j < real_times; j++)
        {
            cout << iterations[i][j];
            cout << ",";
        }
        cout << "\n";
    }

    cout << "done";
    std::cin.get(); // pause so the program doesnt exit instantly
    return 0;
}

Thanks in advance!

rphii
  • 209
  • 2
  • 13
  • 7
    You cannot mix `new` and `realloc`. Consider a `vector` instead. – ChrisMM Feb 21 '20 at 18:40
  • 3
    I recommend getting rid of all the `new` and `realloc`. Should not need them when you have `std::vector` and `std::unique_ptr`. – Eljay Feb 21 '20 at 18:42
  • 1
    `realloc(iterations[imag_times], ...` doesn't work because `iterations[imag_times]` is potentially uninitialized (realloc won't zero out new memory). – 1201ProgramAlarm Feb 21 '20 at 18:42
  • 1
    Once you've converted from `int **` and `realloc` to `std::vector` (I see no need for `unique_ptr` here), you might also want to consider using `std::complex`. For what it's worth: https://stackoverflow.com/a/30071268/179910 – Jerry Coffin Feb 21 '20 at 18:49
  • oh thanks, I'll try out all these tips, because I'm pretty new to C++. @1201ProgramAlarm yes that is highly the case, I have had the feeling that I know this scenario from somewhere... – rphii Feb 21 '20 at 18:54
  • @rphii [Same code using vector](http://coliru.stacked-crooked.com/a/0c02f17da80de89f) – PaulMcKenzie Feb 21 '20 at 18:59
  • @rphii `[...]because I'm pretty new to C++[...]` if the code you have shown is the outcome from a book, tutorial or course about c++ that you use to learn, you then should consider to not continue with that book/tutorial/course, but instead use a good book about c++ [The Definitive C++ Book Guide and List](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) – t.niese Feb 21 '20 at 19:37
  • What @1201ProgramAlarm said, running a debug compile of this code under Visual Studio points this out, because the debug runtime library initializes allocated memory to 0xCD which you can see when it crashes. – James Feb 21 '20 at 19:42

0 Answers0