Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <blockquote> <p>how can i return a array in a c++ method and how must i declare it? int[] test(void); ??</p> </blockquote> <p>This sounds like a simple question, but in C++ you have quite a few options. Firstly, you should prefer...</p> <ul> <li><p><a href="http://en.cppreference.com/w/cpp/container/vector" rel="noreferrer"><code>std::vector&lt;&gt;</code></a>, which grows dynamically to however many elements you encounter at runtime, or</p></li> <li><p><a href="http://en.cppreference.com/w/cpp/container/array" rel="noreferrer"><code>std::array&lt;&gt;</code></a> (introduced with C++11), which always stores a number of elements specified at compile time,</p></li> </ul> <p>...as they manage memory for you, ensuring correct behaviour and simplifying things considerably:</p> <pre><code>std::vector&lt;int&gt; fn() { std::vector&lt;int&gt; x; x.push_back(10); return x; } std::array&lt;int, 2&gt; fn2() // C++11 { return {3, 4}; } void caller() { std::vector&lt;int&gt; a = fn(); const std::vector&lt;int&gt;&amp; b = fn(); // extend lifetime but read-only // b valid until scope exit/return std::array&lt;int, 2&gt; c = fn2(); const std::array&lt;int, 2&gt;&amp; d = fn2(); } </code></pre> <p>The practice of creating a <code>const</code> reference to the returned data can sometimes avoid a copy, but normally you can just rely on Return Value Optimisation, or - for <code>vector</code> but not <code>array</code> - move semantics (introduced with C++11).</p> <p>If you really want to use an <em>inbuilt</em> array (as distinct from the Standard library class called <code>array</code> mentioned above), one way is for the caller to reserve space and tell the function to use it:</p> <pre><code>void fn(int x[], int n) { for (int i = 0; i &lt; n; ++i) x[i] = n; } void caller() { // local space on the stack - destroyed when caller() returns int x[10]; fn(x, sizeof x / sizeof x[0]); // or, use the heap, lives until delete[](p) called... int* p = new int[10]; fn(p, 10); } </code></pre> <p>Another option is to wrap the array in a structure, which - unlike raw arrays - are legal to return by value from a function:</p> <pre><code>struct X { int x[10]; }; X fn() { X x; x.x[0] = 10; // ... return x; } void caller() { X x = fn(); } </code></pre> <p>Starting with the above, if you're stuck using C++03 you might want to generalise it into something closer to the C++11 <code>std::array</code>:</p> <pre><code>template &lt;typename T, size_t N&gt; struct array { T&amp; operator[](size_t n) { return x[n]; } const T&amp; operator[](size_t n) const { return x[n]; } size_t size() const { return N; } // iterators, constructors etc.... private: T x[N]; }; </code></pre> <p>Another option is to have the called function allocate memory on the heap:</p> <pre><code>int* fn() { int* p = new int[2]; p[0] = 0; p[1] = 1; return p; } void caller() { int* p = fn(); // use p... delete[] p; } </code></pre> <p>To help simplify the management of heap objects, many C++ programmers use "smart pointers" that ensure deletion when the pointer(s) to the object leave their scopes. With C++11:</p> <pre><code>std::shared_ptr&lt;int&gt; p(new int[2], [](int* p) { delete[] p; } ); std::unique_ptr&lt;int[]&gt; p(new int[3]); </code></pre> <p>If you're stuck on C++03, the best option is to see if the boost library is available on your machine: it provides <code>boost::shared_array</code>.</p> <p>Yet another option is to have some static memory reserved by <code>fn()</code>, though this is NOT THREAD SAFE, and means each call to <code>fn()</code> overwrites the data seen by anyone keeping pointers from previous calls. That said, it can be convenient (and fast) for simple single-threaded code.</p> <pre><code>int* fn(int n) { static int x[2]; // clobbered by each call to fn() x[0] = n; x[1] = n + 1; return x; // every call to fn() returns a pointer to the same static x memory } void caller() { int* p = fn(3); // use p, hoping no other thread calls fn() meanwhile and clobbers the values... // no clean up necessary... } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload