How do I make these 2 macros into a function? /* Given block ptr bp, compute add
ID: 3712717 • Letter: H
Question
How do I make these 2 macros into a function?
/* Given block ptr bp, compute address of next and previous free blocks */
#define NEXT_FREEP(bp)(*(void **)(bp + 16))
#define PREV_FREEP(bp)(*(void **)(bp))
here bp is a block pointer (void* bp)
I want to have this macro as a function so I can call it like this
static char *free_listp = 0;/* Pointer to the first free block */
static void insertAtFront(void *bp){
NEXT_FREEP(bp) = free_listp; //Sets next ptr to start of free list
PREV_FREEP(free_listp) = bp; //Sets current's prev to new block
PREV_FREEP(bp) = NULL; // Sets prev pointer to NULL
free_listp = bp; // Sets start of free list as new block
}
Explanation / Answer
The function equivalent of these macros are given below.
void* NEXT_FREEP(void *bp) {
return *(void **)(bp + 16);
}
void* PREV_FREEP(void *bp) {
return *(void **)(bp);
}
Please note that with functions, you cannot use NEXT_FREEP(bp) = free_listp; as function cannot be at the left hand side of an operation.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.