1

In a example below, I want to know the call of func with a wrong type of argument - vptr, which should be called with an int pointer.

void func(int * ptr){
}
int main(){
    void * vptr;
    func(vptr);
}

GCC doesn't warn this type of warnings even with wall option. Is there any other options in gcc, or other programming tricks of finding out those bad manners of codes. Aside that, how about in cpp?

EDIT: VTT answered this is valid in C, but invalid in C++ with no other cast keywords like static_cast (see the other post in detail).

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
jumeno
  • 37
  • 7
  • For g++ use: `-fpermissive` as in _"main.cpp 6 error: invalid conversion from 'void*' to 'int*' [-fpermissive]"_ – Richard Critten May 26 '17 at 10:28
  • Also try using `-Wextra -Wpointer-arith -Wcast-qual -Wcast-align`. – Andre Kampling May 26 '17 at 10:29
  • My SO g++ build options: `-Wnon-virtual-dtor -Wshadow -Winit-self -Wredundant-decls -Wcast-align -Wundef -Wfloat-equal -Wunreachable-code -Wmissing-declarations -Wmissing-include-dirs -Wswitch-enum -Wswitch-default -Weffc++ -Wzero-as-null-pointer-constant -Wmain -pedantic-errors -pedantic -Wextra -Wall` try them all, remove the ones that start to annoy you. – Richard Critten May 26 '17 at 10:30
  • @Ric Some (many?) of those are implied by either -Wall or -Wextra – Paul Stelian May 26 '17 at 10:34
  • @Andre Those options were in no effect in C. – jumeno May 26 '17 at 13:25

1 Answers1

6

In C, it's a valid implicit conversion. In C++, such conversion requires at least static_cast and causes compile-time error otherwise. You should figure out which language are you actually using.

user7860670
  • 35,849
  • 4
  • 58
  • 84
  • 2
    This is asking for clarification of the question; it should be a comment. – Roger Lipscombe May 26 '17 at 10:25
  • in C++ you would use static_cast – M.M May 26 '17 at 10:35
  • @M.M well, `static_cast` can be used indeed, however using it may lead to strict aliasing violation. – user7860670 May 26 '17 at 10:41
  • @VTT (a) the type of cast has nothing to do with strict aliasing , (b) in this instance reinterpret_cast is defined to have the same behaviour as static_cast, but it's good practice to use the "smallest hammer" – M.M May 26 '17 at 10:45