0

I have three structs being used:

struct namedPlace{
      string name, key;
      double lat, lon, interdist, interref;
      intersections * inter;
      namedPlace * next;

      namedPlace(){next = NULL;}

      namedPlace(string k, string n, double la, double lo, double c, double i, namedPlace * ne){
          key = k;name = n; lat = la; lon = lo;interref=c; interdist = i; next=ne;}
};


struct Node{
      namedPlace * head;

      Node(){head=NULL;}

      void insertInNode(namedPlace * place)
      {   if(head==NULL) cout<<"NULL";
          namedPlace * hold = head;
          head = place;
          place -> next = hold; }
};

 struct haash{
   Node ** nArr;

   haash(){nArr=NULL;}  

   void HashMap() {
            nArr = new Node*[30000];
                }

   void insertInHash(const int entry, namedPlace *in)
   {    nArr[entry]->insertInNode(in);  }
 };

And in main I have:

  haash h;
  h.HashMap();
  for(int i = 0; i <20; i++)
  { 
      //code to get values to insert in node                                                   
       namedPlace * np = new namedPlace(postal, namePlace, lattt, longgg, intersectReff, distanceToIntersection,NULL);
       int hashVal = h.hasher(np->key);
       h.insertInHash(hashVal,np);
  }

The program breaks (run time error) when I try to check if head is NULL in the Node struct. I'm led to believe this is happening because there is some undefined behavior around head, but am confused because I believe I initialize it to NULL with my constructor. I am told that nArr[entry] is CDCDCDCD, and as it has been pointed out, this is due to uninitialized heap memory. How do I properly initialize it?

2 Answers 2

1
 nArr[entry]->insertInNode(in);

This can be problem, because nArr[entry] is NULL.

Sign up to request clarification or add additional context in comments.

3 Comments

Yes, an if statement seems to show that the value is NULL and explains the crash down the next step. However, I once again removed the for loop that turns all those pointers to NULL, and am still getting a break at the same point even though I'm told the nArr[entry] is no longer NULL and is now "CDCDCDCD."
en.wikipedia.org/wiki/Magic_number_%28programming%29 tells us that CDCDCDCD is "Used by Microsoft's C/C++ debug malloc() function to mark uninitialized heap memory, usually returned from HeapAlloc()"
Issue is that if nArr[entry] is NULL, then object is virtualy placed at address 0. At nArr[entry]->insertInNode(in); function is trying to get value of pointer from object that is located at address 0. It is not because value is 0, but because you are reading value of pointer from address near 0.
0

AHA! So I've updated the code to this within the haash strut:

void HashMap() 
   {      nArr = new Node*[TABLE_SIZE];
          for(int i = 0; i<TABLE_SIZE; i++)
             nArr[i] = NULL;   
       }

void insertInHash(const int entry, namedPlace *in)
   {     nArr[entry] = new Node;
         if(in->next == NULL) 
            nArr[entry]->insertInNode(in);  }

And I no longer have uninitialized heap memory. It appears that I must first declare the array of pointers to objects, then set them all to NULL, and then set each slot in the array equal to a new Node before attempting to do anything with the array or its contents.

1 Comment

Yeah or you could use techniques developed after 1991 to handle dynamic memory in a foolproof manner with 5% of the code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.