这里有includes和我的函数:
我正试图用memcpy
复制缓冲区中的stbuf->st_mode
,当读回它时,值并不是我试图复制的值。
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <string.h>
void r1_getattr(char* pth, int cfd){
struct stat* stbuf = malloc(sizeof(stat));
int res = stat(pth, stbuf);
printf("(%3o)n", (stbuf->st_mode)&0777);
char* wbuf = malloc(256);
memcpy(wbuf, &(stbuf->st_mode), sizeof(mode_t));
printf("(%3o)n", (*(mode_t*)((char*)wbuf))&0777);
}
输出:"(775("one_answers"(21("不应该相同吗?
简单的键入错误:
更换
struct stat *stbuf = malloc(sizeof(stat));
带有
struct stat *stbuf = malloc(sizeof(struct stat));
当我们使用未初始化的内存时,看到奇怪的结果总是很有趣:-(