Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IntegerSet in C++

Tags:

c++

arrays

class

I am having problems with an assignment. The assignment is to create an IntegerSet an IntegerSet is an array of 100 elements that represents the numbers from 0 to 99 for example if the number 5 is present in set a then a[5] = 1, an empty set is an array of zeroes.

I created a class called IntegerSet and here is the code in integerSet.cpp

#include "integerset.h"
#include <iostream>
using std::cout;
using std::endl;

IntegerSet::IntegerSet(){
    int temp[100] = {0};
    set = temp;
}

IntegerSet::IntegerSet(int * setPtr) {
    set = setPtr;
}

void IntegerSet::insertElement(int toInsert) {
    if(toInsert < 100 && toInsert >= 0) {
        set[toInsert] = 1;
    }
}

void IntegerSet::deleteElement(int toDelete) {
    if (toDelete < 100 && toDelete >= 0 ) {
        set[toDelete] = 0;
    }
}

IntegerSet * IntegerSet::unionOfSets(IntegerSet * otherPtr) {
    int newSet[100] = {0};
    for(int i = 0; i < 100; i++ ) {
        if (this->set[i] == 1 || otherPtr->set[i] == 1) {
            newSet[i] = 1;
        }
    }
    return new IntegerSet(newSet);
}

IntegerSet * IntegerSet::intersectionOfSets(IntegerSet* otherPtr) {
    int newSet[100] = {0};
    for(int i = 0; i < 100 ; i++) {
        if(this->set[i] == 1 && otherPtr->set[i] == 1){
            newSet[i] = 1; 
        }
    }
    return new IntegerSet(newSet);
}

bool IntegerSet::isEmpty(){
    for(int i = 0 ; i < 100 ; i++) {
        if(set[i] == 1) {
            return false;
        }
    }
    return true;
}  

bool IntegerSet::isEqualTo(IntegerSet * otherPtr) {
    for(int i = 0; i < 100 ; i++) {
        if(this->set[i] != otherPtr->set[i]) {
            return false;                
        }        
    }
    return true;
}

void IntegerSet::printSet() {
    if(isEmpty()) {
        cout << "---" << endl;
    } else { 
        for(int i = 0; i < 100 ; i++) {
            if(set[i] == 1) {
                cout << i << ' ';
            }
        }
        cout << endl;        
    }
}   

IntegerSet::~IntegerSet() { 
    delete[] set;
}     

The class has private member int * set;.

This is the main function used to test my class:

#include <iostream>
#include<new>
#include "integerset.h"

using std::cout;
using std::endl;

int main(int argc, char *argv[])
{
    IntegerSet * set1Ptr = new IntegerSet();
    set1Ptr->insertElement(1);
    set1Ptr->insertElement(2);
    set1Ptr->insertElement(3);
    set1Ptr->insertElement(50);
    IntegerSet * set2Ptr = new IntegerSet();
    set2Ptr->insertElement(0);
    set2Ptr->insertElement(3);
    set2Ptr->insertElement(2);
    set2Ptr->insertElement(51);
    set2Ptr->insertElement(100);
    set2Ptr->insertElement(99);
    IntegerSet * set3Ptr = set1Ptr->unionOfSets(set2Ptr);
    IntegerSet * set4Ptr = set1Ptr->intersectionOfSets(set2Ptr);
    cout << "First Set" << endl;
    set1Ptr->printSet();
    cout << "Second Set" << endl;
    set2Ptr->printSet();
    cout << "Equal ? : " << set1Ptr->isEqualTo(set2Ptr) << endl;
    cout << "Intersection : " << endl;
    set4Ptr->printSet();
    cout << "Union : " << endl;
    set3Ptr->printSet();
    system("PAUSE");
    return EXIT_SUCCESS;
}

This the output I get when running

First Set
16 19 35 45 46 54 66 84
Second Set
0 1 10 12 13 14 19 35 45 46 54 66 84
Equal ? : 1
Intersection :
16 19 35 45 46 54 66 84 98
Union :
0 1 10 12 13 14 19 35 45 46 54 66 84 98

I tried tracing and I have no idea what's wrong, so any help is really appreciated.

like image 322
A. H. Avatar asked Dec 12 '25 16:12

A. H.


1 Answers

IntegerSet::IntegerSet(){
   int temp[100] = {0};
   set = temp;
}

That is creating a local array of 100 elements and storing a pointer to it in the member set. The problem here is that the lifetime of temp is restricted to the constructor, and as soon as the constructor exits, the array is destroyed and what you have is a dangling pointer (a pointer to a block of memory that is not valid).

If you need to use pointers then you should dynamically allocate the memory and make sure to release it in the destructor. If not, you can declare the array as a member attribute of your type and avoid the pointers altogether.

like image 59
David Rodríguez - dribeas Avatar answered Dec 15 '25 07:12

David Rodríguez - dribeas



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!