在我的代碼中,我被要求返回一個指向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