Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Implementation -defined behavior

Reply
Thread Tools

Implementation -defined behavior

 
 
amit.codename13@gmail.com
Guest
Posts: n/a
 
      05-06-2009
will the following code have implementation defined behavior???

int main()
{
int *i,j;
i=(int*)10;
return 0;
}

is it certain as to what value is stored in i???
 
Reply With Quote
 
 
 
 
Antoninus Twink
Guest
Posts: n/a
 
      05-06-2009
On 6 May 2009 at 22:02, http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> int main()
> {
> int *i,j;
> i=(int*)10;
> return 0;
> }
>
> is it certain as to what value is stored in i???


Yes it is. (The representation of that value is not certain: it may be
little or big endian depending on the architecture the code is compiled
for.)

Pointers are interchangeable with the signed integer type intptr_t, and
/any/ integer type is guaranteed to be large enough to store 10 without
overflow.

In practise, of course, intptr_t will either be int32_t or int64_t on a
modern non-embedded implementation.

 
Reply With Quote
 
 
 
 
Flash Gordon
Guest
Posts: n/a
 
      05-06-2009
Antoninus Twink wrote:
> On 6 May 2009 at 22:02, (E-Mail Removed) wrote:
>> int main()
>> {
>> int *i,j;
>> i=(int*)10;
>> return 0;
>> }
>>
>> is it certain as to what value is stored in i???

>
> Yes it is.


<snip>

Wrong. It could be a trap and if it isn't the conversion is
implementation defined. I can't be bothered to correct all the other
errors you made.
--
Flash Gordon
 
Reply With Quote
 
Antoninus Twink
Guest
Posts: n/a
 
      05-07-2009
On 6 May 2009 at 23:15, Flash Gordon wrote:
> Wrong. It could be a trap and if it isn't the conversion is
> implementation defined.


Let's break it up into an extra step then, to be clear about what's
going on.

int *ip;
intptr_t i;
i = 10;
ip = (int *) i;
assert((intptr_t) i == 10);

Would you like to name me an implementation where this code compiles and
the assertion fails when it runs?

 
Reply With Quote
 
amit.codename13@gmail.com
Guest
Posts: n/a
 
      05-07-2009
On May 7, 4:15*am, Antoninus Twink <(E-Mail Removed)> wrote:
> On *6 May 2009 at 23:15, Flash Gordon wrote:
>
> > Wrong. It could be a trap and if it isn't the conversion is
> > implementation defined.

>
> Let's break it up into an extra step then, to be clear about what's
> going on.
>
> int *ip;
> intptr_t i;
> i = 10;
> ip = (int *) i;
> assert((intptr_t) i == 10);
>
> Would you like to name me an implementation where this code compiles and
> the assertion fails when it runs?


thats exactly what i need the answer for...
 
Reply With Quote
 
Antoninus Twink
Guest
Posts: n/a
 
      05-07-2009
On 7 May 2009 at 19:55, Eric Sosman wrote:
> (Unfortunately, one careless respondent has given Question B's answer
> for Question A.)

[snip]

I think we are in complete agreement here, Eric. Why not leave the
stupid polemics to the Heathfield-Thomson-Falconer trolling machine?

 
Reply With Quote
 
amit.codename13@gmail.com
Guest
Posts: n/a
 
      05-08-2009
On May 7, 12:55*pm, Eric Sosman <(E-Mail Removed)> wrote:
> (E-Mail Removed) wrote:
> > On May 7, 4:15 am, Antoninus Twink <(E-Mail Removed)> wrote:
> >> On *6 May 2009 at 23:15, Flash Gordon wrote:

>
> >>> Wrong. It could be a trap and if it isn't the conversion is
> >>> implementation defined.
> >> Let's break it up into an extra step then, to be clear about what's
> >> going on.

>
> >> int *ip;
> >> intptr_t i;
> >> i = 10;
> >> ip = (int *) i;
> >> assert((intptr_t) i == 10);

>
> >> Would you like to name me an implementation where this code compiles and
> >> the assertion fails when it runs?

>
> > thats exactly what i need the answer for...

>
> * * *Note that you've changed your question. *You began *with
> "Is it certain?" and now you've switched to something more like
> "Is it likely?" *You should not be surprised when different
> questions get different answers. *(Unfortunately, one careless
> respondent has given Question B's answer for Question A.)
>
> * * *"Is it certain" that converting 10 to an int* gives a known
> value? *No, it is not. *Ints can be converted to pointers (and
> vice versa), but everything about the conversion is implementation-
> defined. *Even the validity of the converted value is up to the
> implementation.
>
> * * *"Is it likely" that converting 10 to an int* gives a known
> value? *Yes, it is. *On most machines, pointers behave very much
> like some flavor of integer, and there is a natural correspondence
> between pointer values and integer values. *The value 10 is almost
> certainly included in the range of the correspondence.
>
> * * *A question you didn't ask, but might have: "Is the converted
> value a valid int* pointer value?" *Possibly, but probably not.
> On many systems, the int corresponding to an int* must be a multiple
> of four. *Even on those with more relaxed alignment requirements,
> it is common to find that "low core" addresses are off-limits and
> inaccessible.
>
> --
> (E-Mail Removed)


that was bad on my side... you have answered all the questions that i
wanted to get answer of...

thanks...

i misinterpreted what antonius told cos i was little biased that it is
*guareenteed* that the assertion that a value 10 is stored in i would
never fail

 
Reply With Quote
 
James Kuyper
Guest
Posts: n/a
 
      05-08-2009
(E-Mail Removed) wrote:
....
> i misinterpreted what antonius told cos i was little biased that it is
> *guareenteed* that the assertion that a value 10 is stored in i would
> never fail


A key point to understand is that a value of 10 cannot be stored in an
pointer. The value of a pointer is the location in memory that it points
at. That location might have an address of 10, and that address might be
stored in the representation of the pointer, but the actual value of the
pointer is not 10.

On a more complicated level, Antonius' comments to the contrary
notwithstanding, converting a value of 10 into a pointer is not
guaranteed to produce a pointer that, when converted back to an integer
type, will have a value of 10. That's possible, and commonplace, but not
guaranteed.

It is guaranteed that converting a valid pointer value to a [u]intptr_t
(if available) and back to it's original type will produce a pointer
value that compares equal to the original. It might seem that this
guarantee implies the other guarantee, but it doesn't; multiple
different integer values might convert to the same pointer value,
without violating the guarantee, but the reverse conversion can produce
only one of those integer values, which need not be the same as the one
you started with.
 
Reply With Quote
 
Antoninus Twink
Guest
Posts: n/a
 
      05-08-2009
On 8 May 2009 at 13:25, James Kuyper wrote:
> Multiple different integer values might convert to the same pointer
> value, without violating the guarantee, but the reverse conversion can
> produce only one of those integer values, which need not be the same
> as the one you started with.


I repeat my invitation to name an implementation for which this is the
case.

 
Reply With Quote
 
Ike Naar
Guest
Posts: n/a
 
      05-08-2009
In article <(E-Mail Removed)>,
Antoninus Twink <(E-Mail Removed)> wrote:
>Let's break it up into an extra step then, to be clear about what's
>going on.
>
>int *ip;
>intptr_t i;
>i = 10;
>ip = (int *) i;
>assert((intptr_t) i == 10);


You're converting i, an intptr_t that has the value 10, to type intptr_t,
and then assert that the converted value equals 10 . What's the point?

Or did you mean ``assert((intptr_t) ip == 10);'' ?
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Insertion Sort : C++ implementation 100 times slower than C implementation sanket C++ 7 11-03-2011 05:00 AM
The behavior of "equals" method of Arrays.asList() implementation Alex J Java 4 07-22-2011 12:36 AM
Rules for "undefined/implementation-defined behavior" Jon C Programming 1 11-08-2010 01:49 AM
Knowing the implementation, are all undefined behaviours become implementation-defined behaviours? Michael Tsang C Programming 54 03-30-2010 07:46 AM
Knowing the implementation, are all undefined behaviours become implementation-defined behaviours? Michael Tsang C++ 32 03-01-2010 09:15 PM



Advertisments