Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > how to store pointer adress in a char*

Reply
Thread Tools

how to store pointer adress in a char*

 
 
mwebel@freenet.de
Guest
Posts: n/a
 
      11-17-2006
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

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

 
Reply With Quote
 
 
 
 
ondra.holub
Guest
Posts: n/a
 
      11-17-2006
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:stringstream osstr;
osstr << &in; // &in is required pointer
std::string value = osstr.c_str();

 
Reply With Quote
 
 
 
 
Rolf Magnus
Guest
Posts: n/a
 
      11-17-2006
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
>
> "fileEADtext2.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?

 
Reply With Quote
 
mwebel@freenet.de
Guest
Posts: n/a
 
      11-18-2006
> 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.

 
Reply With Quote
 
mwebel@freenet.de
Guest
Posts: n/a
 
      11-18-2006
> 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!

 
Reply With Quote
 
Alf P. Steinbach
Guest
Posts: n/a
 
      11-18-2006
* :
>> 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?
 
Reply With Quote
 
mwebel@freenet.de
Guest
Posts: n/a
 
      11-18-2006

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.

 
Reply With Quote
 
mwebel@freenet.de
Guest
Posts: n/a
 
      11-18-2006

mwe...@freenet.de 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.

 
Reply With Quote
 
Alf P. Steinbach
Guest
Posts: n/a
 
      11-18-2006
* :
> 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?
 
Reply With Quote
 
mwebel@freenet.de
Guest
Posts: n/a
 
      11-18-2006

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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Pointer to pointer Vs References to Pointer bansalvikrant@gmail.com C++ 4 07-02-2009 10:20 AM
passing the address of a pointer to a func that doesnt recieve a pointer-to-a-pointer jimjim C Programming 16 03-27-2006 11:03 PM
invalid pointer adress FKothe C Programming 5 02-14-2005 04:29 PM
Pointer-to-pointer-to-pointer question masood.iqbal@lycos.com C Programming 10 02-04-2005 02:57 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57