I have a struct to assign values to it. But my programm crashs it. Hopefully you can help me.
struct HashEntry{
std::string key; //the key of the entry
bool used; //the value of the entry
int value; //marks if the entry was used before
};
HashEntry *initHashList(int N){
HashEntry* hashList = new HashEntry[N];
for (int i = 0; i <= N; i++){
hashList[i].key = " ";
hashList[i].value = -1;
hashList[i].used = false;
}
for(int i = 0; i <N; i++){
cout<<hashList[i].value<<endl;
}
return hashList;
}
i <= Nin your first loop should bei < N, or you'll attempt to accesshashList[N], which corresponds to the(N+1)thelement (which doesn't exist).i <= Ntoi < Nas that will cause a crash because you are trying to access an element in yourhashListthat is out of bounds. Arrays are generally 0 based, so they start at index 0 up toN - 1in your case. Where are you cleaning up after yourhashListmemory allocation as I don't see it in this code?