string to pointer

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • vsachar
    New Member
    • Mar 2009
    • 15

    string to pointer

    Hi,
    I'm writing an address into a file and later i need to read it and delete the memory at this address. I've copied this address into a character array and am having a lot of trouble trying to convert it back into a pointer.
    The pointer initially (when writing to the file) was a void pointer.
    Please help.
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Originally posted by vsachar
    Hi,
    I'm writing an address into a file and later i need to read it and delete the memory at this address. I've copied this address into a character array and am having a lot of trouble trying to convert it back into a pointer.
    The pointer initially (when writing to the file) was a void pointer.
    Please help.
    Cast your void pointer to a long (unsigned) int and write that number to a file. You can later read it back again and cast it to a void*.

    kind regards,

    Jos

    Comment

    • vsachar
      New Member
      • Mar 2009
      • 15

      #3
      Hi,
      I tried this. i don't know if thats what you meant but it didn't give me the output i was looking for

      Code:
      #include<iostream>
      #include<string>
      #include<stdio.h>
      #include<fstream>
      using namespace std;
      void * operator new(size_t size)
      {
      	cout<<"\nOVERLOADED NEW";
      	void * MemHandle;
      	MemHandle=malloc(size);
      	if(!MemHandle)
      	{
      		bad_alloc ba;
      		throw ba;
      	}
      	ofstream out;
      	out.open("D:/Project/abc.txt",ios::app);
      	out<<"\n"<<(unsigned long int*)MemHandle;
      	out.close();
      	cout<<"\nmem handle:"<<MemHandle<<endl;
      	return MemHandle;
      }
      void main()
      {
      	fstream in("D:/Project/abc.txt");
      	int *p=new int;
      	char buf[20];
      	in.getline(buf,8);
      	buf[8]='\0';
      	cout<<"buf:"<<(unsigned long int*)buf;
      }
      The output was :

      OVERLOADED NEW
      mem handle:003552A0
      buf:0012FE9


      i needed the buf to hold the initial value.
      Please help and Thanks in advance.

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Don't do it like that; do something like this instead for your output:

        Code:
        void* memory_pointer= ...;
        cout << (unsigned long)memory_pointer;
        and for input do something like this:
        Code:
        unsigned long tmp;
        void* memory_pointer;
        cin >> tmp;
        memory_pointer= (void*)tmp;
        kind regards,

        Jos

        Comment

        • vsachar
          New Member
          • Mar 2009
          • 15

          #5
          Originally posted by JosAH
          Don't do it like that; do something like this instead for your output:

          Code:
          void* memory_pointer= ...;
          cout << (unsigned long)memory_pointer;
          and for input do something like this:
          Code:
          unsigned long tmp;
          void* memory_pointer;
          cin >> tmp;
          memory_pointer= (void*)tmp;
          kind regards,

          Jos

          Thank you so much!!
          It worked!
          :)

          Comment

          Working...