memset 将 -1 放入我的数组中,而不是我指定的值,这是怎么回事

memset is placing -1 into my array instead of the value I specified, what is going on?

本文关键字:怎么回事 我的 数组 memset      更新时间:2023-10-16
    int * best = new int[numNodes];
memset(best,numeric_limits<int>::max()/2,numNodes*sizeof(int));
    int test = numeric_limits<int>::max()/2;

打印出数组给我一个全部 -1 的数组。打印出int"test"给了我正确的"1073741823"值

我包括"cstring"用于使用内存集。这是我关注的文档:http://www.cplusplus.com/reference/clibrary/cstring/memset/

为什么我会得到这个结果?我敢肯定这可能是显而易见的事情,我只是没有看到它。

memset 用一个无符号字符填充内存:

void *memset(void *DST, int C, size_t LENGTH);
   This  function converts the argument C into an unsigned char and fills the
   first LENGTH characters of the array pointed to by DST to the value.