program crashes while generating a string pattern at higher level

Hello forum,
I am trying to generate a string pattern for the hilbert curve at level 16 and program crashes with the following output:

1
2
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc


Here goes the code snippet :

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
std::string Hilbert2DScene::hilbertWithLSystem(unsigned int level)
{
    std::string current;


    if(level < 1)
    {
        current = "";
        return current;
    }
    else
    {
        //define the initializing string
        current = "A";

        unsigned int generation = 1;

        while(generation <= level)
        {
            std::string next;


            for(std::string::iterator it = current.begin();it != current.end();it++)
            {
                switch(*it)
                {
                    case 'A':
                        next.append("-BF+AFA+FB-");
                    break;
                    case 'B':
                        next.append("+AF-BFB-FA+");
                    break;
                    case '-':
                        next.append("-");
                    break;
                    case '+':
                        next.append("+");
                    break;
                    case 'F':
                        next.append("F");
                    break;
                }
            }

            current = next;
            generation++;
        }
    }

    return current;
}


I must generate at least upto level 16 and I hope that you will come up with some hint to get around this issue. I tried to step through GDB and I found that the program crashes at

 
current = next;


A better way to formulate the algorithm is requested from the forum.


Thanks
Do you realize how big your string is getting? Remember you're algorithm has exponential growth. You're going to run out of space for your string well before you reach generation 16.

Remember a string, or any container can only hold a finite amount of data. The amount is limited by your operating system, processor type, and several other limits.

If you really want to generate a pattern this big I suggest you think about using some files.
Topic archived. No new replies allowed.