initializing vector of char * s

Hello, I am trying to solve this puzzle: http://www.puzzleup.com/2015/puzzle/?13

to do that, I want to calculate all the possible codes and pass them in a vector

You will produce a set of 7-letter codes using the the letters A, B, C, D, E, F and G.


So I tried to create vector of char arrays. The code is shared below:

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
#include <iostream>
#include <string>
#include <vector>
#include <array>
#include <cmath>

char letters[] = {'a','b','c','d','e','f','g'};

char * tempArr;
char * pastArr;


std::vector<char * > deneme;
char s1[] = {'a', 'b'};
char s2[] = {'c', 'd'};

void calculateAllPossibilities(int depth, int lastDepth)
{
    //depth 1 den baĆ¾layacak
    for( int i = 0; i < sizeof(letters); i++ )
    {
        //

        if ( depth != 1 && depth != lastDepth )
        {
            //
            tempArr = new char[depth];
            for( int j = 0; j < depth-1; j++ )
            {
                //
                *tempArr = pastArr[j];
                tempArr++;
            }
            *tempArr = letters[i];
            delete pastArr;
            pastArr = new char[depth];
            for( int k = 0; k < depth; k++ )
            {
                //
                *pastArr = tempArr[k];
                pastArr++;
            }
            delete tempArr;
            calculateAllPossibilities(depth + 1, lastDepth );
        }
        else if( depth == lastDepth )
        {
            //
            tempArr = new char[depth];
            for( int k = 0; k < depth - 1; k++ )
            {
                //
                *tempArr = pastArr[k];
                tempArr++;
            }
            *tempArr = letters[i];
            deneme.push_back(tempArr);
            delete tempArr;
        }
        else if( depth == 1 )
        {
            //
            pastArr = new char[depth];
            *pastArr = letters[i];
            delete tempArr;
            calculateAllPossibilities(depth + 1, lastDepth );
        }

    }
}

int main()
{

    calculateAllPossibilities(1,7);

    std::cout << deneme[0][2];

    return 0;
}


The problem is, when I try to cout the values of deneme vector, it gives me different thing in each compilation. Like those: ":", "_", "X", "2" :)

What am I doing wrong here? LB and Helios?
Last edited on
If you want your computer to go crazy, run this 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
#include <iostream>
#include <string>
#include <vector>
#include <array>
#include <cmath>

char letters[] = {'a','b','c','d','e','f','g'};

char * tempArr;
char * pastArr;


std::vector<char * > deneme;
char s1[] = {'a', 'b'};
char s2[] = {'c', 'd'};

void calculateAllPossibilities(int depth, int lastDepth)
{
    //depth 1 den baĆ¾layacak
    for( int i = 0; i < sizeof(letters); i++ )
    {
        //

        if ( depth != 1 && depth != lastDepth )
        {
            //
            tempArr = new char[depth];
            for( int j = 0; j < depth-1; j++ )
            {
                //
                *tempArr = pastArr[j];
                tempArr++;
            }
            *tempArr = letters[i];
            for( int x = 0; x < depth; x++ )
            {
                //
                std::cout << tempArr[x] << ",";
            }
            std::cout << std::endl;
            delete pastArr;
            pastArr = new char[depth];
            for( int k = 0; k < depth; k++ )
            {
                //
                *pastArr = tempArr[k];
                pastArr++;
            }
            delete tempArr;
            calculateAllPossibilities(depth + 1, lastDepth );
        }
        else if( depth == lastDepth )
        {
            //
            tempArr = new char[depth];
            for( int k = 0; k < depth - 1; k++ )
            {
                //
                *tempArr = pastArr[k];
                tempArr++;
            }
            *tempArr = letters[i];
            for( int x = 0; x < depth; x++ )
            {
                //
                std::cout << tempArr[x] << ",";
            }
            std::cout << std::endl;
            deneme.push_back(tempArr);
            delete tempArr;
        }
        else if( depth == 1 )
        {
            //
            pastArr = new char[depth];
            *pastArr = letters[i];
            std::cout << pastArr[0] << std::endl;
            delete tempArr;
            calculateAllPossibilities(depth + 1, lastDepth );
        }

    }
}

int main()
{

    calculateAllPossibilities(1,7);

    std::cout << deneme[0][2];

    return 0;
}


First time I encounter something like this. It makes PC beeps! WTF did I do?!
It makes PC beeps! WTF did I do?!

More simple way to make computer beep: std::cout << '\a';
I did not look at your code yet, but it seems that you are accessing uninitialized memory.

EDIT: problem number 1: you have wrong delete here.
Remember: if you use new, use delete. If you use new[], use delete[]. In other words arrays should be deleted with delete[]

Problem 2:
1
2
3
tempArr = new char[depth];
tempArr++;
delete[] tempArr;
You should pass to delete exact same unchanged pointer, which was returned by new.

Problem 3:
1
2
deneme.push_back(tempArr);
delete[] tempArr;
deneme now contains invalid pointer: pointing to deleted data.
Last edited on
@MiiNiPaa

thanks for the reply. I used std::string instead of char* and problem solved.
Topic archived. No new replies allowed.