简体   繁体   English

C/C++ 分配

[英]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)?给出一个数字X并将X个数字读入一维数组,以下哪种方式最好(执行时间最快)?

Please note that X is a number between 1 and 1000000请注意 X 是 1 到 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?还是C++动态分配方法?

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.注意 1:我是从手机上发布的,我希望上面代码的格式没问题,如果没有,我会请人(<3)编辑它,因为从手机中缩进代码很痛苦。

Note 2: How could I test what I asked above?注 2:我如何测试我上面提出的问题?

You'll get compilation error for this code:您将收到此代码的编译错误:

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

x should be known at compilation time in this case.在这种情况下,应该在编译时知道 x。

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 .使用int array[1000000]您在堆栈上分配内存,而不是在堆中,因此与mallocnew operator相比,这是根本区别。 It would be faster because it takes actually only one CPU command of modifying stack pointer.它会更快,因为它实际上只需要一个 CPU 命令来修改堆栈指针。

If comparing malloc and new , malloc will be faster because new will eventually call malloc inside.如果比较mallocnewmalloc会更快,因为 new 最终会在内部调用malloc 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.但是性能提升会很小,用这种方式优化你的c++程序是不值得的,只需要在需要分配动态内存时使用c++。

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?"由于出现scanf (并且评论假设还有一百万次调用scanf )关于内存分配以及“哪个最快?”的任何问题。 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 .虽然自动存储(“堆栈分配”)通常比 freestore 快,但与您将在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.从某个外部源(文件、网络、argv 等)读取一个整数并根据该数字进行分配而不先进行完整性检查是非常糟糕的业力。 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).在堆栈上分配一个非常量大小的数组将在最新版本的 C 下工作,并且如果您使用 GCC,即使在 C++ 下也可以作为扩展“工作”,但在 C++ 中通常是不允许的(这意味着它将无法编译)。
  3. Allocating a million integers means roughly 4MB of memory, which is pretty harsh towards your maximum stack size (often only 1MB).分配一百万个整数意味着大约 4MB 的内存,这对您的最大堆栈大小(通常只有 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).分配未知数量的整数(但预计数量最多可达一百万)类似于(3)。
  5. The worst thing re (3) and (4) is that it may actually succeed . re (3) 和 (4) 最糟糕的是它实际上可能会成功 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!).您会想知道为什么会发生这种情况,因为崩溃的代码看起来完全有效(而且确实如此!)。

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

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