I am trying to get a program I wrote to work but I am having a problem. The program is supposed to make use of multiple levels of indirection so that I can get more familiar and comfortable with pointers, but when I run it I get the error: "Debug Assertion Failed! Expression: _BLOCK_TYPE_IS_VALID(pHead->nBlockUse).
The program is supposed to take a file, read the input from it into an array and a vector, then output the data. The vector is to be accessed by a pointer to a pointer to a pointer to the vector, and the array by a pointer to a pointer to the array.
Here is part of the program; I am fairly certain that the error is in here. The rest just outputs the data and deletes the dynamic memory. I may be doing the dereferencing incorrectly at some point; I'm not sure. If someone could take a look and point me in the right direction, that'd be great.
Code:
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <new>
using namespace std;
bool readInput(vector<string>***, string**, int);
bool allocateArray(string**, int&, int);
int main()
{
vector<string>*** p = new vector<string>**; //Defines the pointers to the vector
*p = new vector<string>*;
string** r = new string*; //Defines the pointers to the array
int size = 10; //For the array size
bool flag = true;
flag = readInput(p, r, size);
//The rest is omitted
}
// Defines the vector, calls the allocateArray function, and puts the data
//into the vector and array.
bool readInput(vector<string>*** vec, string** arr, int s)
{
bool flag = true;
int count = 0;
ifstream inFile;
string name, input;
inFile.open("TopicAin.txt");
while (!inFile)
{
cout << "Please enter a valid file name" << endl;
cin >> name;
inFile.open(name);
}
**vec = new vector<string>; //Define the vector
flag = allocateArray(arr, s, count); //Define the array
if (!flag)
{
cout << "Error allocating array." << cout;
return false;
}
while(!(inFile.eof()))
{
inFile >> input;
(***vec).push_back(input); //Put the word into the vector
if (count == s) //Call the allocateArray function if the array is full
{
flag = allocateArray(arr, s, count);
if (!flag)
{
cout << "Error allocating array." << cout;
return false;
}
}
*((*arr) + count) = input; //Put the word into the array
count++;
}
if(inFile.fail())
flag = false;
inFile.close(); //Close the file
return flag;
}
//Allocates the array and reallocates it as necessary
bool allocateArray(string** arr, int &size, int count)
{
if (count == 0) //Runs the first time through
{
*arr = new (nothrow) string[size];
if (*arr == 0) //Tests if the allocation was successful
return false;
else
return true;
}
if (count != 0) //Runs every time after the first
{
string *ptr = new string[size];
for (int i = 0; i < size; i++) //Allocates a new array to hold the current words
ptr[i] = *((*arr) + i);
delete *arr;
size *= 2;
*arr = new string[size]; //Allocates a new array double the size of the old one
for (int i = 0; i < (size/2); i++) //Puts the words back into the array
*((*arr) + i) = ptr[i];
delete ptr;
}
if (*arr == 0) //Tests if allocation was successful
return false;
else
return true;
}