1

I'm trying to implement an array of lists, but have some problems with dynamic memory allocation, or I`ve not clearly enough understood pointers yet;/. Here's my code:

struct List {
    List * next;
    char * tab1;
    char * tab2;
};

int size =4000;
List ** array= new List*[size];

void add(char * table_one, char * table_two)
{
    int n=40;
    for(int cc =0; cc<size; cc++)
    {
        array[cc]->tab1 = new char[n];
        array[cc]->tab2 = new char[n];
    }
    ...
}

Unhandled exception error occurs, when i`m trying to allocate memory for tab1 and tab2:

array[cc]->tab1 = new char[n];
array[cc]->tab2 = new char[n];

I have no idea what's wrong in these lines:( Appreciate any hints!

user703016
  • 37,307
  • 8
  • 87
  • 112
aggy
  • 13
  • 3
  • 1
    You are putting char buffers into `List` instance. But is this one particular `List[cc]` allocated? This example is bit complicated even for me. Why List has pointer to next List? – Agent_L May 27 '14 at 13:53
  • you have created a list of pointers in `array`, but where are they pointing? – Pavel May 27 '14 at 13:53
  • 1
    [Too many pointers.](http://klmr.me/slides/modern-cpp) Use proper C++ instead. – Konrad Rudolph May 27 '14 at 13:56

2 Answers2

1
List ** array= new List*[size];

shows that you're dynamically creating an array of List* while here

array[cc]->tab1

without allocating a List to array[cc] you're trying to dereference a rogue pointer which invokes undefined behaviour. You should either allocate memory for those pointers

array[cc] = new List;

or change List ** array= new List*[size]; into List *array= new List[size]; so that you directly create Lists instead of List*s.


I think the confusion stems from not understanding multiple levels of pointers (concept of recursion). When you allocate a pointer to, say, int, you still don't have an integer space to store integer values, all you've done is allocated a pointer p (int pp** = new int*) which you're pointing to with a pointer-to-pointer pp, now you've to allocate an integer for the pointer to point to (new int). Then you can store values in the allocation you just made.

For more info read this post.

Community
  • 1
  • 1
legends2k
  • 31,634
  • 25
  • 118
  • 222
  • Thank you for explaining those (damn!) pointers. It really helps me a bit:) And I`ll surely read the post from the link. Thanks again! – aggy May 27 '14 at 14:15
1

Your array is actually an array of pointers so you have to initialise each element. Alternatively you want an array of List objects.

The solution for the first alternative is to change your for-loop into the following:

for(int cc=0; cc<size; cc++)
{
    array[cc] = new List(); // Add this step
    array[cc]->tab1 = new char[n];
    array[cc]->tab2 = new char[n];
}

The solution for the second alternative is to change the type of your array variable.

List * array= new List[size];

and then use . (a dot) instead of -> (arrow).

for(int cc=0; cc<size; cc++)
{
    array[cc].tab1 = new char[n];
    array[cc].tab2 = new char[n];
}
Mats Fredriksson
  • 19,783
  • 6
  • 37
  • 57
  • First solution works fine. Thank you for that:) But I have a problem with second one. Got _Error 2 error C2679: binary '=' : no operator found which takes a right-hand operand of type 'List *'_ at `array[i] = array[i].next;` – aggy May 27 '14 at 14:06
  • 1
    @aggy you're assigning a pointer `List*` to an object of type `List` hence the error, insteda you should do `array[i] = *array[i].next;` where the `*` operator will give the object at the location pointed to by `array[i].next`. – legends2k May 27 '14 at 15:08