example rquired

S

siju

Hi all,
the following post was saying that
http://groups.google.com/group/comp.lang.c/search?q=aarklon&start=30&sa=N&

The language doesn't allow conversions from floating-point to poitner
types, either implicitly or explicitly.
float x = 1.0f;
void *p = *((void**)&x);

This piece of code does not perform a _conversion_ of floating-point
value to pointer type. What you have here is a _reinterpretation_ of
floating-point lvalue as pointer lvalue (raw memory reinterpretation).
This is a completely different thing in C language world.

For example, in C language _converting_ a pointer of type 'T*' to
'void*' type and back is defined and has its uses. However, trying to
_reinterpret_ a pointer of type 'T*' as a 'void*' pointer (using the
above technique) leads to undefined results. Feel the difference.

now my questions are

1) in C language _converting_ a pointer of type 'T*' to
'void*' type and back is defined and has its uses.

can anyone give examples especially on uses???

2) However, trying to _reinterpret_ a pointer of type 'T*' as a
'void*' pointer (using the above technique) leads to undefined
results.

explanation on un defined results ??
 
J

Jack Klein

Hi all,
the following post was saying that
http://groups.google.com/group/comp.lang.c/search?q=aarklon&start=30&sa=N&

The language doesn't allow conversions from floating-point to poitner
types, either implicitly or explicitly.

That is almost correct. The C standard allows the conversion, with a
suitable cast, between pointers and integer types. Conversion of a
pointer to an integer has implementation-defined behavior if the
result of the conversion fits in the destination integer type,
undefined behavior otherwise.

If an implementation has an integer type capable of holding a pointer
value, conversion of a pointer value to this integer type, via a cast,
and then back again to a pointer to the original type, again via a
cast, will result in a pointer that points to the same object.

But you can convert from a floating point type to a pointer type using
two casts, of course.

double d = 3.14159;
void *vp = (void *)((int)d);

So if you want to be completely correct, you would have to say that
you can't directly convert between floating point and pointer types,
implicitly or explicitly.
This piece of code does not perform a _conversion_ of floating-point
value to pointer type. What you have here is a _reinterpretation_ of
floating-point lvalue as pointer lvalue (raw memory reinterpretation).
This is a completely different thing in C language world.

What you have is undefined behavior, pure and simple. It is invalid
to access the object representation of a double as anything other than
a double, or as an array of character types the size of a double. The
later is only truly safe if the character type used is unsigned char.
For example, in C language _converting_ a pointer of type 'T*' to
'void*' type and back is defined and has its uses. However, trying to
_reinterpret_ a pointer of type 'T*' as a 'void*' pointer (using the
above technique) leads to undefined results. Feel the difference.

now my questions are

1) in C language _converting_ a pointer of type 'T*' to
'void*' type and back is defined and has its uses.

That is only true if T is a complete or incomplete object type. It is
not true if T is a function type. There is no defined conversion
between function pointers and object pointers.
can anyone give examples especially on uses???

Look up functions such as malloc(), free(), and qsort() in your C
reference.
2) However, trying to _reinterpret_ a pointer of type 'T*' as a
'void*' pointer (using the above technique) leads to undefined
results.

There are only certain ways that one is allowed to access the value of
an object in C to have guaranteed defined results.

1. Access as the type of the object, or a compatible type.

2. Access as an array of unsigned chars.

It just so happens that C requires that pointer to char and pointer to
void have the same type and representation, so you can access the
value of a pointer to char as a pointer to void and vice versa.
explanation on un defined results ??

Type "C undefined behavior" into Google and look at the first result.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
 
M

Malcolm McLean

siju said:
1) in C language _converting_ a pointer of type 'T*' to
'void*' type and back is defined and has its uses.

can anyone give examples especially on uses???
Look up qsort(). It uses void *s, and also shows why void*s are necessary.
2) However, trying to _reinterpret_ a pointer of type 'T*' as a
'void*' pointer (using the above technique) leads to undefined
results.

explanation on un defined results ??
This is a subtle one.

int *ptr;
void *vptr;

/* bad */
memcpy(&ptr, &vptr, sizeof(void *));
/* good */
ptr = vptr;

The reason is that void*s might carry about extra baggage. For instance some
machines have a 32-byte architecture, but implent 8 bit chars on top of it.
So char *s have an extra two bits to indicate offset. void *s obvious need
these extra two bits, because they can be converted to char *s and back.
So sizeof(int *) might not equal sizeof(void *).
 
K

Keith Thompson

Jack Klein said:
That is almost correct. The C standard allows the conversion, with a
suitable cast, between pointers and integer types. Conversion of a
pointer to an integer has implementation-defined behavior if the
result of the conversion fits in the destination integer type,
undefined behavior otherwise.

If an implementation has an integer type capable of holding a pointer
value, conversion of a pointer value to this integer type, via a cast,
and then back again to a pointer to the original type, again via a
cast, will result in a pointer that points to the same object.

But you can convert from a floating point type to a pointer type using
two casts, of course.

double d = 3.14159;
void *vp = (void *)((int)d);

So if you want to be completely correct, you would have to say that
you can't directly convert between floating point and pointer types,
implicitly or explicitly.
[...]

Are you sure about that? What constraint does it violate?

C99 6.5.4, "Cast operators", constrains the source and target types to
be scalar (unless the target is void). C99 6.3, "Conversions",
doesn't define the behavior of conversions between floating-point and
pointer types, but there are no constraints in that section.

As far as I can tell, conversion between a pointer type and a
floating-point type is legal, but the behavior is undefined. (IMHO it
would make more sense if it were illegal, but the standard doesn't
seem to say so.)
 
J

Jack Klein

Jack Klein said:
That is almost correct. The C standard allows the conversion, with a
suitable cast, between pointers and integer types. Conversion of a
pointer to an integer has implementation-defined behavior if the
result of the conversion fits in the destination integer type,
undefined behavior otherwise.

If an implementation has an integer type capable of holding a pointer
value, conversion of a pointer value to this integer type, via a cast,
and then back again to a pointer to the original type, again via a
cast, will result in a pointer that points to the same object.

But you can convert from a floating point type to a pointer type using
two casts, of course.

double d = 3.14159;
void *vp = (void *)((int)d);

So if you want to be completely correct, you would have to say that
you can't directly convert between floating point and pointer types,
implicitly or explicitly.
[...]

Are you sure about that? What constraint does it violate?

I don't believe that I said it violated a constraint. Admittedly,
"can't" could mean either a constraint violation or undefined
behavior, and I did not specify which, but either one is good for
"can't" in my view. In any cast, I meant undefined behavior, and not
a constraint violation.
C99 6.5.4, "Cast operators", constrains the source and target types to
be scalar (unless the target is void). C99 6.3, "Conversions",
doesn't define the behavior of conversions between floating-point and
pointer types, but there are no constraints in that section.

As far as I can tell, conversion between a pointer type and a
floating-point type is legal, but the behavior is undefined. (IMHO it
would make more sense if it were illegal, but the standard doesn't
seem to say so.)

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top