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

时间:2018-10-11 14:21:02

标签: c pointers memory

我试图将一个指针的内容一个字节一个字节地复制到另一个指针,但是我陷入了下面函数的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);

}

1 个答案:

答案 0 :(得分:0)

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

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