473,725 Members | 2,281 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Casting a pointer to an int and back

Is it well-defined to make a cast from a pointer to an int and back?
Like:

typedef struct
{
int whatever;
} S;

int main(void)
{
S* s = malloc(sizeof(S ));
int i = (int) s;
S* t = (S*) i;
t.whatever = 42;
etc.
}

/David

Feb 22 '06
24 2468
On 22 Feb 2006 16:04:28 GMT, in comp.lang.c , "Richard G. Riley"
<rg***********@ gmail.com> wrote:

Did you read the OP?
Yes. So what? (quoting me)
that most regulars will review code *in its entirety*, partly on the
(often correct) assumption that the OP may be completely mistaken
about the cause of their bug, partly out of kindness.


The review of the code wasnt the issue. The question about casting was.


Did you actually read *and understand* what I wrote?
If you don't post the code that went wrong, how can someone diagnose
it? And if you want to post pseudocode, then say so.


So you would have thought that "etc." was his code would you?


Of course not, don't be disingenuous, But either he copied a chunk of
his code, snipped it off at a certain point and wrote etc afterwards,
or he retyped the code from memory, which is in any events a horrible
idea.
Why it takes so many people to police posters looking for help is
foreign to me.


When you've been around usenet as long as I have, you will appreciate
topicality, and for what its worth, the posts in this thread were not
'policing' but advice.

Mark McIntyre
--
"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan

----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Feb 22 '06 #21
Jordan Abel <ra*******@gmai l.com> wrote:

# The universal pointer type was never int - it was char * before it was
# void *.

Actually it was, and has been enshrined in APIs that have
been around for 30 years like yacc and yyparse(). Then it
became (char*) and then (void*).

--
SM Ryan http://www.rawbw.com/~wyrmwif/
I'm not even supposed to be here today.
Feb 23 '06 #22
On 2006-02-23, SM Ryan <wy*****@tang o-sierra-oscar-foxtrot-tango.fake.org> wrote:
Jordan Abel <ra*******@gmai l.com> wrote:

# The universal pointer type was never int - it was char * before it was
# void *.

Actually it was, and has been enshrined in APIs that have
been around for 30 years like yacc and yyparse(). Then it
became (char*) and then (void*).

--
SM Ryan http://www.rawbw.com/~wyrmwif/
I'm not even supposed to be here today.


Q: isnt it common consensus in the C world that pointers are always the HW
word size of the underlying architecture? So, I'm still waiting for
one concreate example of where pointers to FP types are a different
size to pointer to ints. It might not be portable in terms of true C,
but for someone wanting to do something highly optimized it is a safe
assumption isnt it?

--
Remove evomer to reply
Feb 23 '06 #23
On 22 Feb 2006 12:10:39 GMT, "Richard G. Riley"
<rg***********@ gmail.com> wrote:
On 2006-02-22, Vladimir S. Oka <no****@btopenw orld.com> wrote:
Richard G. Riley wrote:
On 2006-02-22, Vladimir S. Oka <no****@btopenw orld.com> wrote:
>> etc.
>
> This is a syntax error.

Well spotted. Go to top of the pedants class.


Yes! I finally made it!

Snipping relevant parts of the post is bad etiquette. I also said:
> Please post full, compileable, minimal examples.


Which was the whole point. It's in the OP's interest, really.


The snipped part had no relevance on my pithy reply to your pedant
prize winning reply. The guy was asking a question about casting : not for a
code review or smart assed comments on "etc." being a syntx error.


Silly me. I thought the syntax error was t.whatever since t is a
pointer to struct. Maybe you don't think that was relevant either.
Remove del for email
Feb 24 '06 #24
pete wrote:
Richard Bos wrote:
"pi************ @gmail.com" <pi************ @gmail.com> wrote:
Is it well-defined to make a cast from a pointer to an int and back?
Like:

typedef struct
{
int whatever;
} S;

int main(void)
{
S* s = malloc(sizeof(S ));
int i = (int) s;
S* t = (S*) i;
t.whatever = 42;
etc.
} On two conditions:
- that the cast from pointer to int doesn't lose any information,
i.e.,
that an int is wide enough to hold all pointer values;
- that the pointer is cast to int and then back to the _same_ pointer
type;
AFAICT this is well-defined.


AFAICT it's implementation defined.

N869
6.3.2.3 Pointers
[#5] An integer may be converted to any pointer type.


<snip>
implementation-defined. If the result cannot be represented
in the integer type, the behavior is undefined. The result
need not be in the range of values of any integer type.


but the implementation may define it as undefined (not integer type
large enough for any address). Or as undefined if the pointer happens to
be to an address outside a specific range (some pointers convert to
integers within the range of the largest integer type, so fo integers
outside the range), and of course there is no way to determine if the
address is within that range...
--
Flash Gordon
Living in interesting times.
Web site - http://home.flash-gordon.me.uk/
comp.lang.c posting guidlines and intro -
http://clc-wiki.net/wiki/Intro_to_clc
Feb 25 '06 #25

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

Similar topics

4
3478
by: Jacob Jensen | last post by:
This question has probably been asked a million time, but here it comes again. I want to learn the difference between the three type cast operators: static_cast, reinterpret_cast, dynamic_cast. A good way to do this is by example. So I will give an example and please tell me what you think: I have a base class A with a virtual destructor, and a class B that is it inherits publicly from A and defines som extra stuff.
16
2435
by: He Shiming | last post by:
Hi, I'm having a little bit of trouble regarding pointer casting in my program. I don't understand why the following two cases produce different results. Case 1: IInterface *pInterface = new CImplementation(); pInterface->Method(); Case 2:
8
5660
by: wkaras | last post by:
In my compiler, the following code generates an error: union U { int i; double d; }; U u; int *ip = &u.i; U *up = static_cast<U *>(ip); // error I have to change the cast to reinterpret_cast for the code
31
3179
by: dragoncoder | last post by:
Consider the code class A { private: int a; }; int main(void) { A x; int* ptr = (int*)&x;
33
3656
by: Mark P | last post by:
A colleague asked me something along the lines of the following today. For some type X he has: X* px = new X; Then he wants to convert px to a char* (I'm guessing for the purpose of serializing the object array). I can think of three ways to do this:
3
3652
by: Beta What | last post by:
Hello, I have a question about casting a function pointer. Say I want to make a generic module (say some ADT implementation) that requires a function pointer from the 'actual/other modules' that takes arguments of type (void *) because the ADT must be able to deal with any type of data. In my actual code, I will code the function to take arguments of their real types, then when I pass this pointer through an interface function, I...
8
4271
by: Gamma | last post by:
I'm trying to inherit subclass from System.Diagnostics.Process, but whenever I cast a "Process" object to it's subclass, I encounter an exception "System.InvalidCastException" ("Specified cast is not valid"). How do I fix it ? using System.Diagnostics; .. .. class NewProcess: Process {
7
2648
by: William S Fulton | last post by:
I'm looking for the name of the following casting style in order to do some reading around on it and why it is sometimes used. unsigned long long ull = 0; void * ptr = 0; ull = *(unsigned long long*)&ptr; As opposed to the more usual casting:
5
5926
by: brekehan | last post by:
I've always been a little sketchy on the differences between static, dynamic, and reinterpret casting. I am looking to clean up the following block by using C++ casting instead of the C style casting. from what I am reading, I should use reinterpret cast in this situation, is that correct? Why does static and dynamic casting fail me? // please excuse the windows types, it is necessary in this code, // but the question remains C++ related
9
2458
by: Jess | last post by:
Hello, It seems both static_cast and dynamic_cast can cast a base class pointer/reference to a derived class pointer/reference. If so, is there any difference between them? In addition, if I have a derived class object and then upcast it to its base class, which cast operator should I use? Is it static_cast, or, can I simply cast it implicitly without any operator? Does this upcasting remove the derived class portion of the object?...
0
8889
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8752
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9401
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9257
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9116
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8099
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6702
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4519
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3228
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.