Reputation: 63
I got question on pointer. Here is my question :
Write a function
int* read_data(int& size)
that reads data fromcin
until the user terminates input by entering Q. The function should set the size reference parameter to the number of numeric inputs. Return a pointer to an array on the heap. That array should have exactly size elements. Of course, you won’t know at the outset how many elements the user will enter. Start with an array of 10 elements, and double the size whenever the array fills up. At the end, allocate an array of the correct size and copy all the inputs into it. Be sure to delete any intermediate arrays.
And here is my code :
int* read_data(int& size);
int main()
{
int size ;
int* account_pointer = read_data(size);
int* account_array = new int[size];
int* bigger_array = new int[2 * size];
for (int i = 0; i < size; i++)
{
bigger_array[i] = account_array[i];
}
delete[] account_array;
account_array = bigger_array;
size = 2 * size;
system("PAUSE");
return 0;
}
int* read_data(int& size)
{
int input;
cout << "Enter integer, Q to quit : " ;
while(cin >> input)
{
size++;
}
return &size;
}
However, I got an error message when I insert non-digit. The error message is Debug Error! Invalid allocation size : 4294967295 bytes
. Am I declaring the size variable in main method wrongly? Or my whole code are wrongly coded?
Upvotes: 0
Views: 2446
Reputation: 6039
So... at the risk of doing someone's school project for them (ahem) this is what I would do:
#include "stdafx.h"
#include <iostream>
int* read_data(int& size);
int _tmain(int argc, _TCHAR* argv[])
{
int size = 0;
int* result = read_data(size);
int* end = new int[size];
for(int i=0;i<size;i++)
end[i] = result[i];
for(int i=0;i<size;i++)
std::cout<<end[i];
delete []end;
delete []result;
return 0;
}
int* read_data(int& size)
{
int input;
int arraySize = 10;
int *arrInt = new int[arraySize];
std::cout << "Enter integer, Q to quit : " ;
while(std::cin >> input)
{
arrInt[size++] = input;
if(size >= arraySize) //time to resize
{
arraySize *= 2; //double size
int* arrInt2 = new int [arraySize];
for(int i=0;i<size;i++)
{
arrInt2[i] = arrInt[i];
}
delete [] arrInt;
arrInt = arrInt2;
}
}
return arrInt;
}
Upvotes: 1
Reputation: 11
Start with an array of 10
size is declared but uninitialized
double the size whenever the array fills up
This^ step should be in read_data(..), not in main(), and also should be automatic.
user terminates input by entering Q
In this code snippet you provided is no method of testing that possible input.
Return a pointer to an array on the heap
An integer address is by no means array on heap, moreover, although size is uninitialized at first, after the function returns it it returns the ADDRESS of that variable, so huge numbers occur later during execution and so program attempts to allocate 4 billion numbers...
Last issue I can see a.t.m. is that you discard any user input, not feeding it into any of the arrays...
so my tips:
move the declaration of size and any variables you need including arrays INSIDE read_data(...);
actually parse the input;
make read_data(...) actually return what it should.
Fix these and you should be all set ^.^
Upvotes: 1
Reputation: 793
In main()
you need to initialize size to 0. Without initialization, its value is undefined.
Upvotes: 6