在我的代码中,我被要求返回一个指向heap_ptr旧值的指针,并将heap_ptr移动bytes_alloc的数量。我已经声明heap_ptr为void *heap_ptr
和heap_ptr = mmap(NULL, 2*4096, ...)
来为heap_ptr指定8192个字节。我的问题是,由于bytes_alloc是int类型的,我不确定是否正确地将heap_ptr移动了2000个字节。我是否应该使用像heap_ptr = (void *)((char *)heap_ptr + (char)bytes_alloc)
这样的bytes_alloc?如何将指针变量移动指定的字节数?
这里是我的功能:
void heap_alloc(int bytes_alloc) //bytes_alloc is 2000
{
void *temp_heap_ptr; //teporary pointer to my mmap heap_ptr
heap_ptr = (void *)((char *)heap_ptr + bytes_alloc) //this is where my question is
return (temp_heap_ptr);
}
将返回未初始化的变量'temp_heap_ptr',我假设你的意思是写'该行temp_heap_ptr ='。另外,一个'void'函数可能不会返回一个值。请尝试发布真实的代码,否则答复者必须对真实的代码进行很多猜测。 –
“//指向我的mmap heap_ptr的teporary指针” - nope,它是一个没有特别指向的临时指针,因为你从未将它设置为某种东西。 – immibis