Runtime error when using delete

I have a strange runtime error. I'm trying to make a string by using char* with the command new. Everything goes fine, but when I want to change the contents of my string, and I want to free the memory allocated with the command delete my program runs in an error. It's a runtime error, when compiling everything is fine. Here's the code, please help:

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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include <iostream>
#include <string>

using namespace std;

class Nev{
protected:
	char *string;
	int length;
public:
	Nev() {
		string = new char(1);
		length=1;
		string = "";
	}
	Nev(char n[], int l) {
		string = new char(l+1);
		length=l;
		strcpy(string, n);
	}
	void getstring() {
		cout << string;
	}
	void getLengthN() {
		cout << length;
	}
	~Nev() {
		delete string;
	}
};

class Alkalmazott:public Nev{
	char *beosztas;
	int length2;
public:
	Alkalmazott(char n[], char b[], int l, int l2) {
		string = new char(l+1);
		length=l;
		strcpy(string, n);
		beosztas = new char(l2+1);
		length2=l2;
		strcpy(beosztas, b);
	}
	~Alkalmazott(){
		delete beosztas;
	}
	void getInfo() {
		cout << beosztas;
	};

	Alkalmazott& operator=(const Alkalmazott& sec) {
		if (this != &sec) {
			delete string;
			delete beosztas;
			string = new char(sec.length+1);
			strcpy(string, sec.string);
			beosztas = new char(sec.length2+1);
			strcpy(string, sec.beosztas);
			length=sec.length;
			length2=sec.length2;
		}
		return *this;
	}

	void setBeosztas(char *b, int l) {
		if (beosztas != NULL) delete beosztas;
		beosztas = new char(l+1);
		strcpy(beosztas,b);
		length2=l;
	}  

	void getLength() {
		cout << length2;
	}
};

void main(){
	Alkalmazott a("Kovacs", "Raktaros", 6, 8);
	Alkalmazott b("Kiss", "Ugyved", 4, 6);
	int stop;

	a.getstring();
	a.getInfo();
	a.getLengthN();
	a.getLength();
	a.setBeosztas("Ugyved", 6);
	cin >> stop;
}



I'm using Visual Studio 2008.
You can save the pain of memory allocation for strings alltogether by switching to std::string.
The only problem is that if I don't use it this way, I will fail my class at OOP.:(
delete beosztas; should be delete [] beosztas;

As you are deleting an array of characters.
Last edited on
I've tried it that way too... not working:( Same runtime error...:(
Ok. Some notes on your code.

1) Try not to use lowercase "L" as a parameter, it's too similar to "I". Use either a shorthand name (e.g "len") or a full variable name (e.g "length").

2) All of your deletes should be delete [] variable when they are char pointers.

3) Instead of passing in the length as well, just use the strlen() function.

4) if (beosztas != NULL) delete beosztas; will probably crash your application. Because beosztas is never going to be NULL. Nowhere in your code have you made it null.

4) Use string type instead of character arrays. There is no way you should fail OOP for this. String is a C++ type (OO) and Char* is a C-Type (not OOP).

5) If you are going to use strcpy. Use memset first to blank the memory to \0. This will ensure your char arrays are null-terminated and will be read/deleted properly. I would also suggest using strncpy instead of strcpy for buffer-overflow safety.

Edit -->

6) use int main() not void main.
Last edited on
I've deleted the " if (beosztas != NULL) delete beosztas " part, and it's not working... I've understand that I could use string and these kind of things, but it bothers me that this isn't working this way. I've already tried making a simple program too, but I get the same error:(

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <string>

using namespace std;

void main(){
	char *st;

	st = new char[30];
	st = "Haha\0";
	cout << st;
	delete[] st;
}



Edit: Tried it with int main too, nothing changed:(
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>

using namespace std;

int main() {

  char *pChar = new char[10];
  sprintf(pChar, "%s", "Hello"); // sprint will null-terminate for you
  cout << "pChar = " << pChar << endl;

  delete [] pChar;
  return 0;
}


Note: no need to have #include <string> as you are not using the string data type.
Problem solved. The problem was that when I allocated memory for my char* I used () brackets instead of [] brackets. Everything works now fine, but now I have a new problem(in a new forum post).
Topic archived. No new replies allowed.