Connecting Tech Pros Worldwide Forums | Help | Site Map

How do explicit casts affect program performance ?

Newbie
 
Join Date: Nov 2008
Location: Northern Europe
Posts: 7
#1: Nov 21 '08
So as you all can see i would like to know how an explicit cast influences the performance of a program.

Thank you!

Lives Here
 
Join Date: Sep 2006
Posts: 12,070
#2: Nov 21 '08

re: How do explicit casts affect program performance ?


Depends on the types involved.
e.g for two classes, the run-time cost of a cast depends on the relative positions in the class hierarchy of the two classes involved.
Moderator
 
Join Date: Mar 2007
Location: North Bend Washington USA
Posts: 5,382
#3: Nov 21 '08

re: How do explicit casts affect program performance ?


In some cases the cast appears to work but in fact may cause indeterminate behavior. For example:
Expand|Select|Wrap|Line Numbers
  1. const int value = 10;
  2.    int* p = (int*)&value;
  3.    *p = 20;
  4.  
  5.    cout << value << " " << *p <<"\n"
  6.         << &value << " " << p << endl;
  7.  
Here an attempt is made to change a const by using a non-const pointer. When you run this code using Visual Studio.NET, value appears as 10 and *p appears as 20! Worse, &value and the address contained in p are the same address.

So the part of your program that uses values uses 10. The part that uses *p uses 20.

Bon appetit!
Expert
 
Join Date: Mar 2008
Location: Naperville, Illinois U.S.
Posts: 831
#4: Nov 21 '08

re: How do explicit casts affect program performance ?


For C (not C++), and assuming that the cast doesn't break the program, I don't believe an explicit cast has any effect on the program's execution time.
Member
 
Join Date: Nov 2008
Posts: 64
#5: Nov 21 '08

re: How do explicit casts affect program performance ?


I would say that the impact of a cast on a performance of a C program is negligible. Don't cast if you don't have to, but otherwise don't worry about it.
Newbie
 
Join Date: Nov 2008
Location: Northern Europe
Posts: 7
#6: Nov 21 '08

re: How do explicit casts affect program performance ?


@weaknessforcats

Wow, your snippet knocked me almost off my seat :)
BUT, after i played a little bit with the code it seems to me that this behaviour is c++ related, because the Code below is analog to yours only written in C and shows the expected output.
Hmm.. do you have an explanation ?

P.S.
it is a little bit off topic and sorry for my poor english.

Thanks for all replies!

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2.  
  3. int main(int argc, char* argv[])
  4. {
  5.     const int value = 10;
  6.     int* p = (int*)&value;
  7.     *p = 20;
  8.  
  9.     printf("*p\t\t\t = %d \n", *p);
  10.     printf("value\t\t\t = %d \n", value);
  11.     printf("address of p\t\t = %x \n", p);
  12.     printf("address of value\t = %x \n", &value);
  13.  
  14.     return 0;
  15. }
  16.  
  17. /*
  18. OUTPUT:
  19.  
  20. *p                      = 20 
  21. value                     = 20 
  22. address of p       = bfd004ac 
  23. address of value     = bfd004ac 
  24.  
  25. */
  26.  
Savage's Avatar
Expert
 
Join Date: Feb 2007
Posts: 1,737
#7: Nov 21 '08

re: How do explicit casts affect program performance ?


My guess is that this is happening in C++ and not in C because C++ treats c style casts in different manner(In C++ using c style casts is not recommended).In this particular case it tries to use const_cast to remove const-ines from the variable.It's C++ add-on to the old style cast(which seems to work)(const_cast is a last resort,it's provided in C++ just so that it can support old casting syntax).Now about why the const_cast fails,here is what MSDN has to say about it:

Quote:

Originally Posted by MSDN

A pointer to any object type or a pointer to a data member can be explicitly converted to a type that is identical except for the const, volatile, and __unaligned qualifiers. For pointers and references, the result will refer to the original object. For pointers to data members, the result will refer to the same member as the original (uncast) pointer to data member. Depending on the type of the referenced object, a write operation through the resulting pointer, reference, or pointer to data member might produce undefined behavior.

You cannot use the const_cast operator to directly override a constant variable's constant status.

The const_cast operator converts a null pointer value to the null pointer value of the destination type.

Reply