• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 4673
  • Last Modified:

"local variable".. "used without having been initialized"?

I am getting an error ...node0.c(33) : warning C4700: local variable 'node0' used without having been initialized
but I thought I did initialize it?  Part of my code is shown below...
____________________________________________________________________________________________

#include "prog3.h"
#include <stdio.h>

extern struct rtpkt {
  int sourceid;       /* id of sending router sending this pkt */
  int destid;         /* id of router to which pkt being sent
                         (must be an immediate neighbor) */
  int mincost[4];    /* min cost to node 0 ... 3 */
  };

extern int TRACE;
extern int YES;
extern int NO;

struct distance_table
{
  int costs[4][4];
} dt0;

      printdt0(distance_table);
/* students to write the following two routines, and maybe some others */
/************************************************************************************************************************************/
/* rtinit0() -- This routine will be called once at the beginning of the emulation. rtinit0() has no arguments. */
void rtinit0()
{
      struct rtpkt *pkt2send;
      struct distance_table *node0;
      int i, j;
/* default the distance table by setting the cost to infinity (999)). */

      for (i=0; i<=3; i++){
            for (j=0; j<=3; ++j)
                  node0->costs[i][j] = 999;
      }

/* Initialize the distance table reflect the direct costs */
      node0->costs[0][0] = 0;
      node0->costs[1][1] = 1;
      node0->costs[2][2] = 3;
      node0->costs[3][3] = 7;
      
/* Finding the mincost from the distance table */
      for (i=0; i<=3; i++){
      pkt2send->mincost[i] = 999; // default mincost
      }
      for (i=0; i<=3; i++){
            for (j=0; j<=3; ++j){
                  if (node0->costs[i][j]<pkt2send->mincost[i]){
                        pkt2send->mincost[i] = node0->costs[i][j];
                  }
            }
      }

/* Then send its distance vector directly connected neighbors the cost of it minimum cost paths to all other network nodes. */
            pkt2send->sourceid = 0;
            pkt2send->destid = 1;
            tolayer2(pkt2send);
            pkt2send->destid = 2;
            tolayer2(pkt2send);
            pkt2send->destid = 3;
            tolayer2(pkt2send);
            
      printdt0(node0);
}


/************************************************************************************************************************************/
/* rtupdate0(struct rtpkt *rcvdpkt) -- This routine will be called when node 0 receives a routing packet (distance vector) from its
   directly connected neighbors.  */

void rtupdate0(rcvdpkt)
  struct rtpkt *rcvdpkt;
{
      int k, i, source;
      struct rtpkt *pkt2send;
      struct distance_table *node0;

/* the received distance vectors are used to update its own distance table (as specified by the distance vector algorithm). */
      source = rcvdpkt->sourceid;
      for (i=0; i<=3; i++){
            node0->costs[i][source] = node0->costs[i][source] + rcvdpkt->mincost[i];
      }

/* Finding it's minimum cost paths to all other neighboring nodes
   (the distance vector). */
      k = 0; //check to see if the distance vector gets updated
      for (i=0; i<=3; i++){
            if (pkt2send->mincost[0]<node0->costs[0][i]) {
                  pkt2send->mincost[0]=node0->costs[0][i];
                  k = 1;
            }
            if (pkt2send->mincost[1]<node0->costs[1][i]) {
                  pkt2send->mincost[1]=node0->costs[1][i];
                  k = 1;
            }
            if (pkt2send->mincost[2]<node0->costs[2][i]) {
                  pkt2send->mincost[2]=node0->costs[2][i];
                  k = 1;
            }
            if (pkt2send->mincost[3]<node0->costs[3][i]) {
                  pkt2send->mincost[3]=node0->costs[3][i];
                  k = 1;
            }
      }

/* If the distance vector is updated, then
   This distance vector is sent to neighboring nodes in a routing packet by calling the routine tolayer2()*/
      if ( k = 1) {
            pkt2send->sourceid = 0;
            pkt2send->destid = 1;
            tolayer2(pkt2send);
            pkt2send->destid = 2;
            tolayer2(pkt2send);
            pkt2send->destid = 3;
            tolayer2(pkt2send);
      }

      printdt0(node0);
}

/************************************************************************************************************************************/
/* This routine will print the distance table for node 0 in a pretty way. It is passed a pointer to a structure of type distance_table.
   printdt0() and the structure declaration for the node 0 distance table are declared in the file node0.c. Similar pretty-print
   routines are defined for you in the files node1.c, node2.c node3.c. */

printdt0(dtptr)
  struct distance_table *dtptr;
 
{
  printf("                via     \n");
  printf("   D0 |    1     2    3 \n");
  printf("  ----|-----------------\n");
  printf("     1|  %3d   %3d   %3d\n",dtptr->costs[1][1],
       dtptr->costs[1][2],dtptr->costs[1][3]);
  printf("dest 2|  %3d   %3d   %3d\n",dtptr->costs[2][1],
       dtptr->costs[2][2],dtptr->costs[2][3]);
  printf("     3|  %3d   %3d   %3d\n",dtptr->costs[3][1],
       dtptr->costs[3][2],dtptr->costs[3][3]);
}



0
VWrestle97
Asked:
VWrestle97
2 Solutions
 
sunnycoderCommented:
 struct distance_table *node0;
    int i, j;
/* default the distance table by setting the cost to infinity (999)). */

    for (i=0; i<=3; i++){
         for (j=0; j<=3; ++j)
              node0->costs[i][j] = 999;

when you declare node0, it has some junk value ... immediately after that, you use it in a loop !!!!

struct distance_table *node0 = NULL;

initialized ... but next you need to put some valid value in it or code will seg fault
0
 
sunnycoderCommented:
 struct distance_table *node0;

/* the received distance vectors are used to update its own distance table (as specified by the distance vector algorithm). */
    source = rcvdpkt->sourceid;
    for (i=0; i<=3; i++){
         node0->costs[i][source] = node0->costs[i][source] + rcvdpkt->mincost[i];
    }

same here
0
 
_nn_Commented:
node0 is possibly meant to point to dt0, so I guess it should be

struct distance_table *node0 = &dt0;

0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
VWrestle97Author Commented:
sunnycoder, I tried your suggestion but I don't think that it worked correctly... I have more code... perhaps ... if you can, you look at my full code... is there a way that I can send it you so that you can take a better look at it?
0
 
sunnycoderCommented:
you need to initialize it to a struct distance table .. if you have just one distance table (global one), then try _nn_ suggestion
declare node0 as
struct distance_table *node0 = &dt0;

in functions void rtinit0() and void rtupdate0(rcvdpkt)

if that does not work, post your code here ... if it is too big to be posted, my mail id is in my profile
0
 
g0rathCommented:
but that is global so everyone can modify it's contents, if you are just wanting a local one just do this

struct distance_table node0;

then all your calls would be like

node0.costs[i][j]     // etc....

But if you are going with the pointer route then...

struct distance_table *node0 = NULL; // better practice

node0 = (struct distance_table *)malloc(sizeof(struct distrance_table));

then your logic works as you have it written...

assuming you are wanting a local version for local work...

But if you wanted a local COPY then you would add this line after allocating memory for it (pointer version)...

memcpy((void *)node0,&dt0,sizeof(struct distance_table));


0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now