Question

After reading through a midi file using the MIPS ISA on the MARS emulator, I am trying to store decoded events in an array list that will be read on playback of each event. Because the containment data structure is an array list, it must be able to dynamically shrink and grow.

In my scheme, each event will be stored as two words in an array. This means that every time I add an element to the array list, the list will have to grow by 8 bytes.

My question is how do I dynamically allocate enough memory for an array that I do not know the size of during allocation? How would I then go through this created list?

Was it helpful?

Solution

Either allocate the maximum possible size for the array from the heap at the start of your program, or use a different data structure, such as a linked list, that can be allocated piecemeal.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top