Foreach loop for struct of arrays

Hi,

I am trying a struct of arrays, something like this,

typedef struct {
int hash_id;
int hash_index;
} hash_t;

class something_here
hash_t hash [int];

 int temp_value = 13; 
 
 hash[temp_value].hash_id[0] = 21;
 hash[temp_value].hash_id[1] = 22;
 hash[temp_value].hash_id[2] = 23;
 hash[temp_value].hash_id[3] = 24;

 foreach (hash[temp_value].hash_id[i]) begin
    // display statements and some other code here        
 end

endclass

But, somehow the foreach loop is not even executed and the prints and the code is not getting executed after that. But, i was wondering if what i have done is right for the creation of the hash and if the syntax for the foreach loop is correct.

In reply to Prathyusha Sandilya:

hash_id and hash_index are dynamic arrays and need to be allocated with the new[] operator. Or did you mean to declare these as associative arrays as well?

In reply to dave_59:

Ok, I see the problem now - memory has not been allocated anywhere. That was a hole in my code there. I have done that now!!

Thanks!!

In reply to Prathyusha Sandilya:

Hi, Would it be possible to post your fixed code with the memory allocation ? I’m trying out a couple of things, but i’m unable to allocate memory using the new operator inside a class.

I’m able to do it in a module, but not in a class.

In reply to mavcdn:

I changed my dynamic array into a normal array. Something like this,

typedef struct {
int hash_id[4];
int hash_index[4];
} hash_t;

This solved the problem.

But, memory can be allocated later on as well, like,

typedef struct {
int hash_id[];
int hash_index[];
} hash_t;

hash_t hash [int];

hash.bank_id = new[4];
hash.bank_index = new[4];

not sure if the new[4] is the right syntax but the idea is to allocate memory to the array before trying to access it/assign values to it.

Thanks!