Mandelbrot Set

Hi. I'm trying to create a Mandelbrot Set generator in C++, but I'm stuck!
When I execute the program, I found this error: "Segmentation fault".

Here is the code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#include <iostream>
using std::cout;
using std::endl;
using std::cerr;
using std::ios;

#include<cmath>
using std::pow;

#include<vector>
using std::vector;

#include <fstream>
using std::ofstream;

#include <complex>
using std::complex;

#include<cstdlib>
using std::exit;
using std::atoi;

bool** createPBM (int imageW, int imageH)
{
	bool** image = new bool*[imageH];
	
	for (int i = 0; i < imageH; i++) 
	{
		image[i] = new bool[imageW];
	}
	
	return image;
}

void mandelbrot (int imageW, int imageH, bool** image, int iterations)
{
	double const Rmin = 2, Rmax = 1;
	double const Imin = -1.2, Imax = 1.2;
	double const scaleR = (Rmax - Rmin) / (imageW - 1), scaleI = (Imax - Imin) / (imageH - 1);
	
	complex<double> Z (0, 0);
	complex<double> Zn (0, 0);
	complex<double> C (0, 0);
	
	for (int y = 0; y < imageH; y++) 
	{
		complex<double> C (C.real(), Imax - y * scaleI);
		
		for (int x = 0; x < imageW; x++) 
		{
			complex<double> C (Rmin + x * scaleR, C.imag());
			complex<double> Z (C.real(), C.imag());
			
			for (int n = 0; n < iterations; n++) 
			{
				complex<double> Zn (pow(Z.real(), 2), pow(Z.imag(), 2));
				
				if (Zn.real() + Zn.imag() > 4) 
				{
					image[x][y] = false;
					break;
				}
				
				complex<double> Z (Zn.real() - Zn.imag() + C.real(), 2 * Z.real() * Z.imag() + C.imag());
			}
		}
	}
}

void printPBM (int imageW, int imageH,bool** image, ofstream& oFile)
{
	for (int i = 0; i < imageW; i++) 
	{
		for (int j = 0; j < imageH; j++) 
		{
			if (image[i][j]) 
			{
				oFile << 1 << ' ';
			}
			else 
			{
				oFile << 0 << ' '; 
			}

		}
	}
}

int main (/*int argc, const char* argv[]*/) 
{
	ofstream oFile("mandelbrot.txt", ios::out);
	
	if (! oFile.is_open()) 
	{
		cerr << "File could not be opened." << endl;
		exit(1);
	}
	/*
	if (argc != 4) 
	{
		cout << "Invalid arguments." << endl;
		exit(1);
	}
	
	int arg[4] = {argc, atoi(argv[1]), atoi(argv[2]), atoi(argv[3])};
	
	bool** image = createPBM(arg[1], arg[2]);
	
	mandelbrot(arg[1], arg[2], image, arg[3]);
	
	printPBM(arg[1], arg[2], image, oFile);
	*/
	
	bool** image = createPBM(200, 120);
	mandelbrot(200, 120, image, 10);
	printPBM(200, 120, image, oFile);
	
	system("convert mandelbrot.txt mandelbrot.png");
}
There are two things that I've noticed right away.

1. You shouldn't have any namespace usings before header files.
2. Your createPBM method uses the imageW as the first index and printPBM using imageH for the first index.

EDIT:
3. delete[] that memory!
Last edited on
I don't understand what you mean at point 1.

You are right: i use the imageW as the first index in createPBM and imageH for the first index in printPBM. But this can create some problem? I don't think so. It changes only the way I slide "over" the array, or not?

I created this method to delete the memory:
1
2
3
4
5
6
7
8
9
void deletePBM(int imageW, int imageH, bool** image)
{
	for (int i = 0; i < imageH; i++) 
	{
		delete [] image[i];
	}
	
	delete [] image;
}


The "segmentation fault" error is still here.
You are right: i use the imageW as the first index in createPBM and imageH for the first index in printPBM. But this can create some problem? I don't think so. It changes only the way I slide "over" the array, or not?


Is your 2D array square?

...or is it more like this:
000
000
000
000

...or this:
0000
0000
0000


Both have 4x3 or 3x4 elements, but they do not have the same "footprint". Imagion them on top of one another, one is allocated and the other is what is being displayed.
Last edited on
Ooops! You are right! xD.
I changed the indexes and now, instead obtaining the "segmentation fault" error I obtain this error:

convert: delegate library support not built-in `(null)' (Freetype) @ warning/annotate.c/RenderFreetype/1449.

What is it?
Any helps?
No idea. Repost the code and copy/paste the compiler output so we can take a look.
I discoverde the error! The "convert: delegate library support not built-in `(null)' (Freetype) @ warning/annotate.c/RenderFreetype/1449" was an error of the convert command (the bitmap header wasn't right!).
Topic archived. No new replies allowed.