Dynamic array of pointers to structures - doubt

Reply

Join Date: Sep 2008
Posts: 62
Reputation: AutoC is an unknown quantity at this point 
Solved Threads: 0
AutoC AutoC is offline Offline
Junior Poster in Training

Dynamic array of pointers to structures - doubt

 
0
  #1
Sep 11th, 2008
Hi,

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

  1. struct node{
  2. int freq;
  3. node *lptr;
  4. node *rptr;
  5. };
  6.  
  7. class M
  8. {
  9. private:
  10. node **A;
  11. int length;
  12. public:
  13. M()
  14. {
  15. length=10;
  16. A=new node*[10];
  17. }
  18. M(int a)
  19. {
  20. length=a;
  21. A=new node*[a];
  22. }

Firstly, am I right?

Now when I use this in a function..

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

I get Segmentation Fault.I'm pretty sure im mucking up pointers somewhere.Any help?
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 8,313
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 824
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Dynamic array of pointers to structures - doubt

 
1
  #2
Sep 11th, 2008
>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.
In case you were wondering, yes, I do hate you.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 62
Reputation: AutoC is an unknown quantity at this point 
Solved Threads: 0
AutoC AutoC is offline Offline
Junior Poster in Training

Re: Dynamic array of pointers to structures - doubt

 
0
  #3
Sep 11th, 2008
ohhh...I did not allocate memory for each pointer to hold a node...so would that be something like...
  1. for(i=0;i<length;i++)
  2. A[i]=new node
???
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 8,313
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 824
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Dynamic array of pointers to structures - doubt

 
1
  #4
Sep 11th, 2008
That's it exactly.
In case you were wondering, yes, I do hate you.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 3593 | Replies: 3
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2010 DaniWeb® LLC