0

Hi,

I need to implement a dynamic array of pointers to structures.Here's what I've done

struct node{
        int freq;
        node *lptr;
        node *rptr;
};

class M
{
        private:
        node **A;
        int length;
        public:
        M()
        {
                length=10;
                A=new node*[10];
        }
        M(int a)
        {
                length=a;
                A=new node*[a];
        }

Firstly, am I right?

Now when I use this in a function..

int i;
 for(i=0;i<length;i++)
  A[i]->freq=i;

I get Segmentation Fault.I'm pretty sure im mucking up pointers somewhere.Any help?

2
Contributors
3
Replies
6
Views
9 Years
Discussion Span
Last Post by Narue
0

>Firstly, am I right?
So far, yes.

>I get Segmentation Fault.
Did you allocate memory to the individual pointer or just the array? There are two steps here, first you allocate memory for the pointers in the dynamic array, then you allocate memory to each pointer to hold a node instance.

0

ohhh...I did not allocate memory for each pointer to hold a node...so would that be something like...

for(i=0;i<length;i++)
A[i]=new node

???

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.