Dynamic Arrays

I am having trouble with this problem, I get an error "Invalid address specified to RtlValidateHeap", although the program works as intended it just shows that error at the end. Below I have my constructor for my class which holds an array, I think the "list = new Customer" is where my problem is but I can't figure out how to make it work. Could somebody please point me in the right direction? Please let me know if I need to post more code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
Stack::Stack(char* name, char* email)
{
	size = 0;
	list = new Customer;
	int len = strlen(name) + 1;
	list[0].contactName = new char[len];
	strcpy(list[0].contactName, name);

	len = strlen(email) + 1;
	list[0].emailAddress = new char[len];
	strcpy(list[0].emailAddress, email);

	size++;
}
this being the constructor (sorry about earlier) I think the bug is elsewhere.
you have a minor oddity: size = 0 and ++ ... set size = 1 or 0, whichever you expect it to be after construction.

try running in your debugger to see exactly where it blows up, use the call stack to get back to your code if it is dying in a compiler provided file.
Last edited on
> list = new Customer;
You use this like an array, are you doing
delete [] list;
in the destructor?

If you are, this constructor needs to be
list = new Customer[1];
as long as its [0] he is fine without that.
if he tries [1] or more, you are right.

but *thing vs thing[0] has no difference to the compiler.
I did this a lot way back because the *s were interfering with math code appearance.
Last edited on
Why not: list->contactName


Name of class, "Stack", hints that this object might contain "a stack of Customers". That is "more than one". However, you do initialize it with data for only one Customer and you do create a Customer, not an array that has one Customer.

If everything else in Stack assumes that list points to an array, then of course things will (hopefully) crash and burn.
Topic archived. No new replies allowed.