how to store pointer adress in a char*

Discussion in 'C++' started by mwebel@freenet.de, Nov 17, 2006.

  1. Guest

    Hi,
    i had this problem before (posted here and solved it then) now i have
    the same problem but more complicated and general...

    basically i want to store the adress of a istream in a char* among
    other pieces of information information and retrieve it later... like
    this:
    ***********************

    //supossing i have a istream
    istream in("test.txt");


    //i store its adress as a char
    char* add=(char*)∈


    //[....]

    //inside the code of another function i create a pointer
    istream * t;


    //and restore its adress back
    t=(ifstream*)add;

    *******************

    now thats works ok...


    what i want now is to store not only the adress but also some text...
    and the whole should look like a filename
    lets say the adress is 0xDEAD i would like the char* to look like this

    "file:DEADtext2.txt"

    the prefix has a fixed length as well as the suffix....
    i thought the adress would be 32 bit (4 bytes 32bit pentium machine)

    so i wanted to store first in some strings like this(sorry for the ugly
    code but im desperate :) ):
    ********
    //store prefix
    string pref="http:":

    //store adress
    string add=(char*)&in:

    //store suffix
    string suf="text2.txt":

    //concatenate all three
    string total = pref + add + suf;

    char* add= total.cstr();
    *******
    cout<< sizeof(&in)<<endl;
    says the size of the istream is 4 bytes...

    but when i display add.length() it says its only three bytes long... so
    im quite confused... so where is the last byte?

    also when i display the elements of the istream:

    cout<<(int)((char*)&in)[0]<<endl;
    cout<<(int)((char*)&in)[1]<<endl;
    cout<<(int)((char*)&in)[2]<<endl;
    cout<<(int)((char*)&in)[3]<<endl;

    then element 3 is a zero... thus there are only three bytes...

    so two pleas for help:

    how long is the adress actually?

    how can i concatenate those three pieces of information and retrieve
    them later too??

    thanks in advance to all ideas, hints where to further look for and
    answers...
    , Nov 17, 2006
    #1
    1. Advertising

  2. ondra.holub Guest

    Re: how to store pointer adress in a char*

    If you get any address, it is not text string. So it may contain in any
    byte 0 (zero). If you want to get text representation of pointer (I
    think for some tracing, I see no other usefull usage), write it to
    string this way:

    #include <sstream>

    std::eek:stringstream osstr;
    osstr << &in; // &in is required pointer
    std::string value = osstr.c_str();
    ondra.holub, Nov 17, 2006
    #2
    1. Advertising

  3. Rolf Magnus Guest

    wrote:

    > Hi,
    > i had this problem before (posted here and solved it then) now i have
    > the same problem but more complicated and general...
    >
    > basically i want to store the adress of a istream in a char* among
    > other pieces of information information and retrieve it later...


    Why?

    > like this:
    > ***********************
    >
    > //supossing i have a istream
    > istream in("test.txt");
    >
    >
    > //i store its adress as a char
    > char* add=(char*)&in;


    I'd rather use a C++ cast.


    > //[....]
    >
    > //inside the code of another function i create a pointer
    > istream * t;
    >
    >
    > //and restore its adress back
    > t=(ifstream*)add;
    >
    > *******************
    >
    > now thats works ok...
    >
    >
    > what i want now is to store not only the adress but also some text...
    > and the whole should look like a filename
    > lets say the adress is 0xDEAD i would like the char* to look like this
    >
    > "file:DEADtext2.txt"
    >
    > the prefix has a fixed length as well as the suffix....
    > i thought the adress would be 32 bit (4 bytes 32bit pentium machine)


    Why don't you just put all your data into a struct and get a pointer to
    that?

    > so i wanted to store first in some strings like this(sorry for the ugly
    > code but im desperate :) ):
    > ********
    > //store prefix
    > string pref="http:":



    Did you mean:

    string pref="http:";

    > //store adress
    > string add=(char*)&in:


    Did you mean:

    string add=(char*)&in;


    This line will try to interpret &in as a pointer to the first element of a
    null terminated array of char (also known as C style string) and copy over
    that array into add. Most certainly not what you want.

    > //store suffix
    > string suf="text2.txt":


    Did you mean:

    string suf="text2.txt";

    > //concatenate all three
    > string total = pref + add + suf;
    >
    > char* add= total.cstr();


    add is already defined as string, so you should get an error here.

    > *******
    > cout<< sizeof(&in)<<endl;
    > says the size of the istream is 4 bytes...


    No. It says that the size of a pointer to istream is 4 bytes.

    > but when i display add.length() it says its only three bytes long... so
    > im quite confused... so where is the last byte?


    add won't hold the pointer to the istream. It will try to interpret the
    stream as a C style string (which it isn't). Don't do that!

    > also when i display the elements of the istream:
    >
    > cout<<(int)((char*)&in)[0]<<endl;
    > cout<<(int)((char*)&in)[1]<<endl;
    > cout<<(int)((char*)&in)[2]<<endl;
    > cout<<(int)((char*)&in)[3]<<endl;
    >
    > then element 3 is a zero... thus there are only three bytes...


    There might be, or there might be more. A stream object is not a C style
    string. It may contain zero bytes wherever it wants.

    > so two pleas for help:
    >
    > how long is the adress actually?


    The address is 4 bytes, but that's irelevant.

    > how can i concatenate those three pieces of information and retrieve
    > them later too??


    It wold be best not to do that at all. What are you trying to achieve
    actually?
    Rolf Magnus, Nov 17, 2006
    #3
  4. Guest

    Re: how to store pointer adress in a char*

    > It wold be best not to do that at all. What are you trying to achieve
    > actually?


    hi,
    thanks for the responses(and sorry for the : instead of ; the font was
    too litle :))

    The situation is as follows:

    i have a prrogram in c++(A) and a library in C (B) which i cant change.
    However this library can be extended by plugins. I am writing the
    plugin in C++(C).

    Now i open a file in a ifstream in A but B cant process ifstreams and
    it accepts filenames only... so i want to use plugin C to open and read
    it and pass it back to B.

    1.- (A) opens the file and creates a pointer hides the pointer in a
    filename and pass to (B)
    2.- (B) receives the filename (char*) and passes to plugin (C)
    3.- (C) reads the file and passes the content to (B )


    All the other work is done (plugin, program, opening etc..)
    the only problem left is how to pass the char* containing the pointer
    from (A) to (C)

    i there would be no difference to using a struct as the char* needs to
    look like a filename (its parsed and upon finding the 'file:' and file
    extension passed to my plugin!)

    i hope my problem is clearer now.
    , Nov 18, 2006
    #4
  5. Guest

    Re: how to store pointer adress in a char*

    > write it to string this way:


    Thanks! this might be one important part of what i was looking for!

    I exposed my problem to Rolf Magnus...
    hopefully you can understand it better now...
    i think i i can retrieve this adress back from he middle of a string,
    cast it back to a adress and set it to a ifstream pointer my problem
    might been solved!
    Do you think this might work this way?
    i will try it as soon as i get back to uni!
    thanks for the answer!
    , Nov 18, 2006
    #5
  6. Re: how to store pointer adress in a char*

    * :
    >> It wold be best not to do that at all. What are you trying to achieve
    >> actually?

    >
    > hi,
    > thanks for the responses(and sorry for the : instead of ; the font was
    > too litle :))
    >
    > The situation is as follows:
    >
    > i have a prrogram in c++(A) and a library in C (B) which i cant change.
    > However this library can be extended by plugins. I am writing the
    > plugin in C++(C).
    >
    > Now i open a file in a ifstream in A but B cant process ifstreams and
    > it accepts filenames only...


    Pass it a filename.


    > so i want to use plugin C to open and read
    > it and pass it back to B.
    >
    > 1.- (A) opens the file and creates a pointer hides the pointer in a
    > filename and pass to (B)
    > 2.- (B) receives the filename (char*) and passes to plugin (C)
    > 3.- (C) reads the file and passes the content to (B )


    1. A prepares the filename in a buffer.
    2. B receives the filename.
    3. C opens the file, reads the file and passes the content to B.

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
    Alf P. Steinbach, Nov 18, 2006
    #6
  7. Guest

    Re: how to store pointer adress in a char*

    Alf P. Steinbach wrote
    > Pass it a filename.
    > 1. A prepares the filename in a buffer.
    > 2. B receives the filename.
    > 3. C opens the file, reads the file and passes the content to B.


    unfortunately i cant...

    A already opens the file and may or may not have been working on it...

    unfortunately i can't change the situation more that i already
    pictured it.

    A opens the file and provides a ifstream variable. (outside of my
    working scope!)
    B needs the content.(its a library. Can not chage it)
    C *has* to receive an already open stream. C does only reading into Bs
    intern buffer.
    It is not allowed to open or close it... just read operations.

    Unfortunately i can not change the problem... i can just try to shape
    the answer.
    , Nov 18, 2006
    #7
  8. Guest

    Re: how to store pointer adress in a char*

    wrote:
    > A already opens the file and may or may not have been working on it...

    [...]
    > Unfortunately i can not change the problem... i can just try to shape
    > the answer.


    The problem is to work on an open ifstream.
    B already has routines to work on files...
    else i would pass a filename to B directly.

    So the whole fuzz *is* about teaching B to work with an open C++
    ifstream.
    , Nov 18, 2006
    #8
  9. Re: how to store pointer adress in a char*

    * :
    > Alf P. Steinbach wrote
    >> Pass it a filename.
    >> 1. A prepares the filename in a buffer.
    >> 2. B receives the filename.
    >> 3. C opens the file, reads the file and passes the content to B.

    >
    > unfortunately i cant...
    >
    > A already opens the file and may or may not have been working on it...
    >
    > unfortunately i can't change the situation more that i already
    > pictured it.
    >
    > A opens the file and provides a ifstream variable. (outside of my
    > working scope!)
    > B needs the content.(its a library. Can not chage it)
    > C *has* to receive an already open stream. C does only reading into Bs
    > intern buffer.
    > It is not allowed to open or close it... just read operations.
    >
    > Unfortunately i can not change the problem... i can just try to shape
    > the answer.


    Well, then, let A (the part that you're in control of) communicate
    directly with C.

    1. A passes pointer to stream object to C.
    2. A calls B with some special format file spec, causing B to
    hand it over to extension C.
    3. Extension C uses pointer already passed from A.

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
    Alf P. Steinbach, Nov 18, 2006
    #9
  10. Guest

    Re: how to store pointer adress in a char*

    "Alf P. Steinbach:

    > Well, then, let A (the part that you're in control of) communicate
    > directly with C.


    i cant... A has no way of calling C.
    Like i said i cant reshape the problem.
    And my current problem is how to pass that adress on a char* and
    retrieve it later.
    Do you have any advise on how i can solve this problem?
    , Nov 18, 2006
    #10
  11. Re: how to store pointer adress in a char*

    * :
    > "Alf P. Steinbach:
    >
    >> Well, then, let A (the part that you're in control of) communicate
    >> directly with C.

    >
    > i cant... A has no way of calling C.
    > Like i said i cant reshape the problem.
    > And my current problem is how to pass that adress on a char* and
    > retrieve it later.
    > Do you have any advise on how i can solve this problem?


    Yes, start saying "I can" instead of "I can't".

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
    Alf P. Steinbach, Nov 18, 2006
    #11
  12. Guest

    Re: how to store pointer adress in a char*

    wrote:
    > > write it to string this way:


    hi!
    thanks again for your great advise!
    It works very well and the streams work perfectly.
    it definitely solved my problem.

    just in case anybody who finds this thru google in the future:
    on windows platform instead of int it is advised to work witn INT_PTR
    type which will always be the size of a pointer. this is important
    regarding 64bit portability issues.
    i dont know of any platform independent solution though...
    maybe just work with 'define' operators.
    , Nov 28, 2006
    #12
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?ISO-8859-1?Q?Dennis_Pl=F6ger?=

    Passing the adress of a single item of a char array

    =?ISO-8859-1?Q?Dennis_Pl=F6ger?=, Sep 23, 2003, in forum: C++
    Replies:
    12
    Views:
    639
    =?ISO-8859-1?Q?Dennis_Pl=F6ger?=
    Sep 25, 2003
  2. wwj
    Replies:
    7
    Views:
    542
  3. wwj
    Replies:
    24
    Views:
    2,484
    Mike Wahler
    Nov 7, 2003
  4. FKothe

    invalid pointer adress

    FKothe, Feb 10, 2005, in forum: C Programming
    Replies:
    5
    Views:
    382
    Richard Bos
    Feb 14, 2005
  5. lovecreatesbeauty
    Replies:
    1
    Views:
    1,015
    Ian Collins
    May 9, 2006
Loading...

Share This Page