| | |
Return Array from C++
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: May 2004
Posts: 15
Reputation:
Solved Threads: 0
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.
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.
•
•
•
•
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.
C++ Syntax (Toggle Plain Text)
#include <iostream> int *foo(int *array) { int *start = array; while ( *array ) { std::cout << *array++ << ' '; } std::cout << std::endl; return start; } int *bar(int *array) { int *start = array; while ( *array ) { *array++ += 1; } return start; } int main() { int myarray[] = {1,2,3,4,5,0}; foo(bar(foo(bar(foo(myarray))))); return 0; } /* my output 1 2 3 4 5 2 3 4 5 6 3 4 5 6 7 */
•
•
Join Date: May 2004
Posts: 15
Reputation:
Solved Threads: 0
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
Thanks in advance, by the way, thanks Dava
•
•
Join Date: May 2004
Posts: 15
Reputation:
Solved Threads: 0
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
*/
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
*/
One way:
int* getArray()
{
static int array[LENGHT] = {1, 2, 3, 4, 5};
cout << "array in getArray()" << endl;
printArray(array);
return array;
}•
•
Join Date: May 2004
Posts: 15
Reputation:
Solved Threads: 0
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
*/
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
*/
•
•
•
•
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.
#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.
•
•
Join Date: May 2004
Posts: 15
Reputation:
Solved Threads: 0
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
*/
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
*/
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] Here you may attempt to access out of array bounds.
[/edit1]
[edit2] I think you want the square brackets. [/edit2]
[edit3]
Lint: I forget why, but it's right.
[/edit3]
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)
if ( index < 0 || index >= LENGTH ) cout << "INVALID INDEX" << endl; array[index] = value;
[/edit1]
[edit2]
Array()
{
array = new int(LENGTH);
} Array()
{
array = new int[LENGTH];
}[edit3]
Lint:
•
•
•
•
[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
[/edit3]
•
•
Join Date: May 2004
Posts: 15
Reputation:
Solved Threads: 0
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
*/
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
*/
![]() |
Similar Threads
Other Threads in the C++ Forum
- Previous Thread: Popup Menu displays as very thin box
- Next Thread: Quicksort problems
| Thread Tools | Search this Thread |
api array based beginner binary c++ c/c++ calculator char char* class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news node numbertoword output parameter pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






