visualstudio malloc ERROR_INVALID_ACCESS

时间:2017-03-07 05:03:14

标签: c visual-studio-2013 malloc

#include "stdafx.h"
#include <errno.h>
#include <stdlib.h>
#include <malloc.h>
int main()
{
    int i = 1000;
    int count = 1;
    int newlen = 1;
    int oldlen = 10;
    char *result = NULL;
    int total = 0;

    /* case 1 */
    total = i + 1 + count * (newlen - oldlen);
    printf("case 1 start\n");
    result = (char *)malloc(total);
    if (result == NULL)
    {
        return -1;
    }
    free(result);
    printf("case 1 success\n");


    /* case 2 */
    printf("case 2 start\n");
    result = (char *)malloc(i + 1 + count * (newlen - oldlen));
    free(result);
    if (result == NULL)
    {
        return -1;
    }
    printf("case 2 success\n");
    return 0;
}

为什么malloc()失败了?

可能(newlen - oldlen)为负数或count * (minus number)

0 个答案:

没有答案