Re: allocating space for Node**
- From: Joseph M. Newcomer <newcomer@xxxxxxxxxxxx>
- Date: Wed, 04 Jan 2006 17:49:16 -0500
Gemerally, you should avoid this style of allocation. It is really quaint, resembling
methodologies used back in 1975 on the PDP-11. Use CArray, or std::vector, or some other
reliable mechanism.
See below...
On Tue, 3 Jan 2006 15:03:04 -0800, "kkirtac" <kkirtac@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote:
>hi, i have a problem with forming a node table;
>
>enum m_State{
> INITIAL,
> ACTIVE,
> EXPANDED
>};
>
>struct Node{
> Node* neighbor[8];
> double derivative[8];
> double linkCost[8];
> m_State state;
> double totalCost;
> Node* prevNode;
> int column, row;
> int index;
>};
>
>i want to form a table that each table member will be a Node, so i want to
>use Node** pointer as follows :
>
>Node** costTable;
>
>my image is pointed to by "source" pointer, to allocate space for the table i
>try the following :
>
>costTable = (Node **) new Node*[source->Height]; //referencing Node table
****
Why the cast? new is not malloc, and a cast is not required. In fact, it should be
treated as a coding error.
****
>*costTable = (Node *) new Node[source->Width];
****
OK, you've initialized one row. What happened to the Height number of rows?
for(int i = 0; i < source->Height; i++)
costTable[i] = new Node[source->Width];
The fact that you wrote *costTable is already incredibly suspicious as a programming
style. It essentially doesn't make any sense. It is inconsistent with what you wanted to
say.
I'd seriously suggest using std::vector instead of unchecked array acceses done with
pointers. CArray is a bit clumsy for 2D arrays because there's a problem with assignment
of CArray objects.
****
>
>i dont receive error in build time but receive error in runtime,here is the
>line where i receive the exception error
>
>for (int r=1; r<(bmpGray->Height)-1; r++)
>for (int c=1; c<(bmpGray->Width)-1; c++)
>{
>(costTable[r][c]).state = INITIAL; //<-this line causes error
>.................
>...............
>}
>
>:An unhandled exception of type 'System.NullReferenceException' occurred in
>Dijkstra's Shortest Path.exe
*****
And your point is? Given you haven't constructed the array elements, I'm surprised you
got something this informative. I would have expected a simple access fault. All you had
to do was look at the contents of the array with the debugger.
*****
>Additional information: Object reference not set to an instance of an object.
>
>i think i have made a mistake while allocating space for the table, so can
>anyone recommend a solution to correct the mistake?
Joseph M. Newcomer [MVP]
email: newcomer@xxxxxxxxxxxx
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
.
- Prev by Date: Re: move .exe
- Next by Date: Re: What is the SPAM Policy of these groups?
- Previous by thread: Re: allocating space for Node**
- Next by thread: preventing dialog box to be escapped
- Index(es):
Relevant Pages
|