I have a small class: header file - Grid.h:
class Grid
{
public:
  Grid();
  ~Grid();
  bool WriteGridToDSGrdFile(char* FileName);
  bool SetGrid(float *noisearray, int nx, int ny);
private:
  std::vector<std::vector<float>> GridData;
};
implementation - Grid.cpp
bool
Grid::SetGrid(float* noisearray, int nx, int ny)
{
  for (size_t ix = 0; ix < nx; ix++)
  {
    std::vector<float> row;
    GridData.push_back(row);
    GridData[ix].resize(ny);
    for (size_t iy = 0; iy < ny; iy++)
    {
      GridData[ix].push_back(noisearray[ix*ny + iy]);
    }
  }
  return true;
}
And then the calling routine (testgrid.cpp):
int _tmain(int argc, _TCHAR* argv[])
{
  int nx = 20; 
  int ny = 10;
  float *NoiseArray = new float[nx*ny];
  std::shared_ptr<Grid> NoiseGrid;
  NoiseGrid->SetGrid(NoiseArray, nx, ny);
  return 0;
}
While running in debug mode in MSVS 2013, in SetGrid GridData.push_back(row)goes bad and I am halted in vector in bool _Inside(const value_type *_Ptr) const.
Error message after run:
First-chance exception at 0x0028F249 in testgrid.exe: 0xC0000005: Access violation reading location 0x00000038. Unhandled exception at 0x0028F249 in testgrid.exe: 0xC0000005: Access violation reading location 0x00000038.
I have done these type of 2d manipulations before with great success. I have tried using resize and push_back with same result. Does anyone have a hint?
The problem lies with the lines:
std::shared_ptr<Grid> NoiseGrid;
NoiseGrid->SetGrid(NoiseArray, nx, ny);
From http://en.cppreference.com/w/cpp/memory/shared_ptr/shared_ptr:
The default constructor
Constructs a
shared_ptrwith no managed object, i.e. emptyshared_ptr
Use
std::shared_ptr<Grid> NoiseGrid(new Grid);
NoiseGrid->SetGrid(NoiseArray, nx, ny);
User contributions licensed under CC BY-SA 3.0