Solved

return an size unknown array

Posted on 2007-11-28
9
203 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
 
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

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…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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.

743 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now