Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Destructor deleting string: Should be easy, but...

Status
Not open for further replies.

TuxedoTemplar

Programmer
Apr 9, 2003
14
US
I've gone over this thing with a fine-tooth comb until my face is blue and I've only narrowed it down to the delete[] operators in the destructor not working (the program gives an assertion error.) As far as I've learned and I can observe everything is done as it should be, so I'm basically clueless now. I'm no good at explaining things, so I'll just post the abreviated code samples:

PageSection Class Header
Code:
#ifndef PAGESECTION
	#define PAGESECTION

class PageSection
{
public:
	PageSection(const char *label, const char *body, const char *timestamp, int subsections = 0);
	~PageSection();

	char *getLabel();
	char *getBody();
	char *getTimestamp();
	int getSubsectionNum();
	void setLabel(const char *label);
	void setBody(const char *body);
	void setTimestamp(const char *timestamp);
	void setSubsectionNum(const int num);

private:
	char *LABEL;
	char *BODY;
	char *TIMESTAMP;

	int LABELSIZE;
	int BODYSIZE;
	int TIMESTAMPSIZE;

	int SUBSECTIONS;
};

#endif

PageSection Constructor and Destructor
Code:
#include <cstring>
#include "PageSection.h"


PageSection::PageSection(const char *label, const char *body, const char *timestamp, int subsections)
{
	LABEL = NULL;
	  LABELSIZE = 0;
	BODY = NULL;
	  BODYSIZE = 0;
	TIMESTAMP = NULL;
	  TIMESTAMPSIZE = 0;
	SUBSECTIONS = subsections;

	if(label)
	{
		LABELSIZE = strlen(label + 1);
		LABEL = new char[LABELSIZE];
		strcpy(LABEL, label);
	}
	if(body)
	{
		BODYSIZE = strlen(body + 1);
		BODY = new char[BODYSIZE];
		strcpy(BODY, body);
	}
	if(timestamp)
	{
		TIMESTAMPSIZE = strlen(timestamp + 1);
		TIMESTAMP = new char[TIMESTAMPSIZE];
		strcpy(TIMESTAMP, timestamp);
	}
}

PageSection::~PageSection()
{
	if(LABELSIZE)
		delete[] LABEL;
	if(BODYSIZE)
		delete[] BODY;
	if(TIMESTAMPSIZE)
		delete[] TIMESTAMP;
}

...

Test Program
Code:
#include <iostream>
#include "PageSection.h"
#include "Page.h"

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


void main()
{
	PageSection Hoy("Hi", "Ho", "Yo", 0);

	cout	<< Hoy.getLabel() << endl << Hoy.getBody()
		<< Hoy.getTimestamp() << endl;

	return;
}

Any help would be GREATLY appreciated!
 
I couldn't find it anywhere in the helps, but might it help if you do delete[SIZE] instead? that way it doens have to guess at how big it is.

I prefer to use stings instead. They then handle their own problems most of the time (though there are some functions that work better on char *)
use <string.h>
and string tmpstring;
tmpstring.assign("vale");
tmpstring.c_str() returns the associated char* for the functions that need it.
 
I also have seen it written as
delete []myArray;

does that make a difference?
 
delete [] myArrayy, delete[] myArrayy, and delete []myArray are all the same, as far as I've been taught. I'm also pretty sure you can't have anything in the [] brackets of the delete operator, though I haven't tried before.

This still puzzles me...
 
Ok a bit of clarification: at the end of main() the default destructor for "Hoy" is called, deleting the allocated char *s for the label, body, and timestamp. It doesn't matter what these strings do or what's in them, as the problem is simply that they won't delete[] like a good string is supposed to. As you can see though, they are allocated using the new operator and filled with strcpy, which should not (as far as I know) cause any problems.
 
Code:
LABELSIZE = strlen(label + 1);
should be:
Code:
LABELSIZE = strlen(label) + 1;
Same for the other two strings.
 
Oooooh! Ok! I knew it was something just outside my vision. Something obvious. :p Usually is.

Thanks a lot for the help!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top