I'm having difficulty using reinterpret_cast. Before I show you my code I'll let you know what I'm trying to do.
I'm trying to get a filename from a vector full of data being used by a MIPS I processor I designed. Basically what I do is compile a binary from a test program for my processor, dump all the hex's from the binary into a vector in my c++ program, convert all of those hex's to decimal integers and store them in a DataMemory vector which is the data memory unit for my processor. I also have instruction memory. So When my processor runs a SYSCALL instruction such as "Open File" my C++ operating system emulator receives a pointer to the beginning of the filename in my data memory. So keep in mind that data memory is full of ints, strings, globals, locals, all sorts of stuff. When I'm told where the filename starts I do the following:
Convert the whole decimal integer element that is being pointed to to its ASCII character representation, and then search from left to right to see if the string terminates, if not then just load each character consecutively into a "filename" string. Do this until termination of the string in memory and then store filename in a table. My difficulty is generating filename from my memory.
Here is an example of what I'm trying to do:
Index Vector NewVector ASCII filename
0 240faef0 128123792 'abc7' 'a'
0 240faef0 128123792 'abc7' 'ab'
0 240faef0 128123792 'abc7' 'abc'
0 240faef0 128123792 'abc7' 'abc7'
1 1234567a 243225 'k2s0' 'abc7k'
1 1234567a 243225 'k2s0' 'abc7k2'
1 1234567a 243225 'k2s0' 'abc7k2s'
//EXIT LOOP//
1 1234567a 243225 'k2s0' 'abc7k2s'
Here is the code that I've written so far to get filename (I'm just applying this to element 1000 of my DataMemory vector to test functionality. 1000 is arbitrary.):
int i = 0;
int step = 1000;//top->a0;
string filename;
char *temp = reinterpret_cast<char*>( DataMemory[1000] );//convert to char
cout << "a0:" << top->a0 << endl;//pointer supplied
cout << "Data:" << DataMemory[top->a0] << endl;//my vector at pointed to location
cout << "Data(1000):" << DataMemory[1000] << endl;//the element I'm testing
cout << "Characters:" << &temp << endl;//my temporary char array
while(&temp[i]!=0)
{
filename+=temp[i];//add most recent non-terminated character to string
i++;
if(i==3)//when 4 chatacters have been added..
{
i=0;
step+=1;//restart loop at the next element in DataMemory
temp = reinterpret_cast<char*>( DataMemory[step] );
}
}
cout << "Filename:" << filename << endl;
So the issue is that when I do the conversion of my decimal element to a char array I assume that 8 hex #'s will give me 4 characters. Why isn't this this case? Here is my output:
a0:0
Data:0
Data(1000):4428576
Characters:0x7fff5fbff128
Segmentation fault
Notice that temp is supposed to be 4 characters.