Solved

Another casting question

Posted on 2002-04-24
3
306 Views
Last Modified: 2010-04-15
Hi

One of my fellow co-workers said that double-casting
has no effect
such that code like
uint32 *value
value = (uint32*)((uint8 *)(some memory address));
is equivalent to
value = (uint32*)(some memory address);

is he correct ?

I would expect the result to be double casted

Consider memory looking like
01 02 03 04
I would then expect this
value = (uint32*)(some memory address);
to contain "04 03 02 01" (when printed)

and I would expect this
value = (uint32*)((uint8 *)(some memory address));
to contain "01 00 00 00" (when printed)

Comments ?

0
Comment
Question by:CrypToniC
3 Comments
 
LVL 6

Accepted Solution

by:
Triskelion earned 100 total points
ID: 6965639
When you're casting pointers, the effect (with immediate use) does nothing.  It may make the compiler happy depending on your warning level.

If you're using direct data types (not pointers) such as casting a long to a short then back to a long, there will be truncation.
0
 
LVL 1

Author Comment

by:CrypToniC
ID: 6965656
Don't you just hate when other people are right ?

:)

Thanks
/CrypToniC
0
 
LVL 5

Expert Comment

by:nebeker
ID: 6965734
It depends on what you're casting.  In the example you've given, the cast really has no effect, because you're casting from one pointer type (uint8) to another pointer type (uint32).  All pointers are the same size, so there is really nothing to convert...

In your example, your casts are NOT dereferencing the memory address, so you aren't doing anything to change the value.

If you're casting between different types, then the double casting will most definitely have an effect.

If you wanted the above code to show the effects of the double cast, you would need to cast the _contents_ of the memory address, not the address itself, and to do that, you need to dereference the pointers...

For example:

uint32 *value;
uint32 x = 300;   /* for 'some memory address' */

If you do this:

value = (uint32*) (uint8 *) x;

then (*value) will be 300.

However, if you dereference the pointer, it will be converted to the type represented by that pointer.  For example:

value = (uint32*) * (uint8*) x;

then (*value) will be 44 (i.e. the lower 8 bits)...

Basically, if you have multiple casts, they will be evaluated one at a time, from right to left, causing a conversion each time.  So if you have the statement:

double x = (double)(float)(int)(short)(char) 0x1234567890;

the value "0x1234567890" will be truncated to the size of a char, then expanded to the size of a short, then an int, converted to a float, then to a double.
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question