473,325 Members | 2,860 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

how to store pointer adress in a char*

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 '06 #1
11 3408
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::ostringstream osstr;
osstr << &in; // &in is required pointer
std::string value = osstr.c_str();

Nov 17 '06 #2
mw****@freenet.de 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?

Nov 17 '06 #3
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 '06 #4
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 '06 #5
* mw****@freenet.de:
>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?
Nov 18 '06 #6

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 '06 #7

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.

Nov 18 '06 #8
* mw****@freenet.de:
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?
Nov 18 '06 #9

"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 '06 #10
* mw****@freenet.de:
"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?
Nov 18 '06 #11
mw****@freenet.de 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 '06 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Dennis Plöger | last post by:
Hi all! I'm currently having some problems parsing a char array in c++. (And yes, I'm a half-newbie ;-)) Perhaps you can help me with this: #include <iostream> using std::cout; void...
5
by: FKothe | last post by:
Hello together, the program below shows a behavior i do not understand. When compiled with the HX-UX11 c-comiler ( version B.11.11.04 ) v2.p in function test_it0 points to an invalid adress and...
12
by: arkobose | last post by:
my earlier post titled: "How to input strings of any lengths into arrays of type: char *array ?" seems to have created a confusion. therefore i paraphrase my problem below. consider the...
4
by: Carramba | last post by:
hi! Iam trying to make program were I enter string and serach char. and funktion prints out witch position char is found this is done if funktion serach_char. so far all good what I want do...
3
by: Carramba | last post by:
hi! the code is cinpiling with gcc -ansi -pedantic. so Iam back to my question Iam trying to make program were I enter string and serach char. and funktion prints out witch position char is...
8
by: Serve Laurijssen | last post by:
Consider the following code char *p1 = malloc(10); char *p2 = malloc(10); if (p1 p2) puts("bigger"); free(p1); if (p1 p2)
1
by: mitola | last post by:
#include <stdlib.h> #include <stdio.h> #include <string.h> #include <conio.h> struct oseba //struktura osebe , ki jo kasneje uporabljamo v datoteki { char ime; char priimek;
4
by: ashusharma82 | last post by:
plz check following code pMe i an structure pointer,in which char *linewisechar is made. pMe->linewisechar=(pMe->newcchar+STRLEN(pMe->newcchar)-1); here i am assigning...
4
by: ctx2002 | last post by:
hi guys: I am reading Sqlite code at moment, my c language skill not good enough , hope some one here can help me. In function listAdd(PagerLruList *pList, PagerLruLink *pLink, PgHdr *pPg);...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.