10

I write more and more C applications, and now I wonder something about casts. In C++, a dynamic cast is a very costly operation (for instance a down-cast), but I don’t even know for static one.

In C, I had to write something like that:

assert ( p ); /* p is void* */
int v = *(int*)p;

Is it a « C dynamic-cast »? Is it quite the same as the static_cast<int*>(p) of C++? How much does it cost?

Thanks in advance.

jww
  • 97,681
  • 90
  • 411
  • 885
phaazon
  • 1,972
  • 15
  • 21
  • 2
    ...for appropriate definitions of "very"... – DevSolar Dec 06 '12 at 14:53
  • Possible duplicate of [How do C/C++ compilers handle type casting between types with different value ranges?](https://stackoverflow.com/questions/340413/how-do-c-c-compilers-handle-type-casting-between-types-with-different-value-ra) – jww Mar 08 '18 at 14:48

4 Answers4

8

A cast in C is only meaningful at compile time because it tells the compiler how you want to manipulate a piece of data. It does not change the actual value of the data. For example, (int*)p tells the compiler to treat p as a memory address to an integer. However this costs nothing at run time, the processor just deals with raw numbers the way they are given to it.

devrobf
  • 6,973
  • 2
  • 32
  • 46
7

A C cast of a pointer is more like a C++ reinterpret_cast. It instructs the compiler to treat a variable as being of a different type and costs nothing at runtime.

simonc
  • 41,632
  • 12
  • 85
  • 103
  • 1
    It is when casting from one pointer type to another, but in that case a `reinterpret_cast` does the same thing as a `static_cast`. Casting, say, a double to an integer does real work and is nothing like a `reinterpret_cast`. To me the comparison to `static_cast` seems much more apropriate than `dynamic_cast`. – sepp2k Dec 06 '12 at 14:58
  • @sepp2k I've updated my answer to include your point about casts of pointer types. I'm not sure I agree with you about static and reinterpret casts being identical for pointer types - doesn't the compiler check that static casts are between compatible types? (This is possibly going off on a tangent though... I do agree with and have taken notice of your main point) – simonc Dec 06 '12 at 15:04
  • 1
    You're right, `static_cast` is only identical to `reinterpret_cast` **if** it compiles. – sepp2k Dec 06 '12 at 15:07
  • @sepp2k this is not correct either. In C++11, a `reinterpret_cast` of `0` to a pointer type doesn't necessarily create a null pointer. – Johannes Schaub - litb Dec 06 '12 at 15:30
6

A C cast is more like all C++ style casts except dynamic_cast combined. So when you cast an int to another integer type, it is static_cast. When you cast pointers to other pointer types or to integer types or vice versa, it is reinterpret_cast. If you cast away a const, it is const_cast.

C does not have something similar to dynamic_cast since it has concept of types of objects and would have no use for them like C++ either (no virtual functions ...). Types with regard to interpreting an object's bits only become important when combined with expressions referring to objects, in C. The objects themselfs don't have types.

Johannes Schaub - litb
  • 496,577
  • 130
  • 894
  • 1,212
2

Pointers are pointers - casting a pointer is a noop.

It was a memory address before, it is a memory address afterwards.

It is essentially a statement "let's assume that this is a pointer to type x for future type checking".

So you might call this a reinterpret_cast in terms of C++, as it does not perform extra compile time type checking that e.g. dynamic_cast or a static_cast does.

I don't think C has equivalents of an dynamic_cast ("insert runtime type check here") or a static_cast ("perform extra compile time type checks here").

Note that for non-pointers things will behave slightly differently.

int b = 1;
double a = (double) b;

is not so much a cast, but an explicit type conversion.

Has QUIT--Anony-Mousse
  • 76,138
  • 12
  • 138
  • 194
  • Note that the standard does explicitly reserve the right to have pointers to different types being of different width... while this is an exotic case in today's world of 8-bit two-complement IEEE float architectures, the language lawyers might go after you for the "noop" remark. ;-) – DevSolar Dec 06 '12 at 14:57
  • Ok got it. So it’s more a _compiler_ **hint** than anything else? – phaazon Dec 06 '12 at 15:19
  • Not really a "hint" to the compiler: - it does not help the compiler to optimize anything. But it is a *programmer* thing. I don't let the compiler *guess* this is correct, but I *explicitly state* that this should be cast, and is not a typo; it merely tells the compiler that this is "not an error, but meant this way". – Has QUIT--Anony-Mousse Dec 06 '12 at 17:02
  • C++ goes a step further here: `dynamic_cast` tells the compiler to perform extra type checks (at runtime), and `static_cast` tells the compiler to check the cast and fail if it cannot be checked (by the compiler). – Has QUIT--Anony-Mousse Dec 06 '12 at 17:05