dimanche 3 novembre 2019

Check for NULL fails

I have the following map implementation:

    template <typename value>
    struct map
    {
      node<value>* Nodes;
      uint32_t Capacity;
      uint32_t Size;
    }

Later on I do:

Shaders = InitializeMap<shader>(MAX_SHADERS, sizeof(shader));
......

template<typename value>
internal map<value> InitializeMap(const int Capacity, const size_t ElementSize)
{
  map<value> Map;
  Map.Capacity = Capacity;
  Map.Size = 0;
  Map.Nodes = (node<value>*) AllocateMemory(&Game.Memory, Capacity * ElementSize);
  for (int i = 0; i < Capacity; i++)
  {
    Map.Nodes[i].Key = nullptr;
    Map.Nodes[i].Value = nullptr;
  }

  return Map;
}

Inside the InsertNode function something strange happens when I insert the first node if I do this:

int HashIndex = HashCode(Node.Key);

if (Nodes[HashIndex].Key) // this is true
{
  printf("%s\n",Nodes[HashIndex].Key); // this seg faults
}

How can the if condition evaluate to true? After I just explicitly assign it to nullptr

Aucun commentaire:

Enregistrer un commentaire