简体   繁体   English

重载“malloc(int)”的调用不明确

[英]call of overloaded ‘malloc(int)’ is ambiguous

The following function belongs to the CoinUtils project, that is used in the open-source CBC linear programming solver.以下function属于CoinUtils项目,用于开源CBC线性规划求解器。

/** 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.当我在 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

The two functions it's considering are the standard void* malloc(size_t) and void* arma::superlu::malloc(std::size_t) .它正在考虑的两个函数是标准的void* malloc(size_t)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.大概在某个地方您using namespace arma::superlu ,它将所有内容从该命名空间拉入全局命名空间。

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; ).摆脱using namespace arma::superlu并完全限定该命名空间中的所有内容或完全符合您的需要( using arma::superlu::xyz; )。 Or要么
  2. Qualify the malloc call with ::malloc , or preferably std::malloc to call the C++ standard version.使用::malloc限定malloc调用,或者最好使用std::malloc来调用 C++ 标准版本。

Similar for the ambiguous calls to free .类似于对free的模糊调用。

The error messages explain: the call to malloc is ambiguous due to two candidates:错误消息解释:由于两个候选人,对malloc的调用不明确:

std::malloc
arma::superlu::malloc

To avoid this you should explicitly call the intended one, eg std::malloc , rather than relying on overload resolution.为避免这种情况,您应该显式调用预期的调用,例如std::malloc ,而不是依赖重载解析。 You might also want to look at why arma::superlu:: is being searched.您可能还想看看为什么要搜索arma::superlu:: 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.或者此代码是否可能在该命名空间内定义,因此这是要调用的 malloc 的预期版本。

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.当输入字符串长于INT_MAX时,代码在行为上存在漏洞,如果malloc返回 null 指针,检查将发生什么也是很好的。

std::malloc is not a candidate here: std::malloc不是这里的候选人:

/usr/include/stdlib.h:540:14: note: candidate: ‘void* malloc(size_t)’
  540 | extern void *malloc (size_t __size) __THROW __attribute_malloc__
      |              ^~~~~~
...
/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)
      |   ^~~~~~

But the culprit likely is a using namespace arma::superlu somewhere in your own code, or in one of the thirdparty headers.但罪魁祸首可能是在您自己的代码或第三方标头之一中的某个地方using namespace arma::superlu

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM