C - 写访问冲突

标签 c malloc unhandled-exception

我在 nullString 的最后一行有一个错误,该函数使用简单的 for() 将所有字符串设置为 '\0'

void function ( unsigned char inputArray[], size_t inputSize )
{
    size_t cellSize;
    if (inputSize <= 256)
        cellSize = 1;
    else
        cellSize = ceil(inputSize / 2 / 256) + 1;


    // Sub Box
    unsigned char subBox[255];
    for (size_t line = 0; line < 255; line++)
        subBox[line] = 0;

    generate_SubBox(subBox, key);
    // Sub Box


    // Sub Box reverse
    unsigned char subBox_Inverse[255];
    for (size_t line = 0; line < 255; line++)
        subBox_Inverse[line] = 0;

    generate_SubBox_Inverse(subBox_Inverse, subBox, key);
    // Sub Box reverse        

    unsigned char* inputArray2 = NULL;
    inputArray2 = malloc(sizeof(unsigned char)* inputSize / 2);
    verifyMalloc(inputArray2);
    nullString(inputArray2, inputSize / 2);

    unsigned char string_temp[3] = { 0 };
    size_t w = 0;
    for (size_t i = 0; i < inputSize / 2; i++)
    {
        string_temp[0] = inputArray[w];
        string_temp[1] = inputArray[w + 1];

        inputArray2[i] = strtoll(string_temp, NULL, 16);

        w += 2;
    }
}

我尝试通过注释来中和 nullString() 之前的所有指令,但它不会改变任何内容。

如果我中和 nullString,则会出现错误,位于

inputArray2[i] = strtoll(...)

希望你能得到答案:)

提前致谢!

编辑: 这是空字符串:

void nullString(unsigned char input[], size_t length)
{
    for (size_t x = 0; x < length; x++)
        input[x] = '\0';
}

我注释了nullString之前的所有指令,错误仍然存​​在。

我还验证了变量,它们看起来都不错

编辑2: 验证Malloc:

void verifyMalloc(int* pointer)
{
    if (pointer == NULL)
    {
        perror("Erreur");

        Sleep(15000);
        exit(0);
    }
}

最佳答案

我们所看到的一切都严重暗示您忘记 #include <stdlib.h> (并忽略由此产生的警告)。

当您使用 malloc() 时可能会发生这种情况不包括stdlib.h在同一文件中:

  • 编译器考虑 malloc()要隐式声明的函数,这意味着它假设其返回类型为 int (而不是 *void )。
  • 可能适用于 sizeof (int)sizeof (*void) 相同。但是当 int是 32 位,而指针是 64 位,则 malloc() 返回的地址可能会丢失一半的位并指向无效地址。

关于C - 写访问冲突,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44504429/

相关文章:

c++ - 如何识别这个结构体中mYear元素的含义?

c - 整数 * float = ?在C中

c - 使用 pthreads 和 malloc

c# - 具有原始异常调用堆栈和内存的 WPF 故障转储

c# - 如何在 MSTest 中处理 currentDomain.UnhandledException

C 成语和鲜为人知的事实

在 C 中更改 char[]

c - 作为参数接收的指针中的 malloc

c - 分配了一些内存后,我无法计算出我分配的内存的正确大小。我不知道为什么

c# - 未处理的异常 C#