Solved

return an size unknown array

Posted on 2007-11-28
9
205 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Converting to C# also VS2015 5 117
C++ standard library based binary archive format 6 90
How to split this in C++ 4 93
object oriented programming comparison 5 52
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
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 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 additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

943 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

5 Experts available now in Live!

Get 1:1 Help Now