Re: allocating space for Node**

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



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
.



Relevant Pages

  • Re: allocating memory for array of pointers to char
    ... >> checking up on allocation and so on, the types of your objects are ... > pointer inside a linked-list element. ... > typedef struct listelem_t { ... "value" is a pointer to an array of unknown length. ...
    (comp.lang.c)
  • Re: Newbie on the lose.. How to add an unknown length dataset to an array
    ... allocation, it does require enough virtual memory to hold twice the ... consisting of one of these records plus a pointer. ... Having read all the data into the list, I know the size of the array ... and your pointer method makes good sense from since it dosen't ...
    (comp.lang.fortran)
  • Re: Ancient history
    ... If a pointer holds origin, ... > segment of memory wholly contained within a single allocation ... > into one allocation into a pointer into another. ... dimensional array to a subroutine with a one dimensional dummy argument. ...
    (sci.crypt)
  • Re: Passing allocatable arrays between Fortran and C
    ... then passed back to the calling Fortran code. ... pass an array descriptor or dope vector, which is a structure containing a pointer ... One variant of it is to pass a pointer ") to C and do the allocation ...
    (comp.lang.fortran)
  • Re: Newbie on the lose.. How to add an unknown length dataset to an array
    ... allocation than with the reallocation approach. ... when I'm reading data of unknown size into an array, ... consisting of one of these records plus a pointer. ...
    (comp.lang.fortran)