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.

Recommended Answers

All 2 Replies

Keep in mind that I'm not married to the code I have written or even reinterpret_cast. I'm up for large changes if necessary.

You are taking the address of a char pointer to get string and taking the address of a char to get the character itself - these are the problems with cout << "Characters:" << &temp << endl and while(&temp[i]!=0) ; In the former, you're taking the address of the pointer temp , while in the latter you're checking whether the address of the i-th element of temp is NULL, which is never true hence the segmentation fault.

The right thing to do is check if the i-th element itself is NULL, not its address.

On a side note, string operations are usually slow, so you might want to avoid doing filename+=temp[i] ; Also, if DataMemory is an ordinary contiguous array with element size of 4 bytes, and step is always 1, then you can omit the whole if(i==3) block.

And finally, a much easier way to get the filename would be to just do string filename( temp ) - this constructor treats temp as a C string and automatically scans the memory pointed by the argument until it finds a 0.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.