| | |
Dynamic array of pointers to structures - doubt
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Sep 2008
Posts: 62
Reputation:
Solved Threads: 0
Hi,
I need to implement a dynamic array of pointers to structures.Here's what I've done
Firstly, am I right?
Now when I use this in a function..
I get Segmentation Fault.I'm pretty sure im mucking up pointers somewhere.Any help?
I need to implement a dynamic array of pointers to structures.Here's what I've done
C++ Syntax (Toggle Plain Text)
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..
C++ Syntax (Toggle Plain Text)
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?
>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.
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.
New members chased away this month: 5
•
•
Join Date: Sep 2008
Posts: 62
Reputation:
Solved Threads: 0
ohhh...I did not allocate memory for each pointer to hold a node...so would that be something like...
???
C++ Syntax (Toggle Plain Text)
for(i=0;i<length;i++) A[i]=new node
![]() |
Similar Threads
Other Threads in the C++ Forum
- Previous Thread: Help! I need a little help starting this project its my final
- Next Thread: Looping Behavior
Views: 2746 | Replies: 3
| Thread Tools | Search this Thread |
Tag cloud for C++
6 api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code compile compiler console conversion convert count data delete deploy dll download dynamiccharacterarray encryption error file format forms fstream function functions game givemetehcodez graph homeworkhelp iamthwee ifstream input int java lib library lines list loop looping loops map math matrix memory newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg search simple sort sorting spoonfeeding string strings struct temperature template templates text tree url variable vector video visual visualstudio void win32 windows winsock wordfrequency wxwidgets






