Thread: Casting

  1. #1
    Registered User
    Join Date
    Jun 2010
    Location
    norway
    Posts
    3

    Casting

    Hi.

    Does static casting really gve any overhead, when no real "conversion" is performed?

    Consider the following:
    Code:
    int i1 = 1;
    char *pc = (char*)&i1;
    int i2 = *(int*)pc;
    I could have used c++ style casts but i don't think it matters. The point is, does this kind of casting really take any time; or is it solely to ensure type safety?

    Thanks!

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    These are not static_casts, but reinterpret_casts. I would guess that it won't take any time, and it does the opposite of type safety.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Casting can cause overhead, yes. Consider when you have a 32-bit integer variable and suddenly want to convert it to a 64-bit variable. Unless you're on a 64-bit system, it will generate a move overhead for transferring the data over to a temporary.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Mar 2010
    Posts
    583
    Quote Originally Posted by tra86 View Post

    Consider the following:
    Code:
    int i1 = 1;
    char *pc = (char*)&i1;
    int i2 = *(int*)pc;
    This example has overhead, yes.
    As soon as you take the address of a variable it must be stored in memory. So if i1 was in a register, it's not anymore.
    Then the final line must reload from the address of pc. This is because the contents of that memory address could have changed by another process/thead since we wrote to it.

    You're also declaring new pointer variables which need to be stored somewhere, and that'll have register usage or stack cost.

    More generally:

    Code:
    int i = 1;
    char c = 2;
    i = (int)c;   // overhead -- value of c must be sign extended
    c = (char)i; // overhead -- value of i must have top bits masked
    I think for this example it is possible for the compiler to optimise and assign the constant 2 to both variables.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Casting to pointer from integer
    By @nthony in forum C Programming
    Replies: 3
    Last Post: 06-30-2010, 04:32 AM
  2. Advantages of c++ type casting over c type casting
    By kaibalya2008 in forum C++ Programming
    Replies: 10
    Last Post: 05-05-2009, 11:09 AM
  3. Casting Question (I think)
    By fayte in forum C Programming
    Replies: 6
    Last Post: 03-08-2006, 05:31 PM
  4. Type casting
    By Lionmane in forum C Programming
    Replies: 28
    Last Post: 08-20-2005, 02:16 PM
  5. question about casting pointers/other types also??
    By newbie02 in forum C++ Programming
    Replies: 3
    Last Post: 08-07-2003, 05:01 AM