Suppose I have this struct representing a virtual machine.
Please, check out my init()
method. In the init()
method I am allocating some space in the heap for the state of my virtual machine. But, something doesn't seem right. I am allocating space first for my state (struct), then for the RAM (array of type int), which is already inside of my struct. Is it okay? or I'm doing something wrong?
My question is how do I property allocate memory for the array inside of struct in the heap.
typedef struct {
int* ram;
int pc;
} vm_t;
vm_t* init(int ram_size)
{
vm_t* vm = (vm_t*)malloc(sizeof(vm_t));
vm->ram = (int*)malloc(ram_size * sizeof(int));
vm->pc = 0;
return vm;
}