C++ 中是数组吗?和C一样吗?

发布于 2024-11-06 12:40:35 字数 138 浏览 2 评论 0原文

C++ 编译器对待数组的方式与 C 中的方式相同吗?

例如,

在 C 中,

  • 使用下标访问数组 运算符始终被解释为 指针。
  • 在函数参数、数组声明中 被视为指向 元素的开始。

Does the C++ compiler treat the arrays same way as in C?

E.g

In C,

  • An array access using subscript
    operator is always interpreted as a
    pointer.
  • In function argument, array declarations
    are treated as pointer to
    start of element.

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

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

发布评论

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

评论(5

深海少女心 2024-11-13 12:40:35

是和不是。大多数情况下,数组在两种语言中的工作方式相同(C99 支持可变长度数组,而 C++ 不支持,并且可能还存在一些其他细微差别)。

但是,您所说的也不完全正确。编译器不会将数组访问视为指针,即使在 C 中也是如此。在某些情况下,数组访问可能会更有效,因为编译器具有有关数组情况下可用的别名的更好信息。在 C 和 C++ 中,普通指针访问意味着编译器必须假设它可以为任何其他兼容类型别名。如果编译器只是将其视为指针取消引用,那么就会失去这个优化机会。

编辑
正如评论中指出的,语言标准确实根据指针算术/取消引用定义数组下标。当然,实际的编译器利用了指针实际上是数组的附加信息,因此它们并不完全像指针一样对待,但这可以被认为是超出标准要求的优化。

Yes and no. Arrays work the same in both languages for the most part (C99 supports variable-length arrays, while C++ doesn't, and there may be a few other subtle differnces as well).

However, what you're saying isn't exactly true either. The compiler doesn't treat an array access as a pointer, not even in C. An array access can be more efficient in some cases, because the compiler has better information on aliasing available in the array case. In both C and C++, a plain pointer access means that the compiler has to assume that it may alias any other compatible type. If the compiler simply treated it as a pointer dereference, then this optimization opportunity would be lost.

Edit
As pointed out in a comment, the language standard does define array subscripting in terms of pointer arithmetics/dereferencing. Of course, actual compilers make use of the additional information that a pointer is really an array, so they're not treated exactly like pointers, but that could be considered an optimization beyond what the standard mandates.

情释 2024-11-13 12:40:35

与 C99 中的不完全相同。 C99支持变量长度阵列(VLA),但是C ++没有。

void f(int n)
{
   int array[n]; //valid C99, but invalid C++
}

这意味着,C ++编译器不会将阵列与C(IE C99)编译器相同。

然而,其他版本的C(即C89)不支持VLA。所以 C89 数组[几乎]与 C++ 数组相同。

Not exactly same as in C99. C99 supports Variable Length Array (VLA), but C++ doesn't.

void f(int n)
{
   int array[n]; //valid C99, but invalid C++
}

That means, C++ compilers do not treat the arrays same way as do C (i.e C99) compilers.

However, other version of C (i.e C89) doesn't support VLA. So C89 arrays would be [almost] same as C++ arrays.

維他命╮ 2024-11-13 12:40:35

是的,他们受到同样的待遇。然而,在 C++ 中,您可能不应该使用它们 - 研究 std::vector 类!

Yes, they are treated in the same way. However, in C++ you probably should not be using them - investigate the std::vector class!

眉目亦如画i 2024-11-13 12:40:35

是的。在 C 和 C++ 中,数组的处理方式相同。然而,C++现在有了STL,它是数据结构及其操作的集合,例如字符串矢量deque

Yes. Arrays are treated in the same way in C and C++. However, C++ now has the STL, which is a collection of data structures and operations on them, such as string, vector, deque etc.

暗藏城府 2024-11-13 12:40:35

是的,除了其有趣且吸引人的 OOP 功能之外,C++ 是 C 语言的扩展版本。 Strostrupp 和其他人设计它的唯一目的是创建一种具有类似 C 语法的面向对象语言。基本上,大多数情况下两者都是相同的(不包括 C++ 的 OOP 功能),数组也不例外。

“数组基本上是指向顺序内存块的指针。其中数组的名称代表该块的第一个位置。”此声明对于 C 和 C++ 均适用。

数组实现是相同的,尽管 C++ 编译器允许您使用它们的方式存在一些限制。

Yes, C++ is an extended version of C language apart from its interesting and appealing OOP features. Strostrupp and other designed it with sole intention of creating a Object Oriented Language with C like syntax. Fundamentally both are same in most cases (excluding C++'s OOP features) and arrays are not an exception.

"An array is basically a pointer to a sequential memory block. where the name of the array represents the first location of that block." This statement is true for both C and C++.

Array Implementation is same though there are some restrictions in how C++ compilers allow you to use them.

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