-1

I'm setting up a program that uses 'DFS' as a child class of 'Search', and calls its base constructor.

The program runs fine, until the breakpoint I placed just after calling the constructor, in which I receive the error:

Exception thrown: read access violation. _Pnext was 0xCCCCCCD0.

I've tried to summarise the code to only what is important.

in main.cpp:

DFS search(gridSize, startPoint, endPoints, walls);

in Search.h:

public:
    Search(unsigned int gridSize, unsigned int startPoint, unsigned int endPoints[2], vector<int> walls);

in Search.cpp:

#include "Search.h"

Search::Search(unsigned int gridSize, unsigned int startPoint, unsigned int endPoints[2], vector<int> walls)
{
    sGridSize = gridSize;
    sStartPoint = startPoint;
    sEndPoints[2] = endPoints[2];
    sWalls = walls;
}

In DFS.h:

#include "Search.h"

#include <vector>

public:
    DFS(unsigned int gridSize, unsigned int startPoint, unsigned int endPoints[2], vector<int> walls);

In DFS.cpp:

#include "DFS.h"

DFS::DFS(unsigned int gridSize, unsigned int startPoint, unsigned int endPoints[2], vector<int> walls) : Search(gridSize, startPoint, endPoints, walls)
{

}

If any more code is needed to understand this, just ask - I'm worried about pasting in too much.

Any help is greatly appreciated :)

Austin K.
  • 13
  • 3
  • @πάντα ῥεῖ Thats a poor duplicate. Its just a duplicate of the symptoms of something going wrong, not what actually went wrong. Just my 2c anyway. – Mike Vine Apr 24 '19 at 12:01
  • @MikeVine Well, it's along the old saying: [_"give a man a fish and you feed him for a day; teach a man to fish and you feed him for a lifetime"_](https://en.wiktionary.org/wiki/give_a_man_a_fish_and_you_feed_him_for_a_day;_teach_a_man_to_fish_and_you_feed_him_for_a_lifetime) – πάντα ῥεῖ Apr 24 '19 at 12:04

1 Answers1

2

With sEndPoints[2] = endPoints[2] you access the third element of the two-element arrays. And you assign only that value.

The C++ solution is to use std::array instead of "old" C-style arrays. Or std::vector.

And if you don't want to do that then the solution is to use constructor initializer lists where you initialize the member variables instead of assigning to them. For example

Search::Search(unsigned int gridSize, unsigned int startPoint, unsigned int endPoints[2], vector<int> walls)
    : sGridSize(gridSize), sStartPoint(startPoint), sEndPoints(endPoints), sWalls(walls)
{
}
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621