MAB*_*ABC 0 c linux gcc uclibc
我打电话给以下时面对"内存故障":
extern void *memcpy (void *__restrict __dest,
__const void *__restrict __src, size_t __n)
__THROW __nonnull ((1, 2));
Run Code Online (Sandbox Code Playgroud)
这是一段代码:
fprintf(stderr, "sysconfig line 440 \n");
fprintf(stderr, "Value size: %d ; Pointer mymsg: %p ; Pointer value: %p ; mymsg->mtext: %s ; value: %s ; size: %d ; \n", strlen(value), mymsg, value, mymsg->mtext, value, size);
memcpy(mymsg->mtext, value, size);
fprintf(stderr, "sysconfig line 442 \n");
Run Code Online (Sandbox Code Playgroud)
mymsg是一个指向struct的指针:
MSG_T *mymsg;
Run Code Online (Sandbox Code Playgroud)
MSG_T:
typedef struct msgInfo {
int cmd;
int arg1;
int arg2;
char mtext[MAX_SEND_SIZE];
} MSG_T;
Run Code Online (Sandbox Code Playgroud)
MAX_SEND_SIZE = 4096,value是一个(void*)char*字符串,包含""(\ 0或空链)和size = 4096,所以我不明白为什么我得到一个内存错误.我检查没有内存重叠.这是执行的输出:
sysconfig line 440
Value size: 0 ; Pointer mymsg: 0x7fd49ac4 ; Pointer value: 0x7fd4ab4c ; mymsg->mtext: ; value: ; size: 4096 ;
Memory fault
Run Code Online (Sandbox Code Playgroud)
我还没有发现这里的哪种条件会导致分段,因为所有内存都已经分配好,值是一个空字符串,并且没有内存重叠.
我有时只会得到内存故障(似乎是随机的),有时候进程会执行并退出而没有错误.
memcpy(mymsg-> mtext,value,size);
value是一个长度的字符串,0您正在尝试4096从此字符串中复制字节.因此,您正在访问4095未分配的字节,这意味着您正在调用未定义的行为.
| 归档时间: |
|
| 查看次数: |
419 次 |
| 最近记录: |