Assertion Failure with cout and class methods!

Hello, I'm having a problem with a programming project of mine. To keep it short and simple, one of my functions in my OWN custom class called vector (it's what my professor wanted despite C++ already having a vector class...) which basically implements an array and does various things such as removing the last element, inserting an element in numerical order, and so on. All the functions work fine, but if I want to print a value on the screen (using cout of course), it gives me an assertion error (and I've made sure that these are non-void functions that I'm using in conjunction with cout).

Here's a few of the functions the implementation code file that's causing the problem:

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
int vector::getItem(int at) {
	if(!isEmpty() && at < count) {
		return data[at];
	} else {
		return -1;
	}
}

int vector::length() {
	return count;
}

int vector::removeFrom(int pos) {
	int result = 0;
	if(!isEmpty() && pos < count) {
	result = data[pos];
	for(int i=pos; i<count; i++) {
		data[i] = data [i+1];
	}
	count--;
	return result;
	}else {
		return -1;
	}
}


NOTE: *data is an integer pointer, and count is the number of elements in the vector. Both are private class members of the "vector" class.

Here's the code for the driver/test program:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include "vector.h"
using namespace std;

int main() {
	vector v(10);
	int foo[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
	int listNum = 10;
	for(int i=0; i<listNum; i++) {
		v.insertAtBack(foo[i]);
	}
	cout << v.removeFromBack() << endl;
	return 0;
}


That darn cout statement is what's causing the ruckus, but if I simply remove the cout and JUST call the "removeFromBack()" function, everything goes "apparently" smooth.

Also, the error message I'm getting is:

Debug Assertion Failed!

Program: g:\hooray\debug\hooray.exe

File: f:\\dd\vtools\crt_bld\self_x86\crt\src\dbgheap.c

Line: 1317

Expression: _CrtIsValidHeapPointer(pUserData)


Any help would be greatly appreciated!
It smells of a memory management problem inside vector. Can you post vector's definition and also the implementation of vector's constructors/destructors?
Sure, no problem. Here's the constructor part of the .h file code (there's two of them):

1
2
3
4
class vector {
public:
   vector();
   vector(int max);


...And here is the constructor implementation:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include "vector.h"
using namespace std;

const int MAX_SIZE = 100;

vector::vector() {
	maxSize = MAX_SIZE;
	data = new int(maxSize);
	count = 0;
}

vector::vector(int max) {
	maxSize = max;
	data = new int(maxSize);
	count = 0;
}
you probably meant to do new[] not new.

1
2
3
4
data = new int[maxSize];  // note, [], not ()

// also then be sure to use delete[] for cleanup:
delete[] data;  // note the brakets, here 
Ah, it worked. Thank you (darn subtle syntax...)!
Topic archived. No new replies allowed.