c - 错误: expected statement before ‘)’ token in C macro

标签 c macros

#define ALIGNMENT 8

#define CHUNK_SIZE (1<<12) //Extend heap by this amount (bytes)
#define WSIZE 4
#define DSIZE 8
#define MIN_BLOCK_SIZE (4*WSIZE) //Should it require at least 1 word of payload?

#define MAX(x,y) ((x) > (y) ? (x) : (y))

//Pack a size and allocated bit into a word
//#define PACK(size, alloc) ((size << 1 ) | (alloc))

//Read and write a word at address p
#define GET(p) (*(unsigned int*)(p))
#define PUT(p,val) (*(unsigned int *)(p) = (val))

//Read the size and allocated fields from address p
#define GET_SIZE(hp) (GET(hp)) //alli has doubts
//#define GET_ALLOC(hp) (GET(hp) & 0x1) 
//Get next and prev block pointers from a block
#define GET_NEXT(hp) ((char*)(hp)+2*WSIZE)
#define GET_PREV(hp) ((char*)(hp)+1*WSIZE)

//Given block ptr bp, compute address of its header and footer
//Should we ever reference a block ptr in an explicit list?
#define HDRP(bp) ((char*)(bp) - 3 * WSIZE))
#define FTRP(bp) ((char*)(bp) + GET_SIZE(HDRP(bp)))
#define BP(hp) ((char*)(hp) + (3 * WSIZE))

/* rounds up to the nearest multiple of ALIGNMENT */
#define ALIGN(size) (((size) + (ALIGNMENT-1)) & ~0x7)


#define SIZE_T_SIZE (ALIGN(sizeof(size_t)))

我正在获取

error: expected statement before ‘)’ token in C macro

我使用 PUT 的一个地方是:

static int extend_heap(size_t words) {
char* hp;
size_t size;

//This returns a ptr to the HEADER of the list allocatable block
void* last_block = find_end_of_free_list();
void* tail = GET_NEXT(last_block);



 //Only allocate an even number of words to maintain alignment
  size = (words % 2) ? (words+1) * WSIZE : words * WSIZE;
  if((long)(hp = mem_sbrk(size)) == -1)
    return -1;

  //Maybe we should make a function (create header/create footer when given an address)
  PUT(hp, size); //Free block header
  PUT(FTRP(BP(hp)), size); //Free block footer

  PUT(GET_PREV(hp), last_block); //Make prev of new block point to end of old list
  PUT(GET_NEXT(hp), tail); //Make next of new block point to tail

  PUT(GET_NEXT(last_block), hp); // Make next of old list point to new list
  return 0;
}

我在 PUT 上遇到错误。
在extend_heap中我收到另一个错误:

mm.c:121:3: note: in expansion of macro ‘PUT’ PUT(FTRP(BP(hp)), size); //Free block footer ^ mm.c:53:49: error: expected statement before ‘)’ token #define PUT(p,val) (*(unsigned int *)(p) = (val))

我在 PUT 上面包含了代码,因为我认为错误可能是由于上面的语法造成的。

最佳答案

该错误符合:

#define HDRP(bp) ((char*)(bp) - 3 * WSIZE))

末尾有一个额外的 )。应将其删除。 由于在 PUT 中您使用的是 FTRP() 宏,并且在该宏为 HDRP() 之前,我们会看到此错误。 删除额外的 ) 将解决您的问题。

关于c - 错误: expected statement before ‘)’ token in C macro,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27096974/

相关文章:

c - 从宏生成 `#define`

c - 我如何让 C 编译器安静下来,让函数指针接受任意数量的参数?

C - malloc 字符串时出错

c++ - 如何在 C 或 C++ 中创建异构链接列表

c - 为什么socket关闭后端口没有立即释放?

rust - Rust-混合使用默认宏和个人默认实现

c++ - 清理 C/C++ 代码揭示可变参数宏的问题

emacs - pretty-print 和扩展

c - 错误 : Variable-sized object may not be initialized

javascript - 在 Python 或 JavaScript 中实现 C 预处理器?