Return a bool Array

Nov 10, 2013 at 7:22pm
Could somebody tell me how to return a bool array from a function? When I try the code below the bools gets messed up after being returned.



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
#include <iostream>

using namespace std;

bool* SetStuff(bool a, bool b, bool c, bool d, bool e, bool f, bool g, bool h){ //Creates a bool array then returns it.

    bool Booler[8];
    Booler[0] = a;
    Booler[1] = b;
    Booler[2] = c;
    Booler[3] = d;
    Booler[4] = e;
    Booler[5] = f;
    Booler[6] = g;
    Booler[7] = h;

    for(int x = 0; x<8; x++){ //Displays the values "0|0|0|1|1|1|1|1|" (What they are supposed to be)
        if(Booler[x]){
            cout << "1|";
        }
        else{
            cout << "0|";
        }
    }
    cout << endl;
    return Booler;
}

int main()
{
    bool* Cat;

    for(int x = 0; x<8; x++){ //Shows what's inside the bool before anything is set, just in case it's important
//Displays "1|1|1|1|0|1|0|1|"
        if(Cat[x]){
            cout << "1|";
        }
        else{
            cout << "0|";
        }
    }
    cout << endl;


    Cat = SetStuff(false,false,false,true,true,true,true,true); //Tries to fill Cat with the Boolean values)

    for(int x = 0; x<8; x++){ //Displays the values  "0|1|1|0|1|1|1|0|" (What they become)
        if(Cat[x]){
            cout << "1|";
        }
        else{
            cout << "0|";
        }
    }

    return 0;
}


output:
1|1|1|1|0|1|0|1| //ignore this if not important, (What's inside from before)
0|0|0|1|1|1|1|1| //what the bool array contains inside the function
0|1|1|0|1|1|1|0| //what the bool array contains after being returned (should be the same as the one above)


Last edited on Nov 10, 2013 at 7:24pm
Nov 10, 2013 at 7:24pm
Returning a pointer to a local array has undefined behavior.

You can, however, return pointers to dynamically allocated arrays. http://www.cplusplus.com/doc/tutorial/dynamic/
Last edited on Nov 10, 2013 at 7:24pm
Nov 10, 2013 at 7:45pm
Can you give some sample-code of how to use this in a function with a return value?
Nov 10, 2013 at 8:05pm
Here's a way to do it by defining the array in main() and passing it as a parameter. The original code at lines 33 to 41 was accessing the memory pointed to by an uninitialised pointer, which is not valid.
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
#include <iostream>

using namespace std;

void SetStuff(bool* Booler, bool a, bool b, bool c, bool d, bool e, bool f, bool g, bool h)
{
    Booler[0] = a;
    Booler[1] = b;
    Booler[2] = c;
    Booler[3] = d;
    Booler[4] = e;
    Booler[5] = f;
    Booler[6] = g;
    Booler[7] = h;

    for (int x = 0; x<8; x++)
        cout << Booler[x] << '|';

    cout << endl;
}

int main()
{
    bool Cat[8] = { 0 };

    for (int x = 0; x<8; x++)  // Shows what's inside the array
        cout << Cat[x] << '|';
    cout << endl;

    SetStuff(Cat,false,false,false,true,true,true,true,true); // Tries to fill Cat with the Boolean values)

    for (int x = 0; x<8; x++)  //Displays the values  "0|1|1|0|1|1|1|0|" (What they become)
        cout << Cat[x] << '|';

    return 0;
}


The other way would be to use new and delete as shown in the tutorial pages.

bool * Booler = new bool[8];

and in main(), when you are done with the array,
delete [] Cat;

new and delete should always be matched, in order to avoid memory leaks.
Nov 10, 2013 at 8:35pm
Thanks Chervil, something similar to the first code should work perfect, since I'll always know the length of the array.
Topic archived. No new replies allowed.