简体   繁体   English

Stack中的动态数组?

[英]Dynamic array in Stack?

Is this correct ? 它是否正确 ? This is compiled with g++ (3.4) sucessfully. 这是用g ++(3.4)成功编译的。

int main()
{
    int x = 12;
    char pz[x]; 
}

Here's your combination answer of all these other ones: 以下是您对所有这些其他方面的组合答案:

Your code right now is not standard C++. 您的代码现在不是标准C ++。 It is standard C99. 标准C99。 This is because C99 allows you to declare arrays dynamically that way. 这是因为C99允许您以这种方式动态声明数组。 To clarify, this is also standard C99: 澄清一下,这也是标准C99:

#include <stdio.h>

int main()
{
    int x = 0;

    scanf("%d", &x);

    char pz[x]; 
}

This is not standard anything: 不是标准的东西:

#include <iostream>

int main()
{
    int x = 0;
    std::cin >> x;
    char pz[x]; 
}

It cannot be standard C++ because that required constant array sizes, and it cannot be standard C because C does not have std::cin (or namespaces, or classes, etc...) 它不能是标准的C ++,因为它需要常量数组大小,并且它不能是标准C,因为C没有std::cin (或名称空间,或类等等)

To make it standard C++, do this: 要使其成为标准C ++,请执行以下操作:

int main()
{
    const int x = 12; // x is 12 now and forever...
    char pz[x]; // ...therefore it can be used here
}

If you want a dynamic array, you can do this: 如果需要动态数组, 可以执行以下操作:

#include <iostream>

int main()
{
    int x = 0;
    std::cin >> x;

    char *pz = new char[x];

    delete [] pz;
}

But you should do this: 但你应该这样做:

#include <iostream>
#include <vector>

int main()
{
    int x = 0;
    std::cin >> x;

    std::vector<char> pz(x);
}

Technically, this isn't part of C++. 从技术上讲,这不是C ++的一部分。 You can do variable length arrays in C99 (ISO/IEC 9899:1999) but they are not part of C++. 您可以在C99(ISO / IEC 9899:1999)中执行可变长度数组,但它们不是C ++的一部分。 As you've discovered, they are supported as an extension by some compilers. 正如您所发现的,一些编译器支持它们作为扩展。

G++ supports a C99 feature that allows dynamically sized arrays. G ++支持C99功能,允许动态调整大小的数组。 It is not standard C++. 它不是标准的C ++。 G++ has the -ansi option that turns off some features that aren't in C++, but this isn't one of them. G ++有-ansi选项可以关闭一些不在C ++中的功能,但这不是其中之一。 To make G++ reject that code, use the -pedantic option: 要使G ++拒绝该代码,请使用-pedantic选项:

$ g++ -pedantic junk.cpp
junk.cpp: In function ‘int main()’:
junk.cpp:4: error: ISO C++ forbids variable-size array ‘pz’

If you want a dynamic array on the stack: 如果要在堆栈上使用动态数组:

void dynArray(int x)
{
    int *array = (int *)alloca(sizeof(*array)*x);

    // blah blah blah..
}

Allocating arrays with variable length on the stack is a good idea, because it fast and doesn't fragment the memory. 在堆栈上分配具有可变长度的数组是一个好主意,因为它快速且不会碎片化内存。 But C++ Standard unfortunately doesn't support it. 但不幸的是,C ++标准不支持它。 You could do this by using template wrapper to alloca function. 您可以通过使用模板的包装要做到这一点alloca功能。 But using alloca is not really a standard conforming. 但是使用alloca并不是真正符合标准的。

Standard way is to use std::vector with custom allocator if you want to avoid memory fragmentation and speedup memory allocations. 标准方法是使用std :: vector和自定义分配器,如果你想避免内存碎片和加速内存分配。 Take a look on boost::pool_alloc for a good sample of fast allocator. 看一下boost :: pool_alloc ,获得快速分配器的一个很好的样本。

Practically speaking, if you want to make a dynamic array you should use std::vector, as in: 实际上,如果你想创建一个动态数组,你应该使用std :: vector,如:

#include <iostream>
#include <cstdlib>
#include <vector>

int main(int argc, char* argv[])
{
   int size;
   std::cin>>size;
   std::vector<int> array(size);
   // do stuff with array ...
   return 0; 
}

If you are just curious about the syntax, then what you are looking for is: 如果您只是对语法感到好奇,那么您要寻找的是:

//...
int* array = new int[size];
// Do stuff with array ...
delete [] array;
//...

Neither of these are allocated with local storage. 这些都没有分配本地存储。 A dynamically sized array that is automatically allocated using local storage is not currently supported in standard C++, but is supported in the current C standard. 标准C ++目前不支持使用本地存储自动分配的动态大小的数组,但在当前的C标准中受支持。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM