简体   繁体   中英

C/C++ Allocation

Giving a number X and reading X numbers into an uni-dimensional array, which of the following ways is the best(fastest as execution time)?

Please note that X is a number between 1 and 1000000

scanf("%d", &x);
int array[x];
//continue reading X numbers into array

Or

scanf("%d", &x);
int array[1000000];
//continue reading X ...

Or

scanf("%d", &x);
int * array = malloc(x*sizeof(int));
//same as above
free(array);

Or the C++ dynamic allocation method?

Note 1: that I am posting this from a mobile phone, I hope the format for the code above is fine, if not, I ask nicely somebody (<3) to edit it, since it is painfull to indent code from a phone.

Note 2: How could I test what I asked above?

You'll get compilation error for this code:

scanf("%d", &x);
int array[x];

x should be known at compilation time in this case.

When using int array[1000000] you allocate memory on the stack, not in the heap, so it's fundamental difference comparing to malloc or new operator . It would be faster because it takes actually only one CPU command of modifying stack pointer.

If comparing malloc and new , malloc will be faster because new will eventually call malloc inside. But the performance gain will be tiny, It doesn't worth to optimize your c++ program in this way, just use c++ when you need to allocate dynamic memory.

Since there appears scanf (and the comments assume that there's another million calls to scanf ) any questions regarding the memory allocation in combination with "Which is fastest?" can be universally answered with: "Yes" (read as: irrelevant).

While automatic storage ("stack allocation") is generally faster than freestore, it is entirely insignificant compared to the time you will spend in scanf . That being said, it is usually (not necessarily, but usually) dynamic deallocation which is slow, not allocation.

A couple of points to note in general on that code:

  1. Reading an integer from some external source (file, network, argv, whatever) and doing an allocation based on that number without doing a sanity check first is massively bad karma. This is bound to cause a problem one day, it is how many existing real-world exploits came into being. Do not trust blindly that any number that you got from somewhere is automatically valid. Even if no malice is involved, accident may still provide an invalid number which will cause catastrophic failure.
  2. Allocating a non-constant sized array on the stack will work under recent versions of C and will "work" as an extension even under C++ if you use GCC, but it is normally not allowable in C++ (meaning it will fail to compile).
  3. Allocating a million integers means roughly 4MB of memory, which is pretty harsh towards your maximum stack size (often only 1MB). Expect a stack overflow happening .
  4. Allocating an unknown number of integers (but expecting the number to be up to a million) is similar to (3).
  5. The worst thing re (3) and (4) is that it may actually succeed . Which possibly means your program will unexpectedly crash later (encountering a stack overflow), in an entirely unrelated innocent piece of code. And you will wonder why that happens, since the code that crashes looks like it is perfectly valid (and it is, indeed!).

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