heap corruption detected

hi everyone, I am new to this forum and beginners to c++. I am trying to do one program but stuck in the middle of the program. so please guys i desperately need your help.
The program i am trying to do is to read two columns integer data from file and put in two arrays . first column for one array and the other. I am going to paste my program too. but when i debug the program it will say heap corruption detected. i don't have any idea and solution. i hope guys have solution for that. i am going to paste my program below. thanking you all . have a good day.

_______________________________________________________________________________



#include <iostream>
#include <iomanip>
#include <fstream>

using namespace std;

int main()
{
int i,count;
//int *a, *b;

int *a = new int[];

int *b = new int[];

ifstream indata;

indata.open("yummies.txt");

if(!indata)
{
cout << "Can't Open a file. Please check the file again";
return 1;
}
count = 0;

while(!indata.eof())
{
indata >> a[count] >> b[count];

count++;
// count=count +1;//count the number of arrays
}

for(i=0;i<count;i++)
{
cout << a[i] << b[i];

}

indata.close();


delete [] a;
delete [] b;

return 0;

}
Your arrays have no size. Why don't you use a std::vector?
new int[] is rather meaningless and almost certainly wrong. You haven't said how large your array needs to be. If you think the maximum value of count will be less than 100, you can use:
int *a = new int[100];

or you can declare arrays:
int a[100];
If you do this, you won't need to release the memory at the end of the function, so you won't need delete [] a;
1
2
3
int *a = new int[];

int *b = new int[];


This is nonsense. You can't allocate an array without giving it a size.

In fact, that should even be giving you a compiler error.


The solution:

1) You either need to know the size of the array before you allocate.

or

2) You need to dynamically grow the array as you read the file. The easiest way to do this is not with arrays, but with std::vector.


EDIT: too slow!!!111
Last edited on
Thank you guys after reading this i figure it out. i really appreciated.
Topic archived. No new replies allowed.