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!

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? Commented May 27, 2014 at 13:53
  • you have created a list of pointers in array, but where are they pointing? Commented May 27, 2014 at 13:53
  • 1
    Too many pointers. Use proper C++ instead. Commented May 27, 2014 at 13:56

2 Answers 2

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];
}
Sign up to request clarification or add additional context in comments.

2 Comments

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 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.
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.

1 Comment

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!

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.