C 中的动态数组没有 malloc?

发布于 2024-11-19 10:10:26 字数 418 浏览 3 评论 0原文

我一直想知道如何摆脱这个问题:

int main(int argc, char **argv) {
    printf("%p %s %d\n", &argv[1], argv[1], strlen(argv[1]));
    char copy[strlen(argv[1]) + 1];
    strcpy(copy, argv[1]);
    printf("%p %s %d\n", &copy, copy, strlen(copy));
    return 0;
}

无论如何,字符数组copy都会被分配,并且程序运行良好,打印出原始文件和副本。 Valgrind 没有抱怨任何事情。

我认为如果没有 malloc,动态 数组在 C 中是不可能的。我错了吗?

I've always wondered how I could get away with this:

int main(int argc, char **argv) {
    printf("%p %s %d\n", &argv[1], argv[1], strlen(argv[1]));
    char copy[strlen(argv[1]) + 1];
    strcpy(copy, argv[1]);
    printf("%p %s %d\n", ©, copy, strlen(copy));
    return 0;
}

The char array copy gets allocated anyway and the program runs fine, printing out the original and the copy. And Valgrind doesn’t complain about anything.

I thought dynamic arrays weren’t possible in C without malloc. Was I wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

踏月而来 2024-11-26 10:10:26

这是一项 C99 功能,可以由编译器在早期版本上实现

ISO C99 中允许使用可变长度自动数组,并且作为
扩展 GCC 在 C90 模式和 C++ 中接受它们。这些数组是
像任何其他自动数组一样声明,但长度为
不是常量表达式。存储空间是在以下时刻分配的
声明并在退出大括号级别时释放。

This is a C99 feature and could be implemented on prior versions by the compiler.

Variable-length automatic arrays are allowed in ISO C99, and as an
extension GCC accepts them in C90 mode and in C++. These arrays are
declared like any other automatic arrays, but with a length that is
not a constant expression. The storage is allocated at the point of
declaration and deallocated when the brace-level is exited.

一袭水袖舞倾城 2024-11-26 10:10:26

可变长度数组起源于 GCC 扩展 ,但它们也被 C99 采用。

它们仍然在堆栈上分配,因此使它们“巨大”被认为是不好的风格(并且有一天可能会破坏您)。

Variable-length arrays originated as a GCC extension, but they were also adopted by C99.

They are still being allocated on the stack, so making them "huge" is considered bad style (and will likely break on you someday).

爱她像谁 2024-11-26 10:10:26

甚至在 gcc 和 C99 提供的“可变长度数组”存在之前,就已经存在:

alloca()——它允许动态分配堆栈(“自动”)内存。

Even before the existence of "variable length arrays," courtesy gcc and C99, there was:

alloca() -- which allows dynamic allocation of stack ("automatic") memory.

烟若柳尘 2024-11-26 10:10:26

“变长数组”是在C99中被添加到C语言中的。这在第 6.7.5.2 节“数组声明符”中进行了介绍:

如果大小是一个不是整型常量的表达式
表达式:如果它出现在函数原型范围的声明中,
它被视为被 * 替换;否则每次都是
评估后其值应大于零。每个的大小
可变长度数组类型的实例在其运行期间不会改变
寿命。其中 size 表达式是 sizeof 操作数的一部分
运算符并更改大小表达式的值不会
是否影响算子的结果,未明确
计算大小表达式。

"Variable length arrays" were added to the C language in C99. This is covered in §6.7.5.2 "Array declarators":

If the size is an expression that is not an integer constant
expression: if it occurs in a declaration at function prototype scope,
it is treated as if it were replaced by *; otherwise, each time it is
evaluated it shall have a value greater than zero. The size of each
instance of a variable length array type does not change during its
lifetime. Where a size expression is part of the operand of a sizeof
operator and changing the value of the size expression would not
affect the result of the operator, it is unspecified whether or not
the size expression is evaluated.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文