ISO C等同于表达式中的支撑组

如何以兼容(ISO C99)方式执行以下操作?

#define MALLOC(type, length, message) ({ \ type * a_##__LINE__ = (type *)malloc((length) * sizeof(type)); \ assert(message && (a_##__LINE__ != NULL)); \ a_##__LINE__; \ }) double **matrix = MALLOC(double *, height, "Failed to reserve"); 

注意:要编译我使用:gcc -std = c99 -pedantic …

您不应该将malloc()的测试放在assert() :当您执行发布版本时,它不会被编译。 我没有在以下程序中使用assert()

 #include  #include  void *mymalloc(size_t siz, size_t length, const char *message, const char *f, int l) { void *x = malloc(siz * length); if (x == NULL) { fprintf(stderr, "a.out: %s:%d: MALLOC: " "Assertion `\"%s\" && x != ((void *)0)' failed.\n", f, l, message); fprintf(stderr, "Aborted\n"); exit(EXIT_FAILURE); } return x; } #define MALLOC(type, length, message)\ mymalloc(sizeof (type), length, message, __FILE__, __LINE__); int main(void) { int height = 100; double **matrix = MALLOC(double *, height, "Failed to reserve"); /* work; */ free(matrix); return 0; } 

没有标准等效于您使用的GCC扩展。

您可以使用函数(如果使用C99,甚至可能是内联函数)代替宏中的代码来实现等效结果。 您仍然需要一个宏来调用该函数,因为其中一个参数是“类型名称”,您无法将它们传递给函数。

请参阅@pmg的答案,了解使用它的函数类型和宏。