When do you need large heap for Elasticsearch?

How do i declare an array statically, on a stack, and on a heap in c++?

  • I need to write three functions in C++: one that declares a large array statically, one that declares the same large array on the stack, and one that creates the same large array from a heap. Each function has to be called at least 100,000 times and output the time required by each. So far I have this but I got really confused: #include <iostream> #include "stdafx.h" using namespace std; void StaticArray() { static int [100,000]; } void HeapArray() { int *intnode; //create a pointer intnode = new int[100,000]; //create a heap-dynamic variable delete intnode; } } system("pause"); return 0;

  • Answer:

    You have the static version and the heap version fine, except for these problems: You forgot the NAME of the variable in the static version You used commas in the numbers. The compiler is guaranteed to choke on commas. To create it on the "stack", just declare it normally inside of a code block. E.g.: { // begin code block int variableName[100000]; // add to program stack // code code code and more code } // end code block, "pop" the program stack Any code block will work for this, even the function braces, so if you're putting it into a function, you don't need to add additional braces (although it doesn't hurt).

Anne at Yahoo! Answers Visit the source

Was this solution helpful to you?

Other answers

You have the static version and the heap version fine, except for these problems: You forgot the NAME of the variable in the static version You used commas in the numbers. The compiler is guaranteed to choke on commas. To create it on the "stack", just declare it normally inside of a code block. E.g.: { // begin code block int variableName[100000]; // add to program stack // code code code and more code } // end code block, "pop" the program stack Any code block will work for this, even the function braces, so if you're putting it into a function, you don't need to add additional braces (although it doesn't hurt).

Unca Alby

You should use the stack for small data items that have a lifetime that aligns with the scope their declared in. You should use heap for large objects whose lifetime is independent of the scope their created in. I would note that dynamic allocation is available both on the heap through functions like malloc and on the stack through functions like _alloca. You would use that for small buffers whose size you don't know at compile time. Also available is static memory, non-stack memory allocated at compile time. If you've got something you know you only need one of and needs a lifetime similar to the lifetime of the application then static is a good choice.

Dawn

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.