c++c++17dynamic-memory-allocationdynamic-arrays

Memory is not being allocated


I am sorry if the question is stupid. The problem is that memory for my dynamic array inside a static structure is not being allocated. The reason might be my pc (it is a really old notebook with only 8 gbs of RAM) but I doubt it is.I propably done a mistake while coding allocation of memory to the

I have the following structs with static values

struct City {
    std::string name;
    int Population=0;
    bool HasMetro=false;
}NullCity;

struct HashNode {
    long long key=0;
    City data;
};

HashNode NullNode = {0,NullCity};
struct LinkedList {
    HashNode Data;
    LinkedList* next=nullptr;
};

LinkedList NullList = {NullNode, nullptr};

struct HashTable {
    int size=0;
    LinkedList** BucketsArray;
    int amount=0;
};
HashTable HT={30,new LinkedList*[30],0};

The code before was aimed at HT.BucketsArray to be a fixed and I am slowly trying to make it dynamic array.

But when I innitialise the code instead of giving me an array of null pointers as it gave me while it was a fixed array it gives me this

@ ALREADY PREPROCESSED @HT = {HashTable} 
 size = {int} 30
 BucketsArray = {LinkedList **} 0xef3b00 
  *BucketsArray = {LinkedList *} 0xbaadf00dbaadf00d 
   *value = {LinkedList} 
    error = Error occurred in Python: Cannot access memory at address 0xbaadf00dbaadf03d
 amount = {int} 0

((Also I am coding in Clion c++ not in python so I am suprised to see it here)) To note is was as follows before

@ ALREADY PREPROCESSED @HT = {HashTable} 
 size = {int} 30
 BucketsArray = {LinkedList *[30]} 
  [0] = {LinkedList *} NULL
  [1] = {LinkedList *} NULL
  [2] = {LinkedList *} NULL
  [3] = {LinkedList *} NULL
  [4] = {LinkedList *} NULL
  [5] = {LinkedList *} NULL
  [6] = {LinkedList *} NULL
  [7] = {LinkedList *} NULL
  [8] = {LinkedList *} NULL
  [9] = {LinkedList *} NULL
  [10] = {LinkedList *} NULL
  [11] = {LinkedList *} NULL
  [12] = {LinkedList *} NULL
  [13] = {LinkedList *} NULL
  [14] = {LinkedList *} NULL
  [15] = {LinkedList *} NULL
  [16] = {LinkedList *} NULL
  [17] = {LinkedList *} NULL
  [18] = {LinkedList *} NULL
  [19] = {LinkedList *} NULL
  [20] = {LinkedList *} NULL
  [21] = {LinkedList *} NULL
  [22] = {LinkedList *} NULL
  [23] = {LinkedList *} NULL
  [24] = {LinkedList *} NULL
  [25] = {LinkedList *} NULL
  [26] = {LinkedList *} NULL
  [27] = {LinkedList *} NULL
  [28] = {LinkedList *} NULL
  [29] = {LinkedList *} NULL
 amount = {int} 0

((Both current output and output from before are taken from Clion Debuger at the start of the int main() function.))

I also tried to declare HT the following ways and none of them gave me memory.

struct HashTable {
    int size=30;
    LinkedList** BucketsArray = new LinkedList*[size];
    int amount=0;
};

HashTable HT;
struct HashTable {
    int size=30;
    LinkedList** BucketsArray = new LinkedList*[30];
    int amount=0;
};

HashTable HT;
struct HashTable {
    int size=0;
    LinkedList** BucketsArray;
    int amount=0;
};

HashTable HT={};

Solution

  • The problem isn't that memory isn't being allocated but that it's filled with invalid indeterminate values.
    (These may all happen to be 0xbaadf00dbaadf00d, but that's a convenience for debugging purposes; it's still "indeterminate" from the program's point of view.)

    When you used an array, it was initialized with null pointers because it was an array with static storage duration.

    However, new LinkedList*[30] creates an array of 30 uninitialized pointers in the "free store".

    To default-initialize the elements (which means the null pointer for pointers), use new LinkedList*[30]{} or new LinkedList*[30]().