Mar 9, 2011 at 2:11am UTC
You can't change the size of an array declared like that. It needs to be dynamically allocated.
Mar 9, 2011 at 2:14am UTC
Three things wrong with line 17:
1. as Zhuge points out, im is an array, not a pointer.
2. if im were a pointer, you would be leaking memory.
3. the previous contents of im are not copied, resulting in losing all previously stored instructions.
Best to consider using vector<string> for im.
Mar 9, 2011 at 2:20am UTC
im is a pointer. It is a local variable inside the if scope. (but that is an error)
Also line 19 is out of bounds
Mar 9, 2011 at 2:40am UTC
Best to consider using vector<string> for im.
+100 @ This
No need to reinvent the wheel here. Just use a vector:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
class InstructionMemory {
private :
vector<string> im;
public :
string fetch(int pc)
{
return im[pc];
};
void store(string a, int index)
{
if (index >= im.size())
im.resize(index+1);
im[index] = a;
};
friend class ControlUnit;
};
Last edited on Mar 9, 2011 at 2:40am UTC
Mar 9, 2011 at 5:49am UTC
Thanks a lot for all of your replies!! Disch, thanks for the example.