我正在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;
}
问题是缺少包含(stdlib.h)。 Visual Studio在32位模式下似乎可以静默处理缺少的内容,并以某种方式找到合适的库来使用。在64位模式下,此操作无效,但不会给出有关丢失引用的错误。它只是返回一个(看似)随机指针地址。
正如@Jabberwocky所说,在Visual Studio中注意警告很重要!