casting a series of chars to a char pointer

Started by
4 comments, last by EvilCrap 22 years, 5 months ago
hi im writting a scriptor, and i have a big array of chars for memory. char* Memory = new char[Max_Mem]; whenever the scriptor wants to store a pointer, how do i stick it in my array, and how do i get it back out?
Advertisement
I figured it out!! but THERE HAS GOT TO BE AN EASIER WAY!!

char* memory = new char[100];

long value = 1002;

long l;
long* plong = &value

l = (long) plong;

char* ptChar = (char*) l;

for (int i = 0; i < sizeof(long*); i++)
cout << ( memory = ptChar );<br> <br> cout << endl;<br> long l2;<br> long* plong2;<br> char* pchar = new char[sizeof(char)];<br><br> for( i =0; i <sizeof(long*);i++)<br> pchar = memory;<br> l2 = (long)pchar;<br> plong2 = (long*) l2;<br><br> cout << *plong2;<br><br><br><br>PLEASE, IS THERE AN EASIER WAY?! </i>
Just what is it that you want to do?

If you really want to store pointers in an array, then use an array of pointers. If you want to store the characters in the array, then use strdup(string) to make a copy of the string, or strcpy(array, string) to copy the string''s characters into an existing array.

I''ve got no idea what your code is intending to do. Near as I can tell you''re trying to store a pointer to VALUE in a string array. Why you can''t just keep it in a pointer, I''m unsure.

    // The string only needs to be as long as the pointer (unless  // you want to store more than one in it.  char* memory = new char[sizeof(long *)];  long value = 1002;  // Cast MEMORY to a pointer to a pointer to a long.  *((long**)memory) = &value  long value2;  // Cast MEMORY to a pointer to a pointer to a long again.  // Dereference it once to get a pointer to a long (value),  // and once again to get the long itself (1002).  value2 = **((long**)memory);  


That should work. But why are you doing it this way?

Signatures? We don''t need no steenking signatures!
CoV
Seriuosly, what exactly are you trying to do? If you want to store pointers... you could do something like this to make it readable...

void StorePointer(char *Mem, long MemLoc, long Ptr)
{
*(long*)&Mem[MemLoc]=Ptr;
}

long GetPointer(char *Mem, long MemLoc)
{
return *(long*)&Mem[MemLow];
}

int main(void)
{
char *Memory = new char[Max_Mem];
long value = 1002;
long *ptr;
StorePointer(Memory,0,(long)&value); //Store pointer in memory.. takes 4 chars
tmp = (long*)GetPointer(Memory,0); //Get pointer out.
cout << *tmp << endl; //Display pointer
value = 200;
StorePointer(Memory,10,(long)&value); //Store pointer in mem.. 4 chars
tmp = (long*)GetPointer(Memory,10); //Get it back out.
cout << *tmp << endl; //Display pointer
//You could just do this:
// cout << *(long*)GetPointer(Memory,10); and it works fine
}

Billy
im trying to write a virtual machine, which will run scripts writtin by users; since the script and variables are stored in the array Memory[], i need a way to store both literal values, and pointers to other areas in the array.
ive thought it out, and this is bad because the user would be able to access memory outside of the virtual machine''s memory.
and, that code i posted was really poor, i admit, i was excited at the time.
instead, im goinu use null to signal pointer, and then subsequent bytes will state the memory index to be refrenced.

thanks for the input, given my vague post.
quote:Original post by EvilCrap
im trying to write a virtual machine, which will run scripts writtin by users; since the script and variables are stored in the array Memory[], i need a way to store both literal values, and pointers to other areas in the array.

Ah, that makes sense then.
quote:
ive thought it out, and this is bad because the user would be able to access memory outside of the virtual machine's memory.

It doesn't have to:
          void *MemSys::getPtr (int index) // index is in bytes  {    if ((index < 0) || (index > MemoryLength))      /* do something to handle an invalid memory reference */    return ((void*)Memory)[Memory + index / sizeof(void*)];  }    With this method, it occurs to me, pointers must be aligned on a sizeof(void*) boundary.              return *((void*)&Memory[Memory + index]);        

With this one, the pointer can be anywhere.

All your bases belong to us

Edited by - Mayrel on November 3, 2001 8:17:09 AM
CoV

This topic is closed to new replies.

Advertisement