-3

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={};
7
  • 5
    please post a minimal reproducible example. Commented Apr 24 at 10:24
  • 6
    LinkedList** BucketsArray; - Why? Why manual memory management? Why not just use one of the standard containers? Strive to be a "zero star programmer" when you can. Commented Apr 24 at 10:27
  • 4
    a dynamic array of pointers to LinkedLists is spelled: std::vector<LinkedList*> Commented Apr 24 at 10:34
  • 2
    Your example is neither complete nor minimal. Please create a minimal reproducible example that has all necessary #includes and a main() that exercises just one thing, so we can see exactly what's expected. Commented Apr 24 at 13:13
  • The C-style linked list makes for awful C++. Forgetting that <list> exists and assuming some kind of homework. Hash tables as a linked list also makes little sense. The point is to use the hash for O(1) access to your data, linked list means your access time is O(n) due to needing to iterate the list. Commented Apr 24 at 16:27

1 Answer 1

9

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]().

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

4 Comments

Thanks it helped but also gave a new problem. I made the declaration as follows: ` struct HashTable { int size=30; LinkedList** BucketsArray=new LinkedList*[30]{}; int amount=0; }HT; ` It now gives memory but now the whole array is null instead of elements of it ` @ ALREADY PREPROCESSED @HT = {HashTable} size = {int} 30 BucketsArray = {LinkedList **} 0x703b00 *BucketsArray = {LinkedList *} NULL amount = {int} 0` So may I ask you how to fix that as well? I still am aiming for same allocation as it was in static if possible ofc.
same happened for LinkedList** BucketsArray=new LinkedList*[30](), and LinkedList** BucketsArray=new LinkedList*[30]{nullptr}
The array is still there, but BucketsArray is not an array any more, it's just a pointer to an array's first element. There might be some way to force the debugger to display the memory as an array, but you need to read the manual to find out. (And you should probably review the chapters about arrays and pointers in your favourite C++ book.)
Ah thanks I am just unused to this debug so I forgot. Thanks again for helping me this solved the whole problem

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.