简体   繁体   中英

Dynamic Array of Booleans Constructor Issue

I'm having problems with the constructor of a class with an array of booleans, representing whether a int value is present by holding a "true" value at that index. So if I create a new set:

IntSet set(4, 5, 6); // up to 5 parameters may be entered, with default values if some are missing.

this will create an array of size 6 (the highest value passed) and the value at indexes 4, 5 and 6 will be set to true, while the rest false.

This is what I have so far but I can't seem to get it to work properly:

.h:

class IntSet {

public:

bool* value;
int size;

// CONSTRUCTORS
IntSet(int val1 = -1, int val2 = -1, int val3 = -1, int val4 = -1, int val5 = -1);
IntSet(const IntSet &other);
~IntSet();

.cpp:

IntSet::IntSet(int val1, int val2, int val3, int val4, int val5) {



// check if all values are default.
if((val1 == -1) && (val2 == -1) && (val3 == -1) && (val4 == -1) && (val5 == -1)) {

    val1 = 1;
    size = 1;

// else if one or more values are not default, find largest value and 
// set size to the value.
} else {

    if(val1 >= val2 && val1 >= val3 && val1 >= val4 && val1 >= val5) {
        size = val1;
    }

    if(val2 >= val1 && val2 >= val3 && val2 >= val4 && val2 >= val5) {
        size = val2;
    }

    if(val3 >= val1 && val3 >= val2 && val3 >= val4 && val3 >= val5) {
        size = val3;
    }

    if(val4 >= val1 && val4 >= val2 && val4 >= val3 && val4 >= val5) {
        size = val4;
    }

    if(val5 >= val1 && val5 >= val2 && val5 >= val3 && val5 >= val4) {
        size = val5;
    }
}

// Create pointer to boolean array of size "size"
value = new bool[size];

// set all values at their specified index to true.
if(val1 >= 0)
    value[val1] = true;

if(val2 >= 0)
    value[val2] = true;

if(val3 >= 0)
    value[val3] = true;

if(val4 >= 0)
    value[val4] = true;

if(val5 >= 0)
    value[val5] = true;
}

//------------------------------------------------------------------------------

IntSet::IntSet(const IntSet &otherSet) : size(otherSet.size){
cout << "In Copy Constructor" << endl;

value = new bool[size];
for(int i = 0; i < size; i++)
    value[i] = otherSet.value[i];
}

//------------------------------------------------------------------------------

IntSet::~IntSet() {
cout << "In Destructor" << endl;

delete[] value;
}

For some reason, when I debug and follow the values, I see "value" set to true(205). I checked by outputting the boolean values in the console and all values print as "205" instead of "0" or "1". I'm not sure what is going on, but I think the issue is with bool *value = new bool[size]; although I've tried many different things and nothing seems to work. I will tackle the copy constructor and destructor once I figure this out.

This is a class assignment, so I'm not looking to get the solution from anyone, but any tips that may point me in the right direction are much appreciated!

Thanks everyone!

In the constructor:

IntSet::IntSet(int val1, int val2, int val3, int val4, int val5);

You create a local variable called the same as the class variable "value", which hides the class declaration.

// Create pointer to boolean array of size "size"
bool *value = new bool[size];

Therefore, the class variable is still unitialized.

Results:

  • local variable value causes memory leak
  • class variable never defined, will be improperly deleted - runtime error

Instead use:

value = new bool[size];

or

this->value = new bool[size];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM