| | |
Dynamic array of pointers to structures - doubt
![]() |
•
•
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.
In case you were wondering, yes, I do hate you.
•
•
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: 3593 | Replies: 3
| Thread Tools | Search this Thread |
Tag cloud for C++
6 algorithm array arrays assignment beginner binary browser c++ c/c++ calculator char class classes code compile compiler constructor conversion convert count delete dll dynamic encryption error file files filestream forms fstream function functions game givemetehcodez graph graphics gui homework iamthwee input int lazy link linker list loop looping loops math matrix member memory newbie number object objects opengl operator output parameter path pointer pointers problem program programming project python random read reading recursion recursive reference server sort spoonfeeding string strings struct student studio template templates text time tree undefined url variable vc++ vector video visual win32 window windows winsock wxwidgets






