0

I have something like the follow:

template<class T>
struct point{
   point* next = nullptr;
   T* data = nullptr;
}

template<class T>
class newClass
{
   point<T>*** points;

public:
   newClass()
   {
      points = new point<T>**[10];
      for (int i = 0; i < 10; i++)
          points[i] = new point<T>*[10];

      for (int i = 0; i < 10; i++)
          for(int j = 0; j < 10; j++)
             if(j != 9)
                points[i][j]->next = points[i][j+1]; //ERROR
   }
};

Can someone help me understand why this produces an error? I don't receive an error message, the program just crashes.

4
  • 7
    Please don't be Three Star Programmer Commented Oct 30, 2015 at 13:39
  • 1
    Why not just use std::vector et al rather than re-inventing the wheel ? Commented Oct 30, 2015 at 13:41
  • Why make a simple linked list of the nodes, when you already have all of them in a container? You already know that the "next" node of points[a][b] will be points[a][b+1]. Commented Oct 30, 2015 at 13:44
  • points[i][j] is not initialized Commented Oct 30, 2015 at 14:10

3 Answers 3

3

When you do points[i][j]->next you dereference an uninitialized pointer which leads to undefined behavior.

Drop one level of indirection and do

points[i][j].next = &points[i][j+1];

Or, do an extra allocation loop to initialize that last pointer too.


For a different way, you don't need to use pointers at all here, especially since your allocations are all of a fixed size, which means you can use fixed arrays, or even better, std::array:

std::array<std::array<point<T>, 10>, 10> points;
Sign up to request clarification or add additional context in comments.

Comments

0

The type of expression

points[i][j]

is point<T> *. This pointer was not initialized. It has indertermined value. At first you need to allocate an object that will be pointed to by this pointer. For example

points[i][j] = new point<t>();

Otherwise this

points[i][j]->next 

refers nowhere.

I think you mean the following

template<class T>
class newClass
{
   point<T> **points;

public:
   newClass()
   {
      points = new point<T>*[10];
      for (int i = 0; i < 10; i++)
          points[i] = new point<T>[10]();

      for (int i = 0; i < 9; i++)
                points[i]->next = points[i+1];
   }
};

2 Comments

Isn't points[i] and array of points? If points[i] has 10 elements, I want each of them to have a "next" basically.
@KalibiddJlddkO I am sorry. I have not understood what you asked. Do you need a one-dimensional array of dynamically allocated objects of type point<T> or do you need a two-dimensional array of dynamically allocated objects of type point<T>?
0

You have too many pointer operators. The code below will work...

#include <iostream>

using namespace std;

template<class T>
struct point {
    point* next = nullptr;
    T* data = nullptr;
    T* operator->() {
        return data;
    }
};

template<class T>
class newClass
{
    point<T>** points;

public:
    newClass()
    {
        points = new point<T>*[10];
        for (int i = 0; i < 10; i++) {
            points[i] = new point<T>[10];
        }

        for (int i = 0; i < 10; i++) {
            for (int j = 0; j < 10; j++) {
                if (j != 9) {
                    points[i][j].next = points[i][j + 1].next; //ERROR
                }
            }
        }
    }
};

int main()
{
    newClass<int> nc;

    return 0;
}

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.