2

Why am I getting a segmentation fault in the following code?

    struct Cell
    {
        cellMode mode;
        bool visited;
        //bool scanned;
        int rowIndex;
        int colIndex;
        Cell *neighbours;//if using Cell neighbours[3] i am getting a compilation error

        Cell()
        {
            neighbours = new Cell[3];//seg fault here
        }
    };

When I use a static array, I get the following error

neighbours has incomplete type

2
  • 3
    Every time a "Cell" is allocated, its constructor is invoked. This means that the first constructor never exits since each time you're allocating three more. Commented Jun 17, 2013 at 21:01
  • 2
    Just make neighbours pointers to Cell and with a function outside of the constructor create them with "new" or create them one by one when you need them. Commented Jun 17, 2013 at 21:06

3 Answers 3

6

If you new 3 Cells in Cell's constructor you are calling 3 more Cell's constructors. Each of those 3 then calls 3 more, and so on. Until, a... wait for it... stack overflow. Hurrah. It's infinite.

Sign up to request clarification or add additional context in comments.

2 Comments

so How can I avoid this recursion ??
Make neighbours an array of 3 pointers instead as it makes no sense for a cell to have 3 actual other cells within it. Then in your constructor or elsewhere, link your pointers up.
5

You wrote an infinitely recursive function. Your Cell::Cell constructor indirectly calls itself thorough new expression, which in turn calls Cell::Cell again, again and again... This eventually leads to stack overflow and crash.

(Also, it is not possible to have an explicit array of Cell objects inside a Cell object. Such a data structure would be infinitely nested, i.e. it would have infinite size.)

Note that there's nothing generally wrong with recursion per se. But it has to end at some point. If you want to build your tree recursively, you can do so, but you have to make sure your recursion bottoms-out at one point or another. I.e. your tree has to have leaf nodes. What you have now is a tree without leaf nodes. It is an infinite recursion that attempts to build an infinitely large tree. This is not possible. It makes no sense.

When it is time to build your leaf nodes - only you can answer. We don't know what kind of tree your are trying to build.

6 Comments

ok in a tree data structure each node has node pointers to children nodes, I have the same concept here, how can I avoid such recursion ??
@Ahmed Kato: You can do it by taking new out of constructor. First, finish your constructor. Then you can allocate nodes.
I did something and it is working, making a new contructor that takes the number as argument so that when calling the default constructor their will be no recursion.
@Ahmed Kato: You can do it that way too. In can keep it recursive if you want, but you have to make sure the recursion ends eventually. What you have now attempts to build an infinitely large tree. This is not possible.
thank you, it is working fine now,a function not a constructor and then call the function, simple as it is =)
|
0

Since the constructor is calling itself three times per instance, you are basically creating an infinite loop due to never-ending recursion. That's where your problem is.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.