943,480 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 289246
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
May 11th, 2004
2

Return Array from C++

Expand Post »
Hi all,
Could anyone help me about returning the array from a function? I believe that it is a syntax error (compile time error) to return an array from a function, but one book mentions briefly that using pointer will solve the issue.
However, my current knowledge does not allow me to see how the pointer can solve this C++ limitation (return a pointer might be dangerous because it might point to the address of local variable, and this variable will go out of scope. Hence, this pointer will point to invalid value). I appreciate for your information.
Similar Threads
Reputation Points: 21
Solved Threads: 0
Newbie Poster
tlee is offline Offline
15 posts
since May 2004
May 11th, 2004
0

Re: Return Array from C++

Quote originally posted by tlee ...
However, my current knowledge does not allow me to see how the pointer can solve this C++ limitation (return a pointer might be dangerous because it might point to the address of local variable, and this variable will go out of scope. Hence, this pointer will point to invalid value). I appreciate for your information.
Pass a pointer to the start as a parameter to the function, and then return this pointer when you're done.
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2.  
  3. int *foo(int *array)
  4. {
  5. int *start = array;
  6. while ( *array )
  7. {
  8. std::cout << *array++ << ' ';
  9. }
  10. std::cout << std::endl;
  11. return start;
  12. }
  13.  
  14. int *bar(int *array)
  15. {
  16. int *start = array;
  17. while ( *array )
  18. {
  19. *array++ += 1;
  20. }
  21. return start;
  22. }
  23.  
  24. int main()
  25. {
  26. int myarray[] = {1,2,3,4,5,0};
  27. foo(bar(foo(bar(foo(myarray)))));
  28. return 0;
  29. }
  30.  
  31. /* my output
  32. 1 2 3 4 5
  33. 2 3 4 5 6
  34. 3 4 5 6 7
  35. */
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
May 11th, 2004
0

Re: Return Array from C++

I am sorry. I did not make my point clear. Is there any way to return the array from the function, this array is created within that function? For instance, I want to have one function to return an array like int[] getArray() (it is valid in Java, but not in C++), so is there any pointer trick to create one such function in C++?
Thanks in advance, by the way, thanks Dava
Reputation Points: 21
Solved Threads: 0
Newbie Poster
tlee is offline Offline
15 posts
since May 2004
May 11th, 2004
0

Re: Return Array from C++

Hi all,
We can see that the result is not what we are expected. Can anyone tell me what need to fix in order to get the correct result using same function? Thanks in advance.

#include <iostream>
#define LENGHT 5

using std::cout;
using std::endl;

int* getArray();
void printArray(int* array);

int main()
{
int* array = getArray();

cout << "array in main" << endl;
printArray(array);

return 0;
}

int* getArray()
{
int array[LENGHT] = {1, 2, 3, 4, 5};

cout << "array in getArray()" << endl;
printArray(array);

return array;
}

void printArray(int* array)
{
for (int i = 0; i < LENGHT; i++)
cout << *(array+i) << " ";

cout << endl;
cout << endl;
}

/*
Output
array in getArray()
1 2 3 4 5

array in main
-858993460 -858993460 -858993460 3 1245056

Press any key to continue

*/
Reputation Points: 21
Solved Threads: 0
Newbie Poster
tlee is offline Offline
15 posts
since May 2004
May 11th, 2004
0

Re: Return Array from C++

One way:
int* getArray()
{
   static int array[LENGHT] = {1, 2, 3, 4, 5};

   cout << "array in getArray()" << endl;
   printArray(array);

   return array;
}
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
May 11th, 2004
0

Re: Return Array from C++

Hi Dave,
I appreciate for your help, but it does not work correctly in the following case. We declare static local variable, so this variable will stay until the program is terminated. Hence, although we want to have 2 different arrays, the first one is modified because 2 pointers in main() actually point in the same memory location. Would you show me another way to do it? By the way, in the first version, I mistyped LENGHT, sorry for that.

#include <iostream>
#define LENGTH 5

using std::cout;
using std::endl;
using std::cin;

int* getArray();
void printArray(int* array);

int main()
{
int* array = getArray();

cout << "array in main" << endl;
printArray(array);

int* array2 = getArray();

cout << "array in main" << endl;
printArray(array2);

cout << "array in main" << endl;
printArray(array);

return 0;
}

int* getArray()
{
int static array[LENGTH];

for (int i = 0; i < LENGTH; i++)
{
cout << "Enter an integer: ";
cin >> array[i];
}

cout << "array in getArray()" << endl;
printArray(array);

return array;
}

void printArray(int* array)
{
for (int i = 0; i < LENGTH; i++)
cout << *(array+i) << " ";

cout << endl;
cout << endl;
}

/*
Output:

Enter an integer: 1
Enter an integer: 2
Enter an integer: 3
Enter an integer: 4
Enter an integer: 5
array in getArray()
1 2 3 4 5

array in main
1 2 3 4 5

Enter an integer: 10
Enter an integer: 20
Enter an integer: 30
Enter an integer: 40
Enter an integer: 50
array in getArray()
10 20 30 40 50

array in main
10 20 30 40 50

array in main
10 20 30 40 50

Press any key to continue

*/
Reputation Points: 21
Solved Threads: 0
Newbie Poster
tlee is offline Offline
15 posts
since May 2004
May 11th, 2004
0

Re: Return Array from C++

Quote originally posted by tlee ...
Hence, although we want to have 2 different arrays, the first one is modified because 2 pointers in main() actually point in the same memory location.
Ah. You want a function that creates a new array each time it is called.
#include <iostream>
 
#define LENGTH 5
 
using std::cout;
using std::endl;
using std::cin;
 
int* getArray();
void printArray(int* array);
 
int main()
{
   int* array = getArray();
   cout << "array in main" << endl;
   printArray(array);
   int* array2 = getArray();
   cout << "array2 in main" << endl;
   printArray(array2);
   cout << "array in main" << endl;
   printArray(array);
delete[] array2;
   delete[] array;
   return 0;
}
 
int* getArray()
{
int *array = new int[LENGTH];
   for ( int i = 0; i < LENGTH; i++ )
   {
	 cout << "Enter an integer: ";
	 cin >> array[i];
   }
   cout << "array in getArray()" << endl;
   printArray(array);
   return array;
}
 
void printArray(int* array)
{
   for ( int i = 0; i < LENGTH; i++ )
	 cout << *(array+i) << " ";
   cout << endl;
   cout << endl;
}
 
/* my output
Enter an integer: 1
Enter an integer: 2
Enter an integer: 3
Enter an integer: 4
Enter an integer: 5
array in getArray()
1 2 3 4 5 
array in main
1 2 3 4 5 
Enter an integer: 6
Enter an integer: 7
Enter an integer: 8
Enter an integer: 9
Enter an integer: 10
array in getArray()
6 7 8 9 10 
array2 in main
6 7 8 9 10 
array in main
1 2 3 4 5 
*/
Last edited by The Other Dave; May 11th, 2004 at 5:47 pm. Reason: Put back in (most of) the indention spaces that were removed when posting.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
May 11th, 2004
0

Re: Return Array from C++

Thanks Dave. However, would you mind if I have another question regarding to the same problem? While I was waiting for your solution, I developed one in below.
I know your code is much superior than mine. I am just confused that why the compiler issues the error when I try to delete the pointers. Could you give me the explanation please?

#include <iostream>
#define LENGTH 5

using std::cout;
using std::cin;
using std::endl;

class Array
{
public:
Array()
{
array = new int(LENGTH);
}

~Array()
{
delete[] array;
}

void setElement(int index, int value)
{
if ( index < 0 || index >= LENGTH )
cout << "INVALID INDEX" << endl;

array[index] = value;
}

int getElement(int index) const
{
if ( index < 0 || index >= LENGTH )
cout << "INVALID INDEX" << endl;

return array[index];
}
private:
int* array;

};

Array* getArray();
void printArray(Array* array);

int main()
{
Array* array = getArray();

cout << "array in main" << endl;
printArray(array);

Array* array2 = getArray();

cout << "array in main" << endl;
printArray(array2);

cout << "array in main" << endl;
printArray(array);

delete array;
delete array2;

return 0;
}

Array* getArray()
{
int value = 0;
Array* array = new Array();

for (int i = 0; i < LENGTH; i++)
{
cout << "Enter an integer: ";
cin >> value;
array->setElement(i, value);
}

cout << "array in getArray()" << endl;
printArray(array);

return array;
}

void printArray(Array* array)
{
for (int i = 0; i < LENGTH; i++)
cout << array->getElement(i) << " ";

cout << endl;
cout << endl;
}

/*
Output:

Enter an integer: 1
Enter an integer: 2
Enter an integer: 3
Enter an integer: 4
Enter an integer: 5
array in getArray()
1 2 3 4 5

array in main
1 2 3 4 5

Enter an integer: 10
Enter an integer: 20
Enter an integer: 30
Enter an integer: 40
Enter an integer: 50
array in getArray()
10 20 30 40 50

array in main
10 20 30 40 50

array in main
1 2 3 4 5

Press any key to continue
*/
Reputation Points: 21
Solved Threads: 0
Newbie Poster
tlee is offline Offline
15 posts
since May 2004
May 11th, 2004
0

Re: Return Array from C++

C++ is not my native tongue, but I'll mention a couple of things and try to wade through the linter's output.

First, I believe you are trying to delete the objects twice. Once in main(), and then again in the destructor when the object goes out of scope.

[edit1]
C++ Syntax (Toggle Plain Text)
  1. if ( index < 0 || index >= LENGTH )
  2. cout << "INVALID INDEX" << endl;
  3. array[index] = value;
Here you may attempt to access out of array bounds.
[/edit1]

[edit2]
 Array()
{
	 array = new int(LENGTH);
}
I think you want the square brackets.
 Array()
{
	 array = new int[LENGTH];
}
[/edit2]

[edit3]
Lint:
Quote ...
[Info 1732] new in constructor for class 'Array' which has no assignment operator
[Info 1733] new in constructor for class 'Array' which has no copy constructor
I forget why, but it's right.
[/edit3]
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
May 11th, 2004
0

Re: Return Array from C++

Hi Dave,
The error is caused because of square bracket[] (I just wrote code in Java, so I used wrong () ). After changing to [], the program runs correctly.
Thanks for everything, Dave.

#include <iostream>
#define LENGTH 5
#define ERROR -1

using std::cout;
using std::cin;
using std::endl;

class Array
{
public:
Array()
{
array = new int[LENGTH];
}

~Array()
{
delete[] array;
}

void setElement(int index, int value)
{
if ( index >= 0 && index < LENGTH )
array[index] = value;
else
cout << "INVALID INDEX" << endl;
}

int getElement(int index) const
{
if ( index >= 0 && index < LENGTH )
return array[index];
else
{
cout << "INVALID INDEX" << endl;
return ERROR;
}


}
private:
int* array;

};

Array* getArray();
void printArray(Array* array);

int main()
{
Array* array = getArray();

cout << "array in main" << endl;
printArray(array);

Array* array2 = getArray();

cout << "array in main" << endl;
printArray(array2);

cout << "array in main" << endl;
printArray(array);

delete array2;
delete array;

return 0;
}

Array* getArray()
{
int value = 0;
Array* array = new Array();

for (int i = 0; i < LENGTH; i++)
{
cout << "Enter an integer: ";
cin >> value;
array->setElement(i, value);
}

cout << "array in getArray()" << endl;
printArray(array);

return array;
}

void printArray(Array* array)
{
for (int i = 0; i < LENGTH; i++)
cout << array->getElement(i) << " ";

cout << endl;
cout << endl;
}

/*
Output:

Enter an integer: 1
Enter an integer: 2
Enter an integer: 3
Enter an integer: 4
Enter an integer: 5
array in getArray()
1 2 3 4 5

array in main
1 2 3 4 5

Enter an integer: 10
Enter an integer: 20
Enter an integer: 30
Enter an integer: 40
Enter an integer: 50
array in getArray()
10 20 30 40 50

array in main
10 20 30 40 50

array in main
1 2 3 4 5

Press any key to continue
*/
Reputation Points: 21
Solved Threads: 0
Newbie Poster
tlee is offline Offline
15 posts
since May 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Popup Menu displays as very thin box
Next Thread in C++ Forum Timeline: Quicksort problems





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC