Invalid Pointer Error

Hey, attempting to write a simple test building software, and keep running into a pointer error. I'm 100% sure its a problem with my use of the delete function, but I don't know how to fix it. I'll paste the code and the errors that valgrind gives. Thanks for your time :)

Code:


#include <iostream>
#include <sys/types.h>
#include <dirent.h>
#include <string>
#include <fstream>
#include <cstdio>

using namespace std;


//This class is used for holding all necessary information for a question and answer set-up
class Question
{
public:

void TestFunc()
{
cout << "Hello, World!\n";
}

void GetInfo(string FileName, string Sepparator)
{

}

string Subject;
string Class;
string Topic;
string Type;
int place;
};



int main()
{
char name[] = "MA--A2--QF--IM\0";
Question * test = new Question[10];
char sep[] = "--";
for (int x = 0;x < 10;x++)
{
(test + x)->TestFunc();
}
//test->TestFunc();//GetInfo(name, sep);//don't worry about this
delete test;
}

valgrind error:


==9203== Invalid free() / delete / delete[]
==9203== at 0x4024851: operator delete(void*) (vg_replace_malloc.c:387)
==9203== by 0x8048988: main (in /media/HP V125W/TestBuilder/TestBuilder)
==9203== Address 0x42da02c is 4 bytes inside a block of size 204 alloc'd
==9203== at 0x402532E: operator new[](unsigned int) (vg_replace_malloc.c:299)
==9203== by 0x804889B: main (in /media/HP V125W/TestBuilder/TestBuilder)
==9203==

Thanks again for your time :)
To release a dynamically allocated array it should look like this
delete [] test;

This calls the destructor for every object in the array, then deallocates the memory.
Topic archived. No new replies allowed.