Delete elements of an object array.

sokairyk's Avatar, Join Date: Sep 2010
Newbie Member
Hello everyone!

I need some help to make my code more efficient. I write a C++
program implementing the kmeans algorithm with parallelization
(using object oriented MPI).

My question is rather stupid but since I program in C# the last 2 years
and the garbage collector does the job for me... (I know I'm lame )

So I have 2 classes:

Code:
class TextTermEntry : public OOMPI_User_type
{
public:
    //Members
    int code;
    int frequency;
    float weight;

    //Constructors
    TextTermEntry();
    TextTermEntry(const TextTermEntry& term);

private:
    // Static variable to hold the newly constructed
    // MPI_Datatype
    static OOMPI_Datatype type;
};

class TextEntity : public OOMPI_User_type
{
public:
    //Members
    char file_path[FILEPATH_SIZE];
    int text_terms_size;
    TextTermEntry* text_terms;

    //Constructors
    TextEntity();
    TextEntity(const TextEntity& text);

    //Functions
    void set_file_path(char* _file_path);
    void initialize_text_terms(void);
    void initialize_centroid_terms(int max_terms_size);
    void initialize_centroid_terms();
    void get_weight_vector(float* weight_vector, int vector_size);
    bool recalculate_terms_weight(float* new_weight_vector, int vector_size, int text_count);
    void delete_text_terms(void);

private:
    // Static variable to hold the newly constructed
    // MPI_Datatype
    static OOMPI_Datatype type;
};
I initialize the text_terms pointer as an array of TextTermEntity objects
the following way:

Code:
void TextEntity::initialize_text_terms()
{
    text_terms = new TextTermEntry[text_terms_size];
}
Now what I do is to define 2 global variables:

Code:
TextEntity* unsorted_collection;    //Array to hold objects representing the text files (unsorted)
TextEntity* text_collection;        //Array to hold objects representing the text files (sorted)
The unsorted collection will be sorted (based on the text_terms_size value of array element) with a quicksort algorithm implementation and the swaping of the TextEntities is done the following way:

Code:
void swap_texts(TextEntity* text_1, TextEntity* text_2)
{
    TextEntity* temp = new TextEntity(*text_1);

    *text_1 = *text_2;
    *text_1->text_terms = *text_2->text_terms;
    *text_2 = *temp;
    *text_2->text_terms = *temp->text_terms;
}
where text_1 and text_2 are pointers of 2 elements in the unsorted_collection array.

Because I want to resort the now sorted unsorted_collection array for my parallel distribution of the objects I do the following:

Code:
text_collection = new TextEntity[text_collection_size];

int local_texts_size = text_collection_size / size;
        int index = text_collection_size - 1;

        //Copy the sorted data accesing the index as described above
        for (int j = 0; j < local_texts_size; j++)
            for (int i = 0; i < size; i++)
            {
                text_collection[local_texts_size * i + j] = unsorted_collection[index];
                text_collection[local_texts_size * i + j].text_terms = unsorted_collection[index].text_terms;
                index--;
            }
Now what happens is that I want to delete the unsorted_array and some elements of the text_collection array to free some memory.

Although my code works it doesn't free any memory... and I'm pretty confused with what I've done

If I try to

Code:
delete [] unsorted_collection;
I the program crashes when it tries to access the text_collection array
and even if I leave the unsorted_collection array as is and at least try to
run:

Code:
void TextEntity::delete_text_terms()
{
    delete [] text_terms;
    text_terms = NULL;
}
.
.
.
for (int i = local_collection_size; i < text_collection_size; i++)
            text_collection[i].delete_text_terms();
.
.
.
Can anyone tell me how should I handle these parts to free some memory?

Thanks!
0
sokairyk's Avatar, Join Date: Sep 2010
Newbie Member
To make things more simple what I need to know is:

Let's say that I have 2 simple objects:

Code:
class B
{
    public:
    int myNum;
};

class A
{
    public:
    B* myObj;
};
when I do:

Code:
.
.
.
A* obj1;
A* obj3;

int main()
{
    obj3 = new A[20];
    .
    .
    .
    {
        A* obj2 = new A;
        *obj2->myObj = new B[10];

        obj1 = new A;
        *obj1 = *obj2;
    }
    .
    .
    .
}
obj2 allocated an array of B objects and when it copied its values to obj1 the pointer to that array was passed also. So the 2 objects point to the same array.

When the obj2 leaves its variable scope the memory of that object is freed (if I understand correctly). That means that the array of B objects that was created through obj2 is also deleted and obj1 which is global points to an invalid memory space?

My other question is is there a way to free the memory that the last 15 elements of the obj3 array hold?