how to store pointer adress in a char*

M

mwebel

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...
 
O

ondra.holub

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();
 
R

Rolf Magnus

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?
 
M

mwebel

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.
 
M

mwebel

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!
 
A

Alf P. Steinbach

* (e-mail address removed):
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.
 
M

mwebel

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.
 
M

mwebel

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.
 
A

Alf P. Steinbach

* (e-mail address removed):
Alf P. Steinbach wrote

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.
 
M

mwebel

"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?
 
A

Alf P. Steinbach

* (e-mail address removed):
"Alf P. Steinbach:


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".
 
M

mwebel

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top