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.

4
  • Is there any reason you're not using std::vector or a similar container? Commented Mar 7, 2016 at 23:44
  • How many elements do you think the array will hold after init? Commented Mar 7, 2016 at 23:45
  • exercise instructs me to use array and not vectors, we do not know the size when initializing Commented Mar 7, 2016 at 23:46
  • Then you need to new int[numVal] (and I'm betting they've covered this in the book and/or class). Commented Mar 7, 2016 at 23:51

4 Answers 4

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;
}
Sign up to request clarification or add additional context in comments.

2 Comments

got ya, makes sense, thanks. This is correct answer to my issue.
+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.
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.

Comments

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);

Comments

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;
}

2 Comments

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.
Hey, thanks for the answer! Could you give a bit of explanation? It would help OP and future readers :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.