从32到64位的问题移植C代码:fread导致访问冲突

问题描述 投票:0回答:1

我正在Visual Studio中将我的C代码从32位移植到64位,并且fread调用遇到问题,该调用引发了异常Access violation writing location 0xFFFFFFFF873B2830。它正在写入的地址与malloc返回的指针地址匹配。

我看不出是什么原因造成的。有什么建议么?

velo_error_t velo_read_binary_file(char input_file[], uint8_t **p_buffer, uint32_t *p_length) {
    // Returns error code, zero if normal:
    velo_error_t err_code = VELO_ERROR_NONE;
    // read input file.
    FILE *f = fopen(input_file, "rb");
    if (!f) {
        printf("File not valid.  File trying to open for reading:\n%s\n", input_file);
        err_code = VELO_ERROR_INVALID_FILE;
    } else {
        fseek(f, 0, SEEK_END);
        *p_length = ftell(f);
        fseek(f, 0, SEEK_SET);
        *p_buffer = malloc(*p_length);
        if (*p_buffer) {
            fread(*p_buffer, 1, *p_length, f);
        }
        fclose(f);
    }
    return err_code;
}
c malloc 64-bit fread
1个回答
0
投票

问题是缺少包含(stdlib.h)。 Visual Studio在32位模式下似乎可以静默处理缺少的内容,并以某种方式找到合适的库来使用。在64位模式下,此操作无效,但不会给出有关丢失引用的错误。它只是返回一个(看似)随机指针地址。

正如@Jabberwocky所说,在Visual Studio中注意警告很重要!

© www.soinside.com 2019 - 2024. All rights reserved.