473,396 Members | 2,011 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,396 software developers and data experts.

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
Jun 27 '08 #1
3 1248
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
Jun 27 '08 #2
On Jun 6, 10:47 am, Christian Hackl <ha...@sbox.tugraz.atwrote:
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!
Jun 27 '08 #3
shuisheng wrote:
On Jun 6, 10:47 am, Christian Hackl <ha...@sbox.tugraz.atwrote:
>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
Jun 27 '08 #4

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

Similar topics

6
by: Jake D | last post by:
I understand how to load a DLL in C++ and use functions from it, however I have a DLL which I did not create which has some classes in it. I can't seem to find out how to use the classes to make...
1
by: kazack | last post by:
Hi all it's me again with another question as I got further in my book. The chapter I am in covers structres, abstract data and classes. I only read through to the end of the coverage on...
8
by: Chad Cartwright | last post by:
I was coding a very basic C++ application, and all I wanted to do was read a space of memory at 0x005CE000 over and over again. There is another program running that is constantly writing to this...
55
by: Steve Jorgensen | last post by:
In a recent thread, RKC (correctly, I believe), took issue with my use of multiple parameters in a Property Let procedure to pass dimensional arguments on the basis that, although it works, it's...
4
by: anonymous | last post by:
Thanks your reply. The article I read is from www.hakin9.org/en/attachments/stackoverflow_en.pdf. And you're right. I don't know it very clearly. And that's why I want to understand it; for it's...
3
by: reageer | last post by:
Hi all, I have a design question: I have a bunch of users (name, address, zip, etc.). They are assigned a card with a specific id. The only thing unique is this card id, or probably the...
39
by: anonymous | last post by:
Dear All, >From my understanding of pointers, a pointer should not be able to access a memory location until that memory has been allocated either by assiging the address of a variable or...
4
by: jthep | last post by:
If I have a pointer say int **pa that stores the address to another pointer int *pb, pa would be storing the address of pb which contains an address to lets say int c = 10. Picking an arbitrary...
21
by: Chad | last post by:
At the following url http://c-faq.com/lib/qsort2.html, they have the following Q: Now I'm trying to sort an array of structures with qsort. My comparison function takes pointers to structures,...
1
by: Keaven Pineau | last post by:
Hello All, I am very new to C# since my job consist to design/develop CE drivers but a client wants to use my C API into a .NET envrironment. I have 2 functions I would like to get help with. ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.