6

In C++ I have a function that only requires read-only access to an array but is mistakenly declared as receiving a non-const pointer:

size_t countZeroes( int* array, size_t count )
{
    size_t result = 0;        
    for( size_t i = 0; i < count; i++ ) {
       if( array[i] == 0 ) {
           ++result;
       }
    }
    return result;
}

and I need to call it for a const array:

static const int Array[] = { 10, 20, 0, 2};

countZeroes( const_cast<int*>( Array ), sizeof( Array ) / sizeof( Array[0] ) );

will this be undefined behaviour? If so - when will the program run into UB - when doing the const_cast and calling the functon or when accessing the array?

Kevin Panko
  • 8,356
  • 19
  • 50
  • 61
sharptooth
  • 167,383
  • 100
  • 513
  • 979
  • I suppose you cannot change the function declaration... but I strongly urge you to write a wrapper with a better one and do the dirty work inside it. – Matthieu M. Oct 09 '09 at 08:25
  • Maybe try a different thing altogether: `size_t numZeros = std::count(Array, Array + sizeof(Array)/sizeof(Array[0]), 0);` – MP24 Oct 09 '09 at 08:25
  • @MP24 This function is just a simple illustration of a problem. – sharptooth Oct 09 '09 at 09:00
  • Does this answer your question? [Where is the undefined behavior when using const\_cast<>?](https://stackoverflow.com/questions/5535863/where-is-the-undefined-behavior-when-using-const-cast) – user202729 Jan 19 '22 at 16:07

5 Answers5

14

Yes, it is allowed (if dangerous!). It's the actual write to a const object that incurs undefined behaviour, not the cast itself (7.1.5.1/4 [dcl.type.cv]).

As the standard notes in 5.2.11/7 [expr.const.cast], depending on the type of the object an attempt to write through a pointer that is the result of casting away const may produce undefined behaviour.

CB Bailey
  • 755,051
  • 104
  • 632
  • 656
1

Since your code does not modify the array, and you told the compiler you know what you are doing by using the const_cast, you will actually be OK. However, I believe you are technically invoking undefined behaviour. Best to get the function declaration fixed, or write, declare and use the const-safe version of it.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • try reading up on how to use const in "Effective C++" or "More Effective C++". It shows some valid uses of the const_cast as well (e.g. to avoid some code duplication). – Tobias Langner Oct 09 '09 at 07:30
  • -1 because this is not undefined behavior; historically, it would have made C++98 code difficult to interoperate with pre-standard C. However, I'm fully with you on the advice. – Jirka Hanika Apr 22 '13 at 10:47
1

Yes, you can do that. No, it is not undefined behavior as long as the function truely does not try to write to the array.

Zanson
  • 3,991
  • 25
  • 31
1

The problem of const_cast is always the same -- it allows you to "break the rules", just like casting to and from void* -- sure you can do that, but the question is why should you?

In this case it's of course ok, but you should ask yourself why didn't you declare size_t countZeroes( const int* array, size_t count ) in the first place?

And as a general rule about const_cast:

  1. It may produce hard to find bugs
  2. You're throwing away the const-agreement with the compiler
  3. Basically you're turning the language into a lower-level one.
Kornel Kisielewicz
  • 55,802
  • 15
  • 111
  • 149
-2

Using const_cast on an object which is initially defined as const is UB, therefore the undefined behaviour comes about immediately at the point you call const_cast.

1800 INFORMATION
  • 131,367
  • 29
  • 160
  • 239