我试图模拟需要将数据复制到外设,裸机,无OS的系统。将地址存储为值并稍后将其用于指针
该约定规定复制函数是一个C函数,它将外设的地址作为写入某个寄存器的8位地址。外设在内部使用。不过,我在模拟C中的事情,并测试所有功能我在做类似如下的MWE:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc,char *argv[]){
//stack-array to copy
char array[4] = {3,32,-12,99};
//naive emulation of the peripheral memory space
char peripheral_array[4];
//herein lies the address send as a value
char address_reg = (char*)(peripheral_array);
//assume this is the peripheral side
//set ptr to the address of the peripheral_array
char *ptr = (char*) address_reg;
memcpy((void*)ptr,array,sizeof(char)*4);
return EXIT_SUCCESS;
}
我得到segmentation fault
。
这里有什么问题?
如何将数组的指针作为值存储并发送,将其作为地址重新写入数组并执行
memcpy
?
更换
我想,如果它是代表外围地址空间'char'更改为'无符号char'。 –
另外:你不需要用'(void *)ptr'强制转换为'void *','memcpy'的'void *'类型的意思是它接受任何指针类型。 –
代码不会编译。 address_reg与地址相同吗? – Ctx