我写了这段代码:
#include <stdlib.h>
int main(){
double x;
foo(&x);
// ...
}
foo(double *p){
char bytes[8];
bytes[0]=62;
bytes[1]=-120;
bytes[2]=41;
bytes[3]=23;
bytes[4]=-16;
bytes[5]=-57;
bytes[6]=47;
bytes[7]=-40;
// bytes = 00111110 10001000 00101001 00010111 11110000 11000111 00101111 11011000
memcpy(p,bytes,sizeof(double));
// (1)
}
memcpy
之后我希望在 x 中找到这些位,但是在调试器中的代码行 (1) 中,我有
*p = Binary: 1000000000000000000000000000000000000000000000000000000000000000
Hex: 0x8000000000000000
但是,通过使用 fprintf(stderr,"%x",*p);
打印该值我得到值 1729883e
。
最佳答案
我制作了一些 mods 来让它运行,它实际上填充了 x:
#include <stdio.h>
#include <string.h>
bar(){
double x;
foo(&x);
printf("%f\n", x);
}
foo(double *p){
char bytes[8];
bytes[0]=-62;
bytes[1]=-120;
bytes[2]=41;
bytes[3]=23;
bytes[4]=-16;
bytes[5]=-57;
bytes[6]=47;
bytes[7]=-40;
memcpy(p,bytes,sizeof(double));
}
int main(int argc, char* argv[]){
bar();
}
输出为:-626117722098128509564427792535309566746344523980579928553066490382037451337019090855228509979567689660741872330473 472.000000
如果这对您不起作用,问题可能出在代码的其余部分或调试器设置中。
关于c - memcpy 后调试器无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21316461/