- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
是否可以在复合语句中使用 alloca?示例:
typedef struct
{
size_t len;
char* data;
} string_t;
#define str_to_cstr(str) \
({ \
char* v = alloca(str.len + 1); \
v[len] = 0; \
memcpy(v, str.data, str.len); \
})
// ... and somewhere in deep space
int main()
{
string_t s = {4, "test"};
printf("%s\n", str_to_cstr(s));
return 0;
}
根据我的经验,它运行良好,但我不确定它是否安全。顺便说一句,它是用 gcc 4.8.4 编译的
最佳答案
在您的示例中不安全:
printf("%s\n", str_to_cstr(s));
来自 glibc documentation分配:
Do not use alloca inside the arguments of a function call—you will get unpredictable results, because the stack space for the alloca would appear on the stack in the middle of the space for the function arguments. An example of what to avoid is foo (x, alloca (4), y).
请注意,({})
不是复合语句,而是 GNU C statement expression .
关于c - alloca inside 复合语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31360271/
malloc 分配的内存可以用realloc 重新分配。 alloca 有类似的功能吗?当您不想在堆上分配内存并且需要多次分配变量堆栈内存时,重新分配堆栈内存可能很有用,例如在库函数中,您需要动态内存
考虑返回动态或自动数组。与 C 语言并不真正相关。 返回数组的常用技术是:A)被调用者在堆上分配并返回,B)调用者在堆栈上分配并传递给被调用者。 // A void caller(void) {
在使用 std vector 保存我的国际象棋引擎的移动列表后,我意识到因为国际象棋的平均因子为 35(即从典型位置开始大约 35 次合法移动), vector 的大小调整了很多,从而对移动生成器的性
这是两个重叠的问题——我想为大型数组尝试 alloca() 而不是在堆上分配动态大小的数组。这样我就可以提高性能而不必进行堆分配。但是,我的印象是堆栈大小通常很小?增加堆栈的大小以便我可以充分利用 a
当您总是可以在足够大的堆栈上分配一个固定大小的缓冲区以满足所有用途时,您为什么要使用 alloca() 呢?这不是反问... 最佳答案 如果缓冲区的大小在运行时变化,或者如果您只是有时需要它,它可能会
我读过很多地方说 alloca 已经过时,不应该使用,应该使用可变长度数组。 我的问题是:alloca 是否可以完全替换为可变长度数组? 在我的特定实例中,我有一些看起来像这样的东西: typedef
引用 BUGS 部分的第二段,来自 alloca(3) 的联机帮助页 On many systems alloca() cannot be used inside the list of argume
我正在尝试编写一个函数,该函数传递一个函数以用于分配作为其参数;它应该接受类型为 void *(*)(size_t) 的任何有效分配器。但是,当我尝试使用 alloca 作为分配器时,我遇到了奇怪的行
如何在 D、C 和 C++ 等语言中使用内联 x86 汇编器实现 alloca()?我想创建一个稍微修改过的版本,但首先我需要知道标准版本是如何实现的。从编译器读取反汇编并没有帮助,因为它们执行了很多
我想为我的玩具编译器设计一个 IR(如 LLVM IR),但我不知道alloca的目的是什么?指导进一步分析。其中优化alloca信息被使用? 最佳答案 The ‘alloca‘ instructio
我正在尝试实现自己的数学库,并且从 vector 开始。这个想法是给类一个指向数字数组的指针,然后复制数组并将其存储在私有(private)变量指针给出的数据地址中。首先,我使用了 alloca尝试为
在 C 中,alloca() 函数在 alloca() 的调用者的栈帧上分配内存。 当您尝试分配它不可能分配的大量字节时会发生什么? 它是否分配尽可能多的字节直到堆栈遇到堆段? 或者它根本不分配任何东
一般理论问题。 我正在尝试从源代码构建一个库(FFTW,如果有人关心的话,但这真的不重要),我注意到有一个选项可以禁用 alloca。 我知道使用 alloca 的危险,但我正在评估使用和不使用 al
这段代码 #include "alloca.h" String str = "abc"; unsigned int *i; void setup() { Serial.begin(9600);
我有一个智能指针类型,我想构造一个对象,该对象采用该类型的指针和一个计数(在运行时动态计算),并从堆栈中分配足够的内存来容纳该智能对象的许多实例指针指向。我似乎找不到实现此目的的正确语法;可能吗? 给
alloca() 在堆栈上而不是在堆上分配内存,如 malloc() 的情况。所以,当我从例程返回时,内存被释放。所以,实际上这解决了我释放动态分配内存的问题。释放通过 malloc() 分配的内存是
我需要在函数中记录一些消息,例如: void log(char* format, ...); 对于格式缓冲区有几种方法: 有一个固定大小的缓冲区。这不会导致溢出,但可能会截断消息。 void log1
我正在维护一个遗留的 C++ 应用程序,它似乎有一个缓慢的内存泄漏。我已经设法通过确保当前配置不再抛出任何异常来“修复”内存泄漏,并且我还可以触发泄漏并通过将其配置为导致许多异常来扩展它。 所有分配的
是否可以在复合语句中使用 alloca?示例: typedef struct { size_t len; char* data; } string_t; #define str_to_
我正在阅读嵌入式软件基础 - Where C and Assembly Meet (2001),看到了以下代码: FILE *OpenFile(char *name, char *ext, char
我是一名优秀的程序员,十分优秀!