Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

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.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote