Home > Software engineering >  call of overloaded ‘malloc(int)’ is ambiguous
call of overloaded ‘malloc(int)’ is ambiguous

Time:01-05

The following function belongs to the CoinUtils project, that is used in the open-source CBC linear programming solver.

/** Returns strdup or NULL if original NULL */
inline char *CoinStrdup(const char *name)
{
  char *dup = NULL;
  if (name) {
    const int len = static_cast< int >(strlen(name));
    dup = static_cast< char * >(malloc(len   1));
    CoinMemcpyN(name, len, dup);
    dup[len] = 0;
  }
  return dup;
}

I'm getting the following error when I compile in debug mode in GCC 11 under ubuntu 22.04.

error: call of overloaded ‘malloc(int)’ is ambiguous
  624 |     dup = static_cast< char * >(malloc(len   1));

Can someone explain what's going on?

The complete error message is this:

In file included from /src/third_party/coin-or/Clp/src/ClpModel.hpp:21,
                 from /src/third_party/coin-or/Clp/src/ClpSimplex.hpp:16,
                 from /src/engine/core/../core/lp_interface/lp_osi_interface.h:12,
                 from /src/test/lp_tests.h:13,
                 from /src/test/lp_tests.cpp:5:
/src/third_party/coin-or/CoinUtils/src/CoinHelperFunctions.hpp: In function ‘char* CoinStrdup(const char*)’:
/src/third_party/coin-or/CoinUtils/src/CoinHelperFunctions.hpp:624:39: error: call of overloaded ‘malloc(int)’ is ambiguous
  624 |     dup = static_cast< char * >(malloc(len   1));
      |                                 ~~~~~~^~~~~~~~~
In file included from /usr/include/c  /11/cstdlib:75,
                 from /usr/include/c  /11/ext/string_conversions.h:41,
                 from /usr/include/c  /11/bits/basic_string.h:6608,
                 from /usr/include/c  /11/string:55,
                 from /usr/include/c  /11/bits/locale_classes.h:40,
                 from /usr/include/c  /11/bits/ios_base.h:41,
                 from /usr/include/c  /11/ios:42,
                 from /usr/include/c  /11/ostream:38,
                 from /usr/include/c  /11/iostream:39,
                 from /src/test/testing_utilities.h:8,
                 from /src/test/lp_tests.h:8,
                 from /src/test/lp_tests.cpp:5:
/usr/include/stdlib.h:540:14: note: candidate: ‘void* malloc(size_t)’
  540 | extern void *malloc (size_t __size) __THROW __attribute_malloc__
      |              ^~~~~~
In file included from /src/third_party/header_only/armadillo-11.1.0/include/armadillo:134,
                 from /src/test/testing_utilities.h:9,
                 from /src/test/lp_tests.h:8,
                 from /src/test/lp_tests.cpp:5:
/src/third_party/header_only/armadillo-11.1.0/include/armadillo_bits/translate_superlu.hpp:332:3: note: candidate: ‘void* arma::superlu::malloc(std::size_t)’
  332 |   malloc(size_t N)
      |   ^~~~~~
In file included from /src/third_party/coin-or/CoinUtils/src/CoinModel.hpp:8,
                 from /src/engine/core/../core/lp_interface/lp_osi_interface.h:16,
                 from /src/test/lp_tests.h:13,
                 from /src/test/lp_tests.cpp:5:
/src/third_party/coin-or/CoinUtils/src/CoinModelUseful.hpp: In destructor ‘CoinYacc::~CoinYacc()’:
/src/third_party/coin-or/CoinUtils/src/CoinModelUseful.hpp:200:11: error: call of overloaded ‘free(char*&)’ is ambiguous
  200 |       free(symbuf);
      |       ~~~~^~~~~~~~
In file included from /usr/include/c  /11/cstdlib:75,
                 from /usr/include/c  /11/ext/string_conversions.h:41,
                 from /usr/include/c  /11/bits/basic_string.h:6608,
                 from /usr/include/c  /11/string:55,
                 from /usr/include/c  /11/bits/locale_classes.h:40,
                 from /usr/include/c  /11/bits/ios_base.h:41,
                 from /usr/include/c  /11/ios:42,
                 from /usr/include/c  /11/ostream:38,
                 from /usr/include/c  /11/iostream:39,
                 from /src/test/testing_utilities.h:8,
                 from /src/test/lp_tests.h:8,
                 from /src/test/lp_tests.cpp:5:
/usr/include/stdlib.h:555:13: note: candidate: ‘void free(void*)’
  555 | extern void free (void *__ptr) __THROW;
      |             ^~~~
In file included from /src/third_party/header_only/armadillo-11.1.0/include/armadillo:134,
                 from /src/test/testing_utilities.h:9,
                 from /src/test/lp_tests.h:8,
                 from /src/test/lp_tests.cpp:5:
/src/third_party/header_only/armadillo-11.1.0/include/armadillo_bits/translate_superlu.hpp:341:3: note: candidate: ‘void arma::superlu::free(void*)’
  341 |   free(void* mem)
      |   ^~~~
In file included from /src/third_party/coin-or/CoinUtils/src/CoinModel.hpp:8,
                 from /src/engine/core/../core/lp_interface/lp_osi_interface.h:16,
                 from /src/test/lp_tests.h:13,
                 from /src/test/lp_tests.cpp:5:
/src/third_party/coin-or/CoinUtils/src/CoinModelUseful.hpp:205:11: error: call of overloaded ‘free(char*&)’ is ambiguous
  205 |       free(s->name);
      |       ~~~~^~~~~~~~~
In file included from /usr/include/c  /11/cstdlib:75,
                 from /usr/include/c  /11/ext/string_conversions.h:41,
                 from /usr/include/c  /11/bits/basic_string.h:6608,
                 from /usr/include/c  /11/string:55,
                 from /usr/include/c  /11/bits/locale_classes.h:40,
                 from /usr/include/c  /11/bits/ios_base.h:41,
                 from /usr/include/c  /11/ios:42,
                 from /usr/include/c  /11/ostream:38,
                 from /usr/include/c  /11/iostream:39,
                 from /src/test/testing_utilities.h:8,
                 from /src/test/lp_tests.h:8,
                 from /src/test/lp_tests.cpp:5:
/usr/include/stdlib.h:555:13: note: candidate: ‘void free(void*)’
  555 | extern void free (void *__ptr) __THROW;
      |             ^~~~
In file included from /src/third_party/header_only/armadillo-11.1.0/include/armadillo:134,
                 from /src/test/testing_utilities.h:9,
                 from /src/test/lp_tests.h:8,
                 from /src/test/lp_tests.cpp:5:
/src/third_party/header_only/armadillo-11.1.0/include/armadillo_bits/translate_superlu.hpp:341:3: note: candidate: ‘void arma::superlu::free(void*)’
  341 |   free(void* mem)
      |   ^~~~
In file included from /src/third_party/coin-or/CoinUtils/src/CoinModel.hpp:8,
                 from /src/engine/core/../core/lp_interface/lp_osi_interface.h:16,
                 from /src/test/lp_tests.h:13,
                 from /src/test/lp_tests.cpp:5:
/src/third_party/coin-or/CoinUtils/src/CoinModelUseful.hpp:208:11: error: call of overloaded ‘free(symrec*&)’ is ambiguous
  208 |       free(symtable);
      |       ~~~~^~~~~~~~~~
In file included from /usr/include/c  /11/cstdlib:75,
                 from /usr/include/c  /11/ext/string_conversions.h:41,
                 from /usr/include/c  /11/bits/basic_string.h:6608,
                 from /usr/include/c  /11/string:55,
                 from /usr/include/c  /11/bits/locale_classes.h:40,
                 from /usr/include/c  /11/bits/ios_base.h:41,
                 from /usr/include/c  /11/ios:42,
                 from /usr/include/c  /11/ostream:38,
                 from /usr/include/c  /11/iostream:39,
                 from /src/test/testing_utilities.h:8,
                 from /src/test/lp_tests.h:8,
                 from /src/test/lp_tests.cpp:5:
/usr/include/stdlib.h:555:13: note: candidate: ‘void free(void*)’
  555 | extern void free (void *__ptr) __THROW;
      |             ^~~~
In file included from /src/third_party/header_only/armadillo-11.1.0/include/armadillo:134,
                 from /src/test/testing_utilities.h:9,
                 from /src/test/lp_tests.h:8,
                 from /src/test/lp_tests.cpp:5:
/src/third_party/header_only/armadillo-11.1.0/include/armadillo_bits/translate_superlu.hpp:341:3: note: candidate: ‘void arma::superlu::free(void*)’
  341 |   free(void* mem)
      |   ^~~~
gmake[3]: *** [CMakeFiles/Tests.dir/build.make:8532: CMakeFiles/Tests.dir/src/test/lp_tests.cpp.o] Error 1
gmake[3]: *** Se espera a que terminen otras tareas....
In file included from /src/third_party/coin-or/Clp/src/ClpModel.hpp:21,
                 from /src/third_party/coin-or/Clp/src/ClpSimplex.hpp:16,
                 from /src/engine/core/../core/lp_interface/lp_osi_interface.h:12,
                 from /src/test/lp_tests.h:13,
                 from /src/test/tests.cpp:20:
/src/third_party/coin-or/CoinUtils/src/CoinHelperFunctions.hpp: In function ‘char* CoinStrdup(const char*)’:
/src/third_party/coin-or/CoinUtils/src/CoinHelperFunctions.hpp:624:39: error: call of overloaded ‘malloc(int)’ is ambiguous
  624 |     dup = static_cast< char * >(malloc(len   1));
      |                                 ~~~~~~^~~~~~~~~
In file included from /usr/include/c  /11/cstdlib:75,
                 from /usr/include/c  /11/ext/string_conversions.h:41,
                 from /usr/include/c  /11/bits/basic_string.h:6608,
                 from /usr/include/c  /11/string:55,
                 from /usr/include/c  /11/bits/locale_classes.h:40,
                 from /usr/include/c  /11/bits/ios_base.h:41,
                 from /usr/include/c  /11/ios:42,
                 from /usr/include/c  /11/ostream:38,
                 from /usr/include/c  /11/iostream:39,
                 from /src/test/tests.cpp:2:
/usr/include/stdlib.h:540:14: note: candidate: ‘void* malloc(size_t)’
  540 | extern void *malloc (size_t __size) __THROW __attribute_malloc__
      |              ^~~~~~
In file included from /src/third_party/header_only/armadillo-11.1.0/include/armadillo:134,
                 from /src/third_party/header_only/mlpack-4.0.0/src/mlpack/core/arma_extend/arma_extend.hpp:21,
                 from /src/third_party/header_only/mlpack-4.0.0/src/mlpack/base.hpp:87,
                 from /src/third_party/header_only/mlpack-4.0.0/src/mlpack.hpp:25,
                 from /src/engine/../utils/type_declarations.h:9,
                 from /src/engine/../utils/armadillo_extension.h:19,
                 from /src/test/../engine/core/data_structures/bus_data.h:10,
                 from /src/test/../engine/core/data_structures/numeric_circuit.h:22,
                 from /src/test/../newton.h:9,
                 from /src/test/tests.cpp:9:
/src/third_party/header_only/armadillo-11.1.0/include/armadillo_bits/translate_superlu.hpp:332:3: note: candidate: ‘void* arma::superlu::malloc(std::size_t)’
  332 |   malloc(size_t N)
      |   ^~~~~~
In file included from /src/third_party/coin-or/CoinUtils/src/CoinModel.hpp:8,
                 from /src/engine/core/../core/lp_interface/lp_osi_interface.h:16,
                 from /src/test/lp_tests.h:13,
                 from /src/test/tests.cpp:20:
/src/third_party/coin-or/CoinUtils/src/CoinModelUseful.hpp: In destructor ‘CoinYacc::~CoinYacc()’:
/src/third_party/coin-or/CoinUtils/src/CoinModelUseful.hpp:200:11: error: call of overloaded ‘free(char*&)’ is ambiguous
  200 |       free(symbuf);
      |       ~~~~^~~~~~~~
In file included from /usr/include/c  /11/cstdlib:75,
                 from /usr/include/c  /11/ext/string_conversions.h:41,
                 from /usr/include/c  /11/bits/basic_string.h:6608,
                 from /usr/include/c  /11/string:55,
                 from /usr/include/c  /11/bits/locale_classes.h:40,
                 from /usr/include/c  /11/bits/ios_base.h:41,
                 from /usr/include/c  /11/ios:42,
                 from /usr/include/c  /11/ostream:38,
                 from /usr/include/c  /11/iostream:39,
                 from /src/test/tests.cpp:2:
/usr/include/stdlib.h:555:13: note: candidate: ‘void free(void*)’
  555 | extern void free (void *__ptr) __THROW;
      |             ^~~~
In file included from /src/third_party/header_only/armadillo-11.1.0/include/armadillo:134,
                 from /src/third_party/header_only/mlpack-4.0.0/src/mlpack/core/arma_extend/arma_extend.hpp:21,
                 from /src/third_party/header_only/mlpack-4.0.0/src/mlpack/base.hpp:87,
                 from /src/third_party/header_only/mlpack-4.0.0/src/mlpack.hpp:25,
                 from /src/engine/../utils/type_declarations.h:9,
                 from /src/engine/../utils/armadillo_extension.h:19,
                 from /src/test/../engine/core/data_structures/bus_data.h:10,
                 from /src/test/../engine/core/data_structures/numeric_circuit.h:22,
                 from /src/test/../newton.h:9,
                 from /src/test/tests.cpp:9:
/src/third_party/header_only/armadillo-11.1.0/include/armadillo_bits/translate_superlu.hpp:341:3: note: candidate: ‘void arma::superlu::free(void*)’
  341 |   free(void* mem)
      |   ^~~~
In file included from /src/third_party/coin-or/CoinUtils/src/CoinModel.hpp:8,
                 from /src/engine/core/../core/lp_interface/lp_osi_interface.h:16,
                 from /src/test/lp_tests.h:13,
                 from /src/test/tests.cpp:20:
/src/third_party/coin-or/CoinUtils/src/CoinModelUseful.hpp:205:11: error: call of overloaded ‘free(char*&)’ is ambiguous
  205 |       free(s->name);
      |       ~~~~^~~~~~~~~
In file included from /usr/include/c  /11/cstdlib:75,
                 from /usr/include/c  /11/ext/string_conversions.h:41,
                 from /usr/include/c  /11/bits/basic_string.h:6608,
                 from /usr/include/c  /11/string:55,
                 from /usr/include/c  /11/bits/locale_classes.h:40,
                 from /usr/include/c  /11/bits/ios_base.h:41,
                 from /usr/include/c  /11/ios:42,
                 from /usr/include/c  /11/ostream:38,
                 from /usr/include/c  /11/iostream:39,
                 from /src/test/tests.cpp:2:
/usr/include/stdlib.h:555:13: note: candidate: ‘void free(void*)’
  555 | extern void free (void *__ptr) __THROW;
      |             ^~~~
In file included from /src/third_party/header_only/armadillo-11.1.0/include/armadillo:134,
                 from /src/third_party/header_only/mlpack-4.0.0/src/mlpack/core/arma_extend/arma_extend.hpp:21,
                 from /src/third_party/header_only/mlpack-4.0.0/src/mlpack/base.hpp:87,
                 from /src/third_party/header_only/mlpack-4.0.0/src/mlpack.hpp:25,
                 from /src/engine/../utils/type_declarations.h:9,
                 from /src/engine/../utils/armadillo_extension.h:19,
                 from /src/test/../engine/core/data_structures/bus_data.h:10,
                 from /src/test/../engine/core/data_structures/numeric_circuit.h:22,
                 from /src/test/../newton.h:9,
                 from /src/test/tests.cpp:9:
/src/third_party/header_only/armadillo-11.1.0/include/armadillo_bits/translate_superlu.hpp:341:3: note: candidate: ‘void arma::superlu::free(void*)’
  341 |   free(void* mem)
      |   ^~~~
In file included from /src/third_party/coin-or/CoinUtils/src/CoinModel.hpp:8,
                 from /src/engine/core/../core/lp_interface/lp_osi_interface.h:16,
                 from /src/test/lp_tests.h:13,
                 from /src/test/tests.cpp:20:
/src/third_party/coin-or/CoinUtils/src/CoinModelUseful.hpp:208:11: error: call of overloaded ‘free(symrec*&)’ is ambiguous
  208 |       free(symtable);
      |       ~~~~^~~~~~~~~~
In file included from /usr/include/c  /11/cstdlib:75,
                 from /usr/include/c  /11/ext/string_conversions.h:41,
                 from /usr/include/c  /11/bits/basic_string.h:6608,
                 from /usr/include/c  /11/string:55,
                 from /usr/include/c  /11/bits/locale_classes.h:40,
                 from /usr/include/c  /11/bits/ios_base.h:41,
                 from /usr/include/c  /11/ios:42,
                 from /usr/include/c  /11/ostream:38,
                 from /usr/include/c  /11/iostream:39,
                 from /src/test/tests.cpp:2:
/usr/include/stdlib.h:555:13: note: candidate: ‘void free(void*)’
  555 | extern void free (void *__ptr) __THROW;
      |             ^~~~
In file included from /src/third_party/header_only/armadillo-11.1.0/include/armadillo:134,
                 from /src/third_party/header_only/mlpack-4.0.0/src/mlpack/core/arma_extend/arma_extend.hpp:21,
                 from /src/third_party/header_only/mlpack-4.0.0/src/mlpack/base.hpp:87,
                 from /src/third_party/header_only/mlpack-4.0.0/src/mlpack.hpp:25,
                 from /src/engine/../utils/type_declarations.h:9,
                 from /src/engine/../utils/armadillo_extension.h:19,
                 from /src/test/../engine/core/data_structures/bus_data.h:10,
                 from /src/test/../engine/core/data_structures/numeric_circuit.h:22,
                 from /src/test/../newton.h:9,
                 from /src/test/tests.cpp:9:
/src/third_party/header_only/armadillo-11.1.0/include/armadillo_bits/translate_superlu.hpp:341:3: note: candidate: ‘void arma::superlu::free(void*)’
  341 |   free(void* mem)
      |   ^~~~
gmake[3]: *** [CMakeFiles/Tests.dir/build.make:8462: CMakeFiles/Tests.dir/src/test/tests.cpp.o] Error 1
gmake[2]: *** [CMakeFiles/Makefile2:324: CMakeFiles/Tests.dir/all] Error 2
gmake[1]: *** [CMakeFiles/Makefile2:331: CMakeFiles/Tests.dir/rule] Error 2
gmake: *** [Makefile:293: Tests] Error 2

CodePudding user response:

The two functions it's considering are the standard void* malloc(size_t) and void* arma::superlu::malloc(std::size_t). Presumably somewhere you have using namespace arma::superlu which pulls everything from that namespace into the global namespace.

To get around it, you can

  1. get rid of using namespace arma::superlu and either fully qualify everything from that namespace or pull in exactly you need (with using arma::superlu::xyz;). Or
  2. Qualify the malloc call with ::malloc, or preferably std::malloc to call the C standard version.

Similar for the ambiguous calls to free.

CodePudding user response:

The error messages explain: the call to malloc is ambiguous due to two candidates:

std::malloc
arma::superlu::malloc

To avoid this you should explicitly call the intended one, e.g. std::malloc, rather than relying on overload resolution. You might also want to look at why arma::superlu:: is being searched. Is there a using directive; or is this code perhaps defined inside that namespace and so that was the intended version of malloc to call.

NB. The code has a vulnerability in the behaviour when the input string is longer than INT_MAX, and it would also be good to check what is going to happen if malloc returns a null pointer.

  •  Tags:  
  • c
  • Related