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

Recommended Answers

All 2 Replies

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.

commented: Proper solution. :D +1

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.