Solved

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

Posted on 2003-11-13
6
4,048 Views
Last Modified: 2012-05-04
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
Comment
Question by:VWrestle97
6 Comments
 
LVL 45

Expert Comment

by:sunnycoder
Comment Utility
 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
 
LVL 45

Expert Comment

by:sunnycoder
Comment Utility
 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
 
LVL 16

Accepted Solution

by:
_nn_ earned 300 total points
Comment Utility
node0 is possibly meant to point to dt0, so I guess it should be

struct distance_table *node0 = &dt0;

0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 

Author Comment

by:VWrestle97
Comment Utility
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
 
LVL 45

Expert Comment

by:sunnycoder
Comment Utility
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
 
LVL 5

Assisted Solution

by:g0rath
g0rath earned 200 total points
Comment Utility
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now