473,387 Members | 1,440 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,387 software developers and data experts.

Why can't I static_cast to a pointer type?

int main()
{
char const* const p =
static_cast< char const* const >( 0xffffffff );
}
Fails to compile with g++; message is:

g++ -c -o main.o main.cc
main.cc: In function `int main()':
main.cc:3: error: invalid static_cast from type `unsigned int' to type
`const
char* const'
make: *** [main.o] Error 1
Why doesn't this work? How can one write to specific addresses? For
example, Lupher Cypher posted a C-style cast to "char* far", in an
attempt to write to a text display. I'm willing to accept that "far"
might be a necessary extension for some architectures, e.g. 16-bit chips
in PDA's with 32 MB of RAM. On such an architecture, he may well need
to write data directly to particular addresses in order to communicate
with a memory-mapped device; how is he supposed to do it? Are the old
C-style casts the only way?

Special thanks to anyone with experience on this issue.

-Jeff

Jul 22 '05 #1
2 12208
Jeff Schwab wrote in news:o5********************@comcast.com:
int main()
{
char const* const p =
static_cast< char const* const >( 0xffffffff );
}

try:

reinterpret_cast< char const* const >( 0xFFFFFFFF );
Check the spelling though, its not very often I get to write this cast
(making the cast ugly(tm) and thus difficult to spell (for the likes of
me anyway :) was as I understand it intentional.

How (and in what way its meanfull/usefull) is implemenation defined.

But if you know the bit layout of your platform's pointers, it should
do what you want.

Fails to compile with g++; message is:
[snip]
Why doesn't this work? How can one write to specific addresses? For
example, Lupher Cypher posted a C-style cast to "char* far", in an
attempt to write to a text display. I'm willing to accept that "far"
might be a necessary extension for some architectures, e.g. 16-bit
chips in PDA's with 32 MB of RAM. On such an architecture, he may
well need to write data directly to particular addresses in order to
communicate with a memory-mapped device; how is he supposed to do it?
Are the old C-style casts the only way?


I would hope that the compilers/libraries that come with such systems
would provide a mechanism to do this (worst case a macro maybe).
Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #2
Rob Williscroft wrote:
Jeff Schwab wrote in news:o5********************@comcast.com:

int main()
{
char const* const p =
static_cast< char const* const >( 0xffffffff );
}
try:

reinterpret_cast< char const* const >( 0xFFFFFFFF );

Bingo! Thanks, Rob.
Check the spelling though, its not very often I get to write this cast
(making the cast ugly(tm) and thus difficult to spell (for the likes of
me anyway :) was as I understand it intentional.

How (and in what way its meanfull/usefull) is implemenation defined.

But if you know the bit layout of your platform's pointers, it should
do what you want.

Fails to compile with g++; message is:


[snip]
Why doesn't this work? How can one write to specific addresses? For
example, Lupher Cypher posted a C-style cast to "char* far", in an
attempt to write to a text display. I'm willing to accept that "far"
might be a necessary extension for some architectures, e.g. 16-bit
chips in PDA's with 32 MB of RAM. On such an architecture, he may
well need to write data directly to particular addresses in order to
communicate with a memory-mapped device; how is he supposed to do it?
Are the old C-style casts the only way?

I would hope that the compilers/libraries that come with such systems
would provide a mechanism to do this (worst case a macro maybe).
Rob.


Jul 22 '05 #3

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

Similar topics

7
by: buds | last post by:
Hi all, Following is the assigment operator of a derived class Derived& Derived::operator=(const Derived& inDerived) { //to assign to the base class object the following statement ...
3
by: Roy Yao | last post by:
Hello, I need to pass a pointer to a callback function to the lower level modules. But the function is thought to be a virtual member one. How can I get the real address of the virtual...
26
by: Steven T. Hatton | last post by:
The code shown below is an example from the Coin3D documentation. I believe the use of the C-style cast is safe under the circumstances, but from what I've been exposed to (TC++PL(SE)), I would...
2
by: Amit | last post by:
Greetings. I am having some problem while using a cast operation(static_cast and/or dynamic_cast) between base and derived objects when passing to functions. what I have is something like this.. ...
10
by: danibe | last post by:
I never had any problems storing pointers in STL containers such std::vector and std::map. The benefit of storing pointers instead of the objects themselves is mainly saving memory resources and...
19
by: PengYu.UT | last post by:
I see some code use static_cast<some_pointer_type>(0) instead of NULL to describe null pointer. I'm wondering what is the pros and cons of each way. Is there any reason why we should one verses the...
24
by: Rahul | last post by:
Hi, I have a class A : public B {...member functions......data members}; and am doing the following A *p=new A(); void *p=static_cast<void *>(p); factory_instance->process(p);
9
by: Vincent RICHOMME | last post by:
Is there any reason to use static_cast instead of old C syntax ? Let's say I declare GLfloat test = static_cast<GLfloat>(x); or GLfloat test = (GLfloat) x;
4
by: subramanian100in | last post by:
when are the use of static_cast and const_cast essential ? Stroustrup, in his book TC++PL(3rd edition) in page number 139 (in Section 6.5 Advice - ), has advised "Avoid explicit type...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.