HEllo
this is reaaally urgent, the deadline is within less than an hour and thirty minute, and I lost hope that I can fix the problem myself. I know it has to do with pointers and allocating memory dynamically. The assignment was to use operator overloading to add two sets (adding two sets by getting the union of the two sets, which means all the elements in both set but not repeated). I figured out a way to do so but I think this memory problem is the cause why it's not working

here is the constructor of the class set that I used:

set::set()
{
	size = 0;
	elements = new int [size];
}

for the two sets to be added I used this member function:

void set::setSet()
{
	cout<<"Enter the size of the set: ";
	cin>>size;
	elements = new int [size];
	cout<<"Enter the elements of the set: ";
	for(int i=0; i<size; i++)
		cin>>elements[i];
}

and here is the operator + overloading member function:

set set::operator +(const set& s2)
{
	set result;
	int inter = 0;
	int *intersection = new int [inter];

	for(int a=0; a<size; a++)
	{
		for(int j=0; j<s2.size; j++)
		{
			if(elements[a] == s2.elements[j])
			{
				inter++;
				intersection[a] = elements[a];
			}
		}
	}
	result.size = (size + s2.size) - inter;
	result.elements = new int [result.size];
	int i = 0;
	for(i=0; i<result.size; i++)
	{
		for(int j=0; j<size; j++)
		{
			result.elements[i] = elements[j];
		}
	}
	bool same = false;
	for(int k=i, j=0; k<result.size, j<s2.size; k++, j++)
	{
		for(int l=0; l<inter; l++)
		{
			if(s2.elements[j] == intersection[l])
				same = true;
		}
		if (same == false)
			result.elements[k] = s2.elements[j];
		else if (same == true)
			continue;		
	}
	return result;
}

and here is the main:

#include<iostream>
#include"sets.h"

using namespace std;

int main()

{
	set s1;
	s1.setSet();
	s1.printSet();
	
	set s2;
	s2.setSet();
	s2.printSet();

	set s3 = s1 + s2;
	s3.printSet();
	return 0;

}

I get the debug error only when I come to the overloading part, s3.

the errors are "heap corruption detected" or "debug assertion failed _block_type_is_valid phead- nblockuse"

Please Help me! Thanks!

Recommended Answers

All 4 Replies

When you execute

int *intersection = new int[inter];

and then you change the value of inter, that does not change the number of elements in the block of memory to which intersection points.

That is exactly the assumption you are making in lines 5 and 14, respectively, of the third segment of code you cited.

I hate to be the bearer of bad news, but if you're writing code like this, it shows that you really don't understand how memory allocation works, and you're unlikely to acquire that understanding between now and when your homework is due.

As mentioned, the issue is that the memory you allocated previously with new int[inter] is still an array with zero elements when you try to use it with

intersection[a] = elements[a];

A slightly hackish and wasteful way around this would be to allocate size+s2.size elements for intersection (since that's the most memory you'll need).

I'm really not sure what you're doing here.

int i = 0;
	for(i=0; i<result.size; i++)
	{
		for(int j=0; j<size; j++)
		{
			result.elements[i] = elements[j];
		}
	}

You overwrite each results.elements with a sequence of elements, over and over again. It's like going

a1 = b1;
a1 = b2;
a1 = b3;
a2 = b1;
a2 = b2;
a2 = b3;

which is clearly redundant.

Thanks for the replies, appreciate it

but what about if I write it this way (sorry am not really an expert here)

int inter = 0;
	int *intersection = NULL;

	for(int a=0; a<size; a++)
	{
		for(int j=0; j<s2.size; j++)
		{
			if(elements[a] == s2.elements[j])
			{
				inter++;
				intersection[a] = elements[a];
			}
		}
	}
	intersection = new int [inter];

That's worse. The array must exist before you start trying to put data into it!

Two simple ways of doing this:

  1. Figure out how big itner needs to be first. You can do this with a similar set of for loops that just increments inter and doesn't try to save the data into intersection[a].
  2. Allocate more space than you need for intersection (like size+s2.size). If you're worried about space you can later copy the data into an array of the correct size, although in this case you might as well just find the size first.
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.