简体   繁体   中英

Dynamic array in Stack?

Is this correct ? This is compiled with g++ (3.4) sucessfully.

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++. It is standard C99. This is because C99 allows you to declare arrays dynamically that way. To clarify, this is also standard 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...)

To make it standard C++, do this:

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++. You can do variable length arrays in C99 (ISO/IEC 9899:1999) but they are not part of C++. As you've discovered, they are supported as an extension by some compilers.

G++ supports a C99 feature that allows dynamically sized arrays. It is not standard C++. G++ has the -ansi option that turns off some features that aren't in C++, but this isn't one of them. To make G++ reject that code, use the -pedantic option:

$ 
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. You could do this by using template wrapper to alloca function. But using alloca is not really a standard conforming.

Standard way is to use std::vector with custom allocator if you want to avoid memory fragmentation and speedup memory allocations. Take a look on boost::pool_alloc for a good sample of fast allocator.

Practically speaking, if you want to make a dynamic array you should use std::vector, as in:

#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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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