Deleting a string with a destructor

I am trying to delete the string str in the deconstructor, but when I do that, I get an error. So how do you delete a string in the deconstructor. I have tried using a pointer (string* str), but then i get an error when i try to do (str = str1)


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
  /* Start of Class Line */
class Line
{
public: 
	int id;
	Line();
	Line(string str1 ){
		int id = rand();
		str=str1;
	}
	~Line();
	int getWordCount();
	void setString(string str1);
	void setWordCount(int value1);
	int getCharCount();
	void setCharCount(int value2);

private:
	string str;
	int wordcount;
	int charcount;
};

Line::Line()
{
	int id = rand();
}

Line::~Line()
{
	delete str;
}
void Line::setString(string str1)
{
	str = str1;
}
int Line::getWordCount()
{
	return wordcount;
}

void Line::setWordCount(int value1)
{
	wordcount = value1;
}

int Line::getCharCount()
{
	return charcount;
}

void Line::setCharCount(int value2)
{
	charcount = value2;
}

/* End of Class Line */
str is not a pointer to dynamically allocated memory, so you cannot delete it. In fact, you don't have to: it is a member of a class and maneges owned memory itself.
okay, so the problem here is that there is no reason for you to use the destructor. destructors are used when you have pointer member data on the free store for your class. The string you are trying to delete doesn't need to be deleted manually. All that stuff is handled by C++. The only time you would need to practically use a destructor would be when you had to deal lots of data. A longer explanation is at http://www.cplusplus.com/doc/tutorial/dynamic/
and
http://www.cplusplus.com/doc/tutorial/classes2/
Provide the following constructors:
default that sets id to a random number, takes a string to set str and sets a random number for id,
takes string to set str and int to set id. Have a destructor delete the string.

The instruction says I have to delete the string so If I want to delete it, can I just say it to null in the deconstructor
no, in order to delete it, first you have to allocate memory for it on the free store. do this by making string str into string * str and in your constructor doing str = new string. then, in your destructor do delete str.
Your instructor probably wanted you to use c-strings (char*) to hold data, which you should manually allocate and deallocate. You have used std::string which does it automaticly.
Okay, so I made string str into string* str and then in my constructor, I did str = new string. But then I get an error whenever there is str1.
http://www.cplusplus.com/doc/tutorial/pointers/
Read at least this, if you did not pay ayyention to that earlier.
You absolutely must not delete something that you did not create with new.

Is your assignment telling you to dynamically create the string with new?


EDIT: holy crap I got ninja'd by like 5 people. I didn't realize how long I had this window open.
Last edited on
Disch, I think so. I did string* str = new string, but then i get an error when i try to do str = str1 for my set and get functions
I think you have to set it up like this to have a pointer receive from another variable:
 
str = &str1;


I'm not too familiar with pointers but it's most likely either that or:
 
*str = str1;


EDIT:
After a quick refresh, I realize that the first piece of code would have the pointer point to the data and the second piece of code would change the value of the data that it points to. I hope this helps.
Last edited on
crimsonzero2 your method worked but can you please explain why?

I know & is a reference variable, but how does it exactly work?
Ok, so variables are held within the computer's memory with addresses to them. This code:
 
str = &str1;

This code states that I want this pointer to point to this address and retrieve this data when needed. This pointer now can control str1. So, if I wanted to change it, I would use the second piece of code. I'm not the best at really wording it correctly (I used to be very bad with pointers). I would recommend looking at that forum that was posted earlier:

http://www.cplusplus.com/doc/tutorial/pointers/

This is a good article that discusses how pointers work and has good diagrams to show what is happening in the background.
Just to make sure, it should be
7
8
9
10
Line(string str1 ){
    int id = rand();
    str = new string(str1);
}
or even
7
8
Line(string str1 ): id(rand()), str(new string(str1))
{}
.

This is wrong:
7
8
9
10
11
Line(string str1 ){
    int id = rand();
    *str=str1; // BOOM! Dereferencing an uninitialized pointer
    // Although, if you put 'str = new string;' right before the above line, this will be okay
}
and so is this:
7
8
9
10
Line(string str1 ){
    int id = rand();
    str=&str1; // Uh-oh, pointing to local variable...
} // BOOM! str1 disappears here, so now you can't use str at all 
Last edited on
Topic archived. No new replies allowed.