0

I'm supposed to dynamically allocate an array of integers. The function should accept an integet argument indicating teh number of elements to allocate. The function should return a pointer to the array.

This is what i got so far.

#include <iostream>
using namespace std;

//Function prototype
int *arrAllocator(int);
int numElements;
int num;
int count; //Loop counter

int main()
{
	int *values; //To point to the numbers

	//Get an array of number chosen by user. 
	values = arrAllocator(numElements);

	cout << "\nEnter an array size: " << endl;
	cin >> numElements;

	//Get number values from user.
	cout << "Enter the value of the elements below.\n";

	for (count = 0; count < numElements; count++)
	{
	cout << "Value #" << (count + 1) << ": ";
	cin >> values[count];
	}

	//Display the numbers.
	for (int count = 0; count < numElements; count++)
		cout << values[count] << endl;

	//Free the memory.
	delete [] values;
	values = 0;
	return 0;
}

//The function returns a pointer to an array.

int *arrAllocator(int numElements)
{
	int *array;

	//Invalid if zero or negative.
	if (numElements <= 0)
		return NULL;

	//Dynamically allocate the array.
	array = new int[num];
	
	//Return a pointer to the array
	return array;
}

The program will start to run normal but after the user inputs the first number in the array it stops for errors. I know that a my mistakes are more than likely in returning the pointer to the array. But i can't figure it out i even wonder if maybe i linked some of the names incorrectly. Could some one please assist in telling me where im messing up, or did i go about writting the program incorrectly. Thank you in advance.

4
Contributors
6
Replies
7
Views
8 Years
Discussion Span
Last Post by pltndragon
0

in the future, please use [ code] [/ code] tags, as it makes your code so much more readable. Formatted here

#include <iostream>
using namespace std;

//Function prototype
int *arrAllocator(int);
int numElements;
int num;
int count; //Loop counter

int main()
{
int *values; //To point to the numbers

//Get an array of number chosen by user.
values = arrAllocator(numElements);

cout << "\nEnter an array size: " << endl;
cin >> numElements;

//Get number values from user.
cout << "Enter the value of the elements below.\n";

for (count = 0; count < numElements; count++)
{
cout << "Value #" << (count + 1) << ": ";
cin >> values[count];
}

//Display the numbers.
for (int count = 0; count < numElements; count++)
cout << values[count] << endl;

//Free the memory.
delete [] values;
values = 0;
return 0;
}

//The function returns a pointer to an array.

int *arrAllocator(int numElements)
{
int *array;

//Invalid if zero or negative.
if (numElements <= 0)
return NULL;

//Dynamically allocate the array.
array = new int[num];

//Return a pointer to the array
return array;
}
0

Now, as to your actual question:

The first error I see is that you use numElements before it gets set.
move values = arrAllocator(numElements); to after cin >> numElements; , and that problem will be solved.

After just a quick reading of the code, that is the only problem I can find.

0

I fixed the and moved the values = arrayAllocator(numElements); to after the cin >> numElements;

That did help in making the program ask for the rest of the values, but at the very end after it displays the array i still get an error. that says crt detected that the application wrote to memory after end of heap buffer.

0

It's better to wrap code tags in noparse tags to avoid putting spaces here and there just to make it look like a code tag :P

Edited by happygeek: fixed formatting

0

pltndragon,
Some suggestions for you:
numElements is declared at file context (global) and hence it's value is 0.

values = arrAllocator(numElements);   //  arrAllocatator(0) ?

First, read value into numElements and then go for allocation.

cout << "\nEnter an array size: " << endl;
 cin >> numElements;
 values = arrAllocator(numElements);

PS: Avoid global declaration of variables if possible. numElements is global variable and you are passing as an argument. It's an improper way.

In arrAllocator() Function, num is global variable with 0 value.

//Dynamically allocate the array.
   array = new int[num]; // <--- new int[numElements]

There are some problems with count variable(s) in main().

::count   //   external count
  for(int count= ..) // local - block var.

Finally, your code should be:

#include <iostream>
using namespace std;

//Function prototype
int *arrAllocator(int);

int main()
{
   int *values; //To point to the numbers
   int numElements;
   int count; //Loop counter

   cout << "\nEnter an array size: " << endl;
   cin >> numElements;
   values = arrAllocator(numElements);
   if(values==NULL) 
      return 0;

   //Get number values from user.
   cout << "Enter the value of the elements below.\n";
    for (count = 0; count < numElements; count++)
    {
      cout << "Value #" << (count + 1) << ": ";
       cin >> values[count];
      }

   //Display the numbers.
   for (count = 0; count < numElements; count++)
     cout << values[count] << endl;

   //Free the memory.
   delete [] values;
return 0;
}

//The function returns a pointer to an array.
int *arrAllocator(int numElements)
{
int *array;
   //Invalid if zero or negative.
   if (numElements <= 0)
       return NULL;
   //Dynamically allocate the array.
   array = new int[numElements];

   //Return a pointer to the array
  return array;
}
0

Thank you, for your help and letting me know what i was doing wrong and why it was wrong. I have defently learned a bit more from your explanations.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.