PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.cplus > Question: how to read an address?
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Question: how to read an address?

Réponse
 
LinkBack Outils de la discussion
Vieux 06/06/2008, 15h33   #1
shuisheng
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Question: how to read an address?

Dear All,

I have a probably simple question but annoying me a lot.

For example:

std::stringstream ss;
int * p = new int(1);
ss << p; // Output the address
void * q;
ss >> q; // I want to input the address, but give a compilation error.
if (q != NULL)
...

Any way to read an address and see if it is valid?

Thanks a lot!

Shuisheng
  Réponse avec citation
Vieux 06/06/2008, 15h47   #2
Christian Hackl
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Question: how to read an address?

shuisheng wrote:

> std::stringstream ss;
> int * p = new int(1);
> ss << p; // Output the address
> void * q;
> ss >> q; // I want to input the address, but give a compilation error.
> if (q != NULL)
> ...


What's the error? The following complete program compiles fine with GCC
4.1.2, VC7 and Comeau 4.3.10.1 Beta (online).

#include <sstream>
#include <iostream>

int main()
{
std::stringstream ss;
int * p = new int(1);
ss << p;
void * q;
ss >> q;
std::cout << q << "\n";

}

But what are you trying to accomplish, anyway? Why don't you put the int
in the stream rather than its address?


--
Christian Hackl
  Réponse avec citation
Vieux 06/06/2008, 16h04   #3
shuisheng
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Question: how to read an address?

On Jun 6, 10:47 am, Christian Hackl <ha...@sbox.tugraz.at> wrote:
> shuisheng wrote:
> > std::stringstream ss;
> > int * p = new int(1);
> > ss << p; // Output the address
> > void * q;
> > ss >> q; // I want to input the address, but give a compilation error.
> > if (q != NULL)
> > ...

>
> What's the error? The following complete program compiles fine with GCC
> 4.1.2, VC7 and Comeau 4.3.10.1 Beta (online).
>
> #include <sstream>
> #include <iostream>
>
> int main()
> {
> std::stringstream ss;
> int * p = new int(1);
> ss << p;
> void * q;
> ss >> q;
> std::cout << q << "\n";
>
> }
>
> But what are you trying to accomplish, anyway? Why don't you put the int
> in the stream rather than its address?
>
> --
> Christian Hackl


You are right. I have a typo in my testing. I used "void q" rather
than "void* q".

What I am doing is coding a smart pointer. To output it, I output
address and value. In reading, if the address is NULL, it means the
pointer does not point a value.

Thank you so much!
  Réponse avec citation
Vieux 06/06/2008, 16h51   #4
Christian Hackl
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Question: how to read an address?

shuisheng wrote:

> On Jun 6, 10:47 am, Christian Hackl <ha...@sbox.tugraz.at> wrote:
>> But what are you trying to accomplish, anyway? Why don't you put the int
>> in the stream rather than its address?

>
> What I am doing is coding a smart pointer.


Is this some kind of exercise? If not, you should have a look at the
smart pointer classes offered by Boost:

http://www.boost.org/doc/libs/1_35_0.../smart_ptr.htm


--
Christian Hackl
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 17h33.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,90665 seconds with 12 queries