Realize that variables that are declared inside a function are completely local to that function -- they don't exist anywhere outside of it.
Also, your comments say that you're using reference parameters, but you're not.
So
getSize should be declared as
void getSize(int&);
.
Your
getSpace function has some problems as well:
1 2 3 4 5
|
void getSpace(int size)
{
int *strings;
strings = new int[size];
}
|
When this function is called, it allocates some memory and doesn't return the pointer to it. Since the
strings variable here disappears after the function ends, you end up getting a memory leak.
I suppose your
inputData function should really be
void inputData(int* strings, int size)
?
Similarly for your
printData function.
And for your
destroy function:
1 2 3 4
|
void destroy()
{
delete ptr;
}
|
Two problems with this:
1) There's no such thing as
ptr. You probably meant to pass it as a parameter to the function:
void destroy(int* ptr)
2) Since you're allocating an array (using
new[]
), it should be
delete[]
, not
delete
.
After fixing all of that, your
main function will need a few adjustments:
1 2 3 4 5 6 7 8 9 10 11 12
|
int main()
{
int size;
//int strings[size]; // Wrong -- non-constant array size, and 'size' is also uninitialized
// Instead, it should probably be
int* strings;
getSize(size);
strings = getSpace(size); // Assuming you change 'getSpace' to return the pointer
// ...
destroy(strings);
}
|
On a side note,
strings is a weird name for an array of
int
s. If you wanted actual strings, use
std::string instead of
int
.
Also, if you use
std::vector, you won't have to worry about allocating and freeing the memory yourself.