Hello, am new to this so please bear with me. I'm working on a windfarm simulator in C++, it has to read in data from a .csv file and calculate various power values. Anyway that's beside the point. The problem I'm having with is the 2 Dimensional Dynamic Array that I created to store the values as the size of each windfarm file varies..

The program runs through the .csv file once to count how big to make the array then goes through it again taking each value in tern and stores in in the array it just created.

From what I can gather it is creating the array and also storing the values but when I try to output the array it doesn't seem to work. I get the following error -

Unhandled exception at 0x60fd942c in Windfarm.exe: 0xC0000005: Access violation reading location 0xcdcdcde5.

I think it hs something to do with trying to read memory from outside the array but I have no idea why it would be doing this. Here is my code

#include <iostream>
#include <string>
#include <math.h>
#include <fstream>
#include <sstream>
using namespace std;

int main (){

	string windspeeddate, filepath, temp, **windarray ;
	int windspeed= 0,rowcounter = 0, colcounter = 1 ,row,col;
	
        cout << " Enter the name of the windspeed data .csv file \n";
	cin >> filepath;

	ifstream inFile;
	inFile.open(filepath.c_str());

	if (!inFile){

		cout << " The file did not open correctly \n";
		return -1;}

	while (!inFile.eof()){
		getline (inFile, temp , ',');
		rowcounter++
}

	cout << " The .csv file contains " << rowcounter <<endl;
 try
  {
    	windarray = new string *[rowcounter];
	windarray[rowcounter]= new string [colcounter];
  }
  catch (bad_alloc& ba)
  {
    cerr << "bad_alloc caught: " << ba.what() << endl;
  }

	
	while (!inFile.eof()){
		getline (inFile, temp , ',');
		for ( row = 0; row<rowcounter;row++){for ( col = 0;col<colcounter;col++){
			cout << temp << endl;
			windarray[row][col] = temp;}
      }
}
              for ( row = 0; row<rowcounter;row++){
			for ( col = 0;col<colcounter;col++)
				{cout <<windarray[row][col]<< "		";
                }
		cout<< endl;
	}
	system("PAUSE");
}

Have been looking around for a solution but can't seem to figure it out. Any help from you guys would be really appreciated. If you need me to add anything to this please just ask. I've attached below the the .csv file in a .txt file format.

Thank You

Kungu

Attachments
Sat Aug 11 20:00:00 BST 2001,9.774444436
-99999,-99999
Sat Aug 11 22:00:00 BST 2001,8.231111104
Sat Aug 11 23:00:00 BST 2001,7.71666666
Sun Aug 12 00:00:00 BST 2001,8.231111104
Sun Aug 12 01:00:00 BST 2001,8.231111104
Sun Aug 12 02:00:00 BST 2001,8.231111104
Sun Aug 12 03:00:00 BST 2001,8.745555548
Sun Aug 12 04:00:00 BST 2001,10.28888888
Sun Aug 12 05:00:00 BST 2001,11.317777768
Sun Aug 12 06:00:00 BST 2001,11.832222212
Sun Aug 12 07:00:00 BST 2001,12.346666656
Sun Aug 12 08:00:00 BST 2001,13.375555544000001
Sun Aug 12 09:00:00 BST 2001,13.375555544000001
Sun Aug 12 10:00:00 BST 2001,14.404444432
Sun Aug 12 11:00:00 BST 2001,12.8611111
Sun Aug 12 12:00:00 BST 2001,14.918888876
Sun Aug 12 13:00:00 BST 2001,12.8611111
Sun Aug 12 14:00:00 BST 2001,13.375555544000001
Sun Aug 12 15:00:00 BST 2001,15.43333332
Sun Aug 12 16:00:00 BST 2001,14.918888876
Sun Aug 12 17:00:00 BST 2001,15.947777764
Sun Aug 12 18:00:00 BST 2001,15.43333332
Sun Aug 12 19:00:00 BST 2001,12.346666656
Sun Aug 12 20:00:00 BST 2001,12.346666656
Sun Aug 12 21:00:00 BST 2001,-99999.0
Sun Aug 12 22:00:00 BST 2001,12.346666656
Sun Aug 12 23:00:00 BST 2001,10.28888888
Mon Aug 13 00:00:00 BST 2001,8.745555548
Mon Aug 13 01:00:00 BST 2001,9.774444436
Mon Aug 13 02:00:00 BST 2001,6.6877777720000005
Mon Aug 13 03:00:00 BST 2001,5.658888884
Mon Aug 13 04:00:00 BST 2001,6.173333328
Mon Aug 13 05:00:00 BST 2001,8.231111104
Mon Aug 13 06:00:00 BST 2001,9.774444436
Mon Aug 13 07:00:00 BST 2001,7.71666666
Mon Aug 13 08:00:00 BST 2001,6.6877777720000005
Mon Aug 13 09:00:00 BST 2001,6.6877777720000005
-99999,-99999

Do you know about vectors? If you use a 2d-vector ( vector<vector<string> > )instead of an 2d array, you wouldn't have to read the filesize before reading the data. You can just push them into the vector as you go, so your problem with running into un-allocated memory will disappear to.

Comments
Proper solution. :D

First : As pointed out there are vectors of vectors. That is ok.
But often you want a 2d array, i.e each column is the same length.
So try the boost::multi_array. Lots better.

Second : you can do what you want with pointers/new/delete etc BUT you have to get it right:

You have

// CODE WITH ERROR:
 windarray = new string *[rowcounter];
windarray[rowcounter]= new string [colcounter];

That is a memory violation since you correctly allocate rowcounter pointers to point to the string giving you an array of [0 to rowcounter-1] but THEN allocate to rowcounter....

You need this:

windarray = new string *[rowcounter];
windarray[0]= new string [rowcounter*colcounter];
for(int i=1;i<rowcounter;i++)
   windarray[i]=windarray[0]+colcounter*i;

NOTE: to delete do this

delete [] windarray[0];
delete [] windarray;

There are other versions of this and you should keep in mind that you can't mix and match creation forms and delete forms.

Finally, please layout your code with more consistent line breaks it will make it much easier for you to read/debug/learn from/deal with in 2 years time. E.g put each for loop on a new line, and then indent. But whatever you choose BE CONSISTENT.

This article has been dead for over six months. Start a new discussion instead.