Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

CoderX

Damage after Normal Block

This topic is 5988 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I get this when I make a ''new'' object of my own class and delete it. Now I have made my own destructor in that class but currently there is nothing in it, just empty braces. Here is the code which is in my ''main'' function: A* a = new A(blah blah); delete a; Where can I look up these Debug errors?

Share this post


Link to post
Share on other sites
Advertisement
I think I found the culprit. I keep an array of pointers in my class. In my constructor I define it's proper size, and also make each element point to NULL. Like so:

array[size];

for(int i=0;i < size; i++)
{
array = NULL;
}


Now when I comment out the for loop, I dont get the error anymore. Why is that? Am I going out of the array index?


[edited by - CoderX on May 31, 2002 1:09:22 PM]

[edited by - CoderX on May 31, 2002 1:10:08 PM]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
It''s probably just a matter of style, but I suggest using memset rather than looping through the array.


  
#define SIZE 10
void main()
{
<datatype> *array = new <datatype>[SIZE];
memset(array,''\0'',SIZE*sizeof(<datatype>));

delete [] array;
}

Share this post


Link to post
Share on other sites
You are right. It has to do with the way I implemented the array of pointers. I get a warning that I am making a zero-sized array, but which I define the size in the constructor, so I think this is bad programming and have ditched it.

So, now what I am doing is keeing just a pointer of type same class in my class. Then in the constructor, I want to make a new array of pointers of type same class. But the compiler now gives me an error that I have to define a default constructor for my class. Here is what I got in the class now:


class A
{
private:
A* arrayA;

public:
A(int size)
{
arrayA = new A[size];
}
}


Now I wrote a default constructor and it works. But is that okay? Why does the array definition need a default destructor? I know I am doing something stupid here.

Share this post


Link to post
Share on other sites
MEMORY LEAK

class A
{
private:
A* arrayA;
public:
A(int size)
{
arrayA = new A[size];
}
};


You need to do this:


class A
{
private:
A* arrayA;
public:
A() : arrayA(NULL) {}
A(int size)
{
arrayA = new A[size];
}
~A()
{
if(arrayA != NULL)
delete [] arrayA;
}
};


You don't need to set the individual pointers to null before deleting them.

But you have to delete the array, or else every time your array gets destroyed, you'll leave a chunk of memory.

[edited by - daerid on May 31, 2002 6:48:20 PM]

Share this post


Link to post
Share on other sites
Thanks for the response. But I already have that in my class, just didn''t write it out in the post because it was irrelevant to my question.

I think I am getting the hang of this, but now I got another problem. I got this same class going and I also added a function that is supposed to add stuff to the array. Here is what I got in that function:


void A::addElement(A* newElement)
{
array[numberOfElements] = newElement;
numberOfElements++;
};



This is me this error:

binary ''='' : no operator defined which takes a right-hand operand of type ''class A *'' (or there is no acceptable conversion)


What am I doing wrong now? Been trying to figure this out for a while.

Share this post


Link to post
Share on other sites
You need to overload the = operator for your class. Look it up on cplusplus.com. It means that the = operator for your class is not defined. You may need to write a copy construtor too (which is a good thing to have anyway).

/*=========================================*/
/* Chem0sh */
/* Lead Software Engineer & Tech Support */
/* http://www.eFaces.biz */
/*=========================================*/

[edited by - Chem0sh on May 31, 2002 7:10:01 PM]

Share this post


Link to post
Share on other sites
I suspected that but thought that this is just a simple assignment of addresses. Well I have overloaded that = operator and it works fine. Thanks for the help and for the awesome link as well.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!