delete[] keeps giving me error

Hello everyone! I'm new around here. School assigment, yadayada...

Here's my situation:

Whenever I try to change an user's data (By using the _Set functions below), I get an error that goes along these lines:

HEAP CORRUPTION DETECTED: after Normal block

Here's the situation:

The User.h:
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
#include <string>
#include <stdlib.h>
#include <stdio.h>

class cUser
{	
	private:
		char	*mcUsername;
		char	*mcPassword;
		char	*mcName;
		char	*mcLastname;
	public:
		class cUser(){};

		void	_SetUserData(char *, char *, char *, char *);
		void	_SetUsername(char *);
		void	_SetPassword(char *);
		void	_SetName(char *);
		void	_SetLastname(char *);

		char	*_GetUsername();
		char	*_GetPassword();
		char	*_GetName();
		char	*_GetLastname();
};


The User.cpp
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
#include "math.h"
#include "string.h"
#include "stdlib.h"
#include "User.h"


void cUser::_SetUserData(char * lcName, char * lcLastname, char * lcUsername, char * lcPassword)
{
	mcName= new char[strlen(lcName)];
	mcLastname= new char[strlen(lcLastname)];
	mcUsername= new char[strlen(lcUsername)];
	mcPassword= new char[strlen(lcPassword)];

	strcpy(mcName, lcName);
	strcpy(mcLastname, lcLastname);
	strcpy(mcUsername, lcUsername);
	strcpy(mcPassword, lcPassword);
}


void cUser::_SetUsername(char *lcUsername)
{   
	delete[] mcUsername;
	mcUsername= new char[strlen(lcUsername)];
	strcpy(mcUsername, lcUsername);
}

void cUser::_SetPassword(char *lcPassword)
{
	delete[] mcPassword;
	mcPassword= new char[strlen(lcPassword)];
	strcpy(mcPassword, lcPassword);
}

void cUser::_SetName(char *lcName)
{
	delete[] mcName;
	mcName= new char[strlen(lcName)];
	strcpy(mcName, lcName);
}

void cUser::_SetLastname(char *lcLastname)
{   
	delete[] mcLastname;
	mcLastname= new char[strlen(lcLastname)];
	strcpy(mcLastname, lcLastname);
}

char * cUser::_GetUsername()
{
	return mcUsername;
}

char * cUser::_GetPassword()
{
	return mcPassword;
}

char * cUser::_GetName()
{
	return mcName;
}

char * cUser::_GetLastname()
{
	return mcLastname;
}
To sum it up, the error pops up as the code calls the _SetName/Username/Lastname/etc function's delete[]. Any help would be appreciated.

Would it be okay to replace the delete[] with an "=NULL"? Wouldn't this leave the allocated memory undeleted?
Last edited on
You don't initialize the pointers in the constructor so if you call _SetUsername, _SetPassword, _SetName or _SetLastname before you have called _SetUserData delete[] will be used on an unitialized pointer.
Last edited on
@Peter87: Thanks for replying. At the moment, _SetUserData is called when the program is started, so everything has a value before I go and try to change them with _SetUsername, _SetPassword, _SetName or _SetLastname.
When you allocate the arrays you don't allocate enough space for the null-terminating character '\0' that marks the end of the string.
How would I fix that? Doing mcName= new char[strlen(lcName+1)]; on _SetUserData's attributes?
Almost. Do +1 on the returned value of strlen instead of the string pointer ;)
Topic archived. No new replies allowed.