c - 预处理器宏重用相同的内存
问题描述
我有一个宏定义如下:
#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
}
我猜这些值from
和to
指向同一个地址,testPath
导致它们都具有相同的值(不是故意的)。我的印象是,由于宏是在do while
范围内定义的,它应该使用单独的地址。我怎样才能解决这个问题?
我正在使用 gcc 8.2.1。
解决方案
推荐阅读
- fluentd - fluentd动态删除键
- python - 如何在 python selenium 中的特定位置移动滑块(我找到了其他解决方案,但它们适用于 javascript 或 java )?
- c# - 抛出异常时响应时间长
- c - STM32微控制器类型转换
- r - 如何对特定列进行子集化?
- javascript - 你能在 React/JS 的组件名称中使用子索引吗
- flutter - 我们如何使用 ArCore 在 Flutter 中实现类似 snapchat 的过滤器
- android - 以编程方式批量安装 APK
- roblox - Roblox Studio:商店系统
- delphi - 将 5 个 ListBox 同步在一起