?
Solved

return an size unknown array

Posted on 2007-11-28
9
Medium Priority
?
212 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +1
9 Comments
 
LVL 53

Accepted Solution

by:
Infinity08 earned 1000 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 46

Assisted Solution

by:Kent Olsen
Kent Olsen earned 480 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 46

Assisted Solution

by:Kent Olsen
Kent Olsen earned 480 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 520 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 1000 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 520 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 1000 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

765 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