C++ HashTable Read access violation

I am building a HashTable in C++ which takes a name of a person as the key and stores his/her favorite drink, the hashtable is defined as below:

class HashTable {
private:
    static const int m_tableSize = 10;

    struct item {
        std::string name;
        std::string drink;
        item* next;
    };

    item* Table[m_tableSize];

I am using the constructor to fill every bucket in the hashtable as "empty":

HashTable::HashTable()
{
    for (int i = 0; i < m_tableSize; i++)
    {
        Table[i] = new item;
        Table[i]->name = "empty";
        Table[i]->drink = "empty";
        Table[i]->next = NULL;
    }
}

This code as it is works but, here's the question:

As far as I know, Table[i] = new item; is supposed to allocate each first item of the bucket in the heap instead of allocating in the stack, but if I try to supress this line, in other words, if I want the first item of each bucket to be allocated in the stack, the compiler throws a read access violation exception, why?

I don't necessarily want the first item of each bucket to be allocated in the stack, but I don't understand the compilers behavior in this case. I know it may be a little bit basic, but can some help?



Comments

Popular posts from this blog

Today Walkin 14th-Sept

Spring Elasticsearch Operations

Hibernate Search - Elasticsearch with JSON manipulation