0

I am beginner in C++ and was trying to insert data into an array through the for loop, however, it throws Stack around the variable 'numArray' was corrupted.

My code:

//Initializing and declairing variables
int numVal = 0;
int numArray[] = {0};

cout << "Enter the number of values to average: ";
cin >> numVal;

//Loop through to accept all values and allocate them to an array
for (int i = 0; i < numVal; i++) {
    cout << "[" << i << "] = ";
    cin >> numArray[i];
}

What's wrong with my code?

Edit: I must use array and not vectors.

Shepard
  • 1,111
  • 5
  • 16
  • 32

5 Answers5

3
int numArray[] = {0}

On this line, you specify that numArray can hold one integer. Later on, when you try to enter anything more than one integer, you get undefined behavior. Think of this as like a promise. This line is you promising "Give me a memory location, and I promise I will not read or write anything past the first n addresses past that." When you break this promise, anything theoretically could happen.

To fix it, you need to allocate more memory for this array, and check to make sure you never define something past that number. Or, the simpler and more c++ way to do it, is to use an array that will automatically do that for you, such as a vector.

If you really must use an array, make sure you have some way of tracking when that many elements have been entered. For example:

const int SIZE = 10;
int numArray[SIZE];

...

std::cout << "Enter the number of values to average (less than " << SIZE << ")" << std::endl;
std::cin >> numVal;
if (numVal >= SIZE)
{
    std::cout << "Please enter a number smaller than " << SIZE << std::endl;
}
Community
  • 1
  • 1
DJMcMayhem
  • 7,285
  • 4
  • 41
  • 61
  • got ya, makes sense, thanks. This is correct answer to my issue. – Shepard Mar 07 '16 at 23:50
  • 1
    +1, however it should be noted that the array is uninitialized. Use `memset` or `int numArray[SIZE]={0}` to initialize to all 0, otherwise the contents of the array can/will be random. – LINEMAN78 Mar 07 '16 at 23:55
2

Assuming you want to do everything dynamically with arrays:

#include <iostream>
using namespace std;

int main() {
  //Initializing and declairing variables                                                                                       
  int numVal = 0;
  int *numArray;

  cout << "Enter the number of values to average: ";
  cin >> numVal;

  numArray = new int[numVal];

  //Loop through to accept all values and allocate them to an array                                                             
  for (int i = 0; i < numVal; i++) {
    cout << "[" << i << "] = ";
    cin >> numArray[i];
  }

  delete[] numArray;

  return 0;
}

ALWAYS remember to free heap memory by calling delete. And for good measure, always test your program using valgrind.

0

int numArray[] = {0}; means to make an array of size 1. C-style arrays must have their size specified in the declaration (either explicitly, or deduced from the number of initializers as you have done).

They cannot be grown or resized later. When you do cin >> numArray[1] you write out of bounds of the array, causing stack corruption.

If you want a resizeable array, then in C++ that is called vector. Your code would be:

vector<int> numArray;

// ... in loop
int temp = 0;
cin >> temp;
numArray.push_back(temp);
M.M
  • 138,810
  • 21
  • 208
  • 365
0

1 - import vector 2- Create an array 3- Added with a loop 4- Print the elements using the loop

#include <iostream>
#include <vector>    // import vector 
using namespace std;

int main()
{
    const int size = 100;
    std::vector< int > arr; // Create an array 
for(int i=0;i< size ;i++)
{

    arr.push_back(i); // Added with a loop

}
for(int i=0;i< size ;i++) 
{
    cout<<arr[i]<<"  "; // Print the elements using the loop
}

    return 0;
}
  • 1
    Please don't post only code as answer, but also provide an explanation what your code does and how it solves the problem of the question. Answers with an explanation are usually more helpful and of better quality, and are more likely to attract upvotes. – FlyingFoX Oct 17 '20 at 22:11
  • Hey, thanks for the answer! Could you give a bit of explanation? It would help OP and future readers :) – xShirase Oct 17 '20 at 23:46
-1

You're writing C++, not C; use C++ container classes.

std::vector<int> numArray;
int numVal = 0;

cin >> numVal;

numArray.resize(numVal); // add numVal entries to the vector

for (int i = 0; i < numArray.size(); ++)
{
    cout << "[" << i << "]" = ";
    cin >> numArray[i];
}
Colin Basnett
  • 4,052
  • 2
  • 30
  • 49