gpt4 book ai didi

C语言逐字节复制指针的内容无限循环

转载 作者:行者123 更新时间:2023-11-30 21:05:46 24 4
gpt4 key购买 nike

我试图将一个指针的内容逐字节复制到另一个指针,但我陷入了下面函数的 for 循环中。我相信这可能与C语言有关。知道为什么会发生这种情况吗?

void copy_COW(unsigned int pid, unsigned int vaddr) {
//pid is the current process id, vaddr is the double mapped page which has a fault
dprintf("copying on write...\n"); dprintf("\n");

//set fresh_page_index to perm writeable
unsigned int writeable_perm = PTE_P | PTE_W | PTE_U;
unsigned int* contents_to_copy;
unsigned int* fresh_page_index;
unsigned int i;

//allocate fresh page
fresh_page_index = (unsigned int*) alloc_page(pid, vaddr, writeable_perm);
contents_to_copy = (unsigned int*) get_ptbl_entry_by_va(pid, vaddr);

//fresh_page_index |= writeable_perm; //make page writeable, usable by user and present

//copy contents at vaddr (dir, page) to fresh_page_index by looping thru
for (i=0; i<4096 ;i++)
{
//dprintf("i is %d \n", i);
char byteToCopy = contents_to_copy[i];

fresh_page_index[i] = byteToCopy;
}

//update memory mapping in pdir to use fresh_page_index
set_ptbl_entry_by_va(pid, vaddr, (unsigned int) fresh_page_index, writeable_perm);

}

最佳答案

在复制循环中,您正在读取 4096 个字符,并写入 4096 个整数。在循环中,读取的字符将被符号扩展为整数并写入整数数组 fresh_page_index

您需要调整类型定义,或者更简单,使用 memcpy

关于C语言逐字节复制指针的内容无限循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52762484/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com