Solved

return an size unknown array

Posted on 2007-11-28
9
206 Views
Last Modified: 2010-04-01
how do i store an array returned by a function?


the size of the array is determined by the function, so I don't know how big of an array to create before hand.


how do I stored it?

reception_array = return_array ()


0
Comment
Question by:Troudeloup
  • 3
  • 2
  • 2
  • +1
9 Comments
 
LVL 53

Accepted Solution

by:
Infinity08 earned 250 total points
ID: 20367961
The easiest way is to allocate the array on the heap :

        int *fun() {
            int *array = new int[10];
            // fill array
            return array;
        }

and call like this :

        int *array = fun();
        // do something with the array
        delete [] array;
0
 

Author Comment

by:Troudeloup
ID: 20367977
why heap?

it's  a very small array i need, i think stack is enough.
0
 
LVL 45

Assisted Solution

by:Kdo
Kdo earned 120 total points
ID: 20367994
Hi Troudeloup,

You might look into some of the already available classes to do the size management for you.  There is a vector class, as well as several list classes such as TList.

TList is probably overkill for an array of integers, though.  vectors is probably the way to go.



Good Luck,
Kent
0
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
LVL 45

Assisted Solution

by:Kdo
Kdo earned 120 total points
ID: 20368003
Hi Troudeloup,

The stack is automatically managed space that is used to hold program created objects (like parameter lists).

The heap is for user created objects, like those generated with 'new', malloc(), etc.


Good Luck,
Kent
0
 
LVL 86

Assisted Solution

by:jkr
jkr earned 130 total points
ID: 20368014
In such a situation, a 'std::vector' will be eaier to handle, since it also holds information about the size and manages the storage issue transparently, i.e. you don't have to care about that. You can just use it like

#include <windows.h>
#include <iostream>
#include <vector>
#include <string.h>
using namespace std;

vector<HWND> vw; // array for HWND

BOOL CALLBACK EnumWindowsProc(HWND hwnd,LPARAM lParam)
{
      char buf [1024];
      GetWindowText(hwnd,buf,1024);

      if(!strstr(buf,(char*)lParam)) return true;

       vw.push_back(hwnd);

      return true;
}

void PrintVCector(vector<HWND>& vw)
{
      for (vector<HWND>::iterator i = vw.begin(); i != vw.end(); ++i)
      {
            cout << "HWND: " << *i << endl;
            ShowWindow(*i , SW_SHOWNORMAL);
            SetForegroundWindow(*i);
      }}

int main()
{
      EnumWindows(EnumWindowsProc,(LPARAM)"Editor");
     
      PrintVector(vw);

   return 0;
}
0
 
LVL 53

Assisted Solution

by:Infinity08
Infinity08 earned 250 total points
ID: 20368016
>> why heap?
>> 
>> it's  a very small array i need, i think stack is enough.

You don't know the size of the array at compile time, and you have to return the array from the function. Those are two very good reasons to use the heap instead of the stack.


Kdo's suggestion of using a vector is even better !
0
 

Author Comment

by:Troudeloup
ID: 20368027
uh, it's just 5 or 6 elements of type hwnd.

i think using containers is really an overkill :)

0
 
LVL 86

Assisted Solution

by:jkr
jkr earned 130 total points
ID: 20368068
No, not really. They're always safer to use and you won't have to worry if the amount grows larger. The performance impact is minimal.
0
 
LVL 53

Assisted Solution

by:Infinity08
Infinity08 earned 250 total points
ID: 20368071
>> uh, it's just 5 or 6 elements of type hwnd.
>> 
>> i think using containers is really an overkill :)

What do you really want to do ?

Using containers does not give that much overhead, and makes your life easier, assuming that the two assumptions from your question still stand : size is unknown at compile time, and the array has to be returned from a function.
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question