Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Re: dereferencing void *

Reply
Thread Tools

Re: dereferencing void *

 
 
Robert W Hand
Guest
Posts: n/a
 
      07-06-2003
On 6 Jul 2003 12:19:28 -0700, http://www.velocityreviews.com/forums/(E-Mail Removed) (maxw_cc) wrote:

>>You could also do something like this:
>>
>> int a = 4;
>> void* addr = &a;
>> float b;
>> b = *((float*)addr);

<snip>
>Now about the code you just mentioned, will it be violating some
>constraint (or semantic constraint) or the only reason it's invalid
>is because of practical usage (in practice it wouldn't yield valuable
>or useful result)?


Hopefully the standard will reflect practical issues and usage.

Subclause 6.5/7 clearly lists what expressions can access objects.
First of all the expression must be an lvalue. I've copied the exact
language(*) at the end, but the gist is that the type of the
expression must be essentially the same as the type of the object or
be a character type.

In other words, if an object is of type X, then it can be read by
expressions of type X, const X, volatile X, char and other character
types, or for integer types X, signed X, unsigned X, signed const X,
unsigned const X, etc.

So you are not allowed to read a float object using an expression of
type int. It is an example of undefined behavior. It might work out
and it might not.

(*)
a type compatible with the effective type of the object,
a qualified version of a type compatible with the effective type of
the object,
a type that is the signed or unsigned type corresponding to the
effective type of the object,
a type that is the signed or unsigned type corresponding to a
qualified version of the effective type of the object,
an aggregate or union type that includes one of the aforementioned
types among its members (including, recursively, a member of a
subaggregate or contained union), or
a character type.

Best wishes,

Bob
 
Reply With Quote
 
 
 
 
maxw_cc
Guest
Posts: n/a
 
      07-07-2003
Robert W Hand <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>. ..
> On 6 Jul 2003 12:19:28 -0700, (E-Mail Removed) (maxw_cc) wrote:
>
> >>You could also do something like this:
> >>
> >> int a = 4;
> >> void* addr = &a;
> >> float b;
> >> b = *((float*)addr);

> <snip>
> >Now about the code you just mentioned, will it be violating some
> >constraint (or semantic constraint) or the only reason it's invalid
> >is because of practical usage (in practice it wouldn't yield valuable
> >or useful result)?

>
> Hopefully the standard will reflect practical issues and usage.
>
> Subclause 6.5/7 clearly lists what expressions can access objects.
> First of all the expression must be an lvalue. I've copied the exact
> language(*) at the end, but the gist is that the type of the
> expression must be essentially the same as the type of the object or
> be a character type.
>
> In other words, if an object is of type X, then it can be read by
> expressions of type X, const X, volatile X, char and other character
> types, or for integer types X, signed X, unsigned X, signed const X,
> unsigned const X, etc.
>
> So you are not allowed to read a float object using an expression of
> type int. It is an example of undefined behavior. It might work out
> and it might not.


....[snip]...

Thank you very much Bob for sharing with us
your knowledge and valuable information.

Max
 
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
What is the difference between void proba(); and void proba(void); ??? PencoOdStip@gmail.com C++ 1 05-23-2007 07:12 PM
what is the difference, void func(void) and void fucn() noblesantosh@yahoo.com C Programming 5 07-22-2005 04:38 PM
"void Method()" vs "void Method(void)" Ollej Reemt C++ 7 04-22-2005 03:47 AM
`void **' revisited: void *pop(void **root) Stig Brautaset C Programming 15 10-28-2003 09:03 AM
Re: dereferencing void * Jun Woong C Programming 0 07-06-2003 10:12 AM



Advertisments