Deleting a char*

In my destructor I have it so that it will delete the char* buf, but after the destructor is done it's keeps on giving me a debugging error. If anyone could help me figure out why its doing this it would be much appreciated
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class String{
protected:
     char* buf;
     int length;
public
     String(){
	buf = new char[1];
	strcpy(buf, " ");
	length = strlen(buf);
     }
     int print(){
	cout << buf << endl;;
     }  
     ~String(){
	delete [] buf;
	buf = NULL;
     }
};

int main(){
    String a;
    a.print();
    return 0;
}
6
7
8
String(){
    buf = new char[1];
    strcpy(buf, " ");

The string " " requires 2 characters in buf -- one for the space, and one for the terminating null character.

Try changing the 1 on line 7 to a 2 and see if that fixes it.
Topic archived. No new replies allowed.