首页 > 解决方案 > 预处理器宏重用相同的内存

问题描述

我有一个宏定义如下:

#define MAP_ACTUAL(input) do {\
    char testPath[256];\
    getcwd(testPath, sizeof(testPath));\
    strcat(testPath, "/actual");\
    strcat(testPath, input);\
    input = testPath;\
} while(0)

当我使用它一次时,它会按预期工作。当我在同一个函数中使用它两次时:

static int do_rename(const char *from, const char *to) {
    printf("[rename] %s -> %s\n", from, to);
    MAP_ACTUAL(from);
    MAP_ACTUAL(to);
    // rest of the function
}

我猜这些值fromto指向同一个地址,testPath导致它们都具有相同的值(不是故意的)。我的印象是,由于宏是在do while范围内定义的,它应该使用单独的地址。我怎样才能解决这个问题?

我正在使用 gcc 8.2.1。

标签: cgcc

解决方案


推荐阅读