0

I tried to input two array numbers, A and B. But every time I input the last B number, besides it changed the value of B[i+n], it also replace the value of A[0] Here is the code:

#include <iostream>

    using namespace std;

    /*
     * 
     */
    int main(int argc, char** argv) {
        int a[] = {};
        int b[] = {};
        int t, i;

        cout << "Amount of numbers: ";
        cin >> t;

        for (i = 0; i < t; i++) {
            cout << "Enter number for A" << i+1 << ": ";
            cin >> a[i];
            cout << "Enter number for B" << i+1 << ": ";
            cin >> b[i];
        }

        for (i = 0; i < t; i++) {
            cout << a[i] << " ";
            cout << b[i] << " ";
        }
        return 0;
    }

For example

Amount of numbers: 2
Enter number for A1: 1
Enter number for B1: 2
Enter number for A2: 3
Enter number for A3: 4

The output supposed to be 1 2 3 4, but from the code I got 4 2 3 4 (the B[1] replace the value of A[0].

Can someone help me to fix this? Thanks

Jack
  • 131,802
  • 30
  • 241
  • 343
graydam
  • 209
  • 3
  • 10

4 Answers4

8

The problem is that you are using a two stack allocated arrays with unspecified size, which is 0 since you initialize them with { }.

These arrays are not dynamic, you can't add elements like you are doing. Their size must be specified a priori directly or through initialization and it's reserved on the stack at compile time, which means that if you try to access an element of a over its size, then you'll end up in b since they are stored next to its other.

Use two std::vector<int> instead so that you can do

std::vector<int> a;
..
a.resize(t);
..
cin >> a[i]
R Sahu
  • 204,454
  • 14
  • 159
  • 270
Jack
  • 131,802
  • 30
  • 241
  • 343
3

You're allocating arrays without a size.

Try std::vector if you want dynamically sized arrays.

doctagre
  • 43
  • 5
2

As others have mentioned, what is happening is that you are allocating empty arrays in the stack, and then accessing past the bounds of said array. This has far more dire consequences than you may at first realize.

The first of which is, as @Jack mentioned, that writing past a[0] will start writing in b, giving you duplicate and nonsensical values at the end of your program. Perhaps less obvious is the fact that you will also start overwriting t and i, since they are allocated in the stack after a and b. To illustrate my point, look at the output below:

Amount of numbers: 2
Enter number for A1: 1
Enter number for B1: 2
Enter number for A2: 3
Enter number for B2: 4
Enter number for A3: 5
Enter number for B3: 6
a[0] 0x7fffffffe2e0 -> 1
b[0] 0x7fffffffe2e4 -> 3
a[1] 0x7fffffffe2e4 -> 3
b[1] 0x7fffffffe2e8 -> 5
a[2] 0x7fffffffe2e8 -> 5
b[2] 0x7fffffffe2ec -> 2
a[3] 0x7fffffffe2ec -> 3
b[3] 0x7fffffffe2f0 -> 4197280
a[4] 0x7fffffffe2f0 -> 4197280
b[4] 0x7fffffffe2f4 -> 0
t 0x7fffffffe2e8 -> 5
i 0x7fffffffe2ec -> 5

You'll note that the program does not behave as expected, and you are prompted for A/B3, not just the expected 2. That is because t is overwritten when b[1] or a[2] are written to, as they all correspond the the same address, which of course makes the loop condition fail.

To allocate arrays in the stack, a size has to be provided at compile time. int a[10], etc. This is used when the size is known prior to execution. If the size unknown, as is your case, you can allocate an array in the heap with new[], as below.

cout << "Amount of numbers: ";
cin >> t;

int* a = new int[t];
int* b = new int[t];

Which will of course give you the expected results:

Amount of numbers: 2
Enter number for A1: 1
Enter number for B1: 2
Enter number for A2: 3
Enter number for B2: 4
a[0] 0x603010 -> 1
b[0] 0x603030 -> 2
a[1] 0x603014 -> 3
b[1] 0x603034 -> 4
t 0x7fffffffe2d8 -> 2
i 0x7fffffffe2dc -> 2

The recommended solution is, of course, to use an std container, namely std::vector, where the heap allocation is done for you, and you can simply write:

std::vector<int> a;
std::cin >> input;
a.push_back(input); 
Ramon
  • 1,169
  • 11
  • 25
  • Which compiler and options did you use, that accepted the invalid code? – Cheers and hth. - Alf Jun 28 '16 at 01:58
  • g++, don't recall the options - not at the office now. You are right in that it is not valid C++, but it seems to be allocating one element, apparently valid in C99? http://stackoverflow.com/a/15494762/5090527 I believe the answer is still useful though, but I will definitely add a note. – Ramon Jun 28 '16 at 02:17
1

The zero size arrays are not valid in C++, and this code doesn't compile with the two compilers I tried.

Instead of raw arrays you can use std::vector.

With std::vector you can use the push_back member function to add items at the end.

Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331