Solved

return an size unknown array

Posted on 2007-11-28
9
209 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 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:Kent Olsen
Kent Olsen 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
Industry Leaders: 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 45

Assisted Solution

by:Kent Olsen
Kent Olsen 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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.

732 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