C-写访问冲突 [英] C - Write access violation

查看:57
本文介绍了C-写访问冲突的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have an error at the last line, in nullString, a function setting all the string to '\0' with a simple for()

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()之前的所有指令,但这并没有改变任何内容.

I tried neutralizing line per line all instructions coming before nullString() by commenting them but it doesn't change anything.

如果我中和nullString,则会在以下错误出现

If I neutralize nullString, the error comes after, at

inputArray2 [i] = strtoll(...)

希望您得到了答案:)

提前谢谢!

这是nullString:

Here is nullString:

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

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

I commented all the instructions before nullString, the error is still there.

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

I also verified variables and they all look like good

verifyMalloc:

EDIT 2: verifyMalloc:

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

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

推荐答案

我们看到的所有内容都在认真地暗示您忘记了 #include< stdlib.h> (并忽略了由此产生的警告)从那开始).

Everything we're seeing is seriously hinting at you forgetting to #include <stdlib.h> (and ignoring the warnings resulting from that).

当您使用 malloc()而不在同一文件中包含 stdlib.h 时,可能会发生以下情况:

This is what might happens when you use malloc() without including stdlib.h in the same file:

  • 编译器认为 malloc()函数是隐式声明的,这意味着它假定其返回类型为 int (而不是 * void ).
  • sizeof(int) sizeof(* void)相同时,此可能起作用.但是,当 int 为32位而指针为64位时,由 malloc()返回的地址可能会丢失一半的位,并指向无效的地址.
  • the compiler consider the malloc() function to be declared implicitly, which means it is assuming that it's return types is int (instead of *void).
  • This might work when sizeof (int) is the same as sizeof (*void). But when int is 32-bits while pointers are 64-bits then the address returned by malloc() might lose half of it's bits and point to an invalid address.

这篇关于C-写访问冲突的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆