Error when trying to print out array in a loop using cout

-2
#include <cstdio>
#include <iostream>

int main (){
    int n;
    std::cin>>n;
    int*a;
    for (int i=0;i<n;i++){
        std::cin>>a[i];
    }
    for(int i=0;i<n;i++){
        std::cout<<a[i];
    }

    return 0;
}

I just started working on a problem and I wanted to check if I knew how to read and array and make a sample output array. When I include the second loop program crashes as soon as I enter n and the first number With the following message

3 1

Process returned -1073741819 (0xC0000005) execution time : 4.943 s Press any key to continue.

c++
arrays
cout
asked on Stack Overflow Oct 11, 2016 by maltkat • edited Jun 16, 2020 by E_net4 is not cute

6 Answers

1

int *a; is a pointer to an integer, it is just a pointer to some memory, it has no memory allocated on its own. Since you are dereferencing this pointer a[i] without setting it up first, your compiler should even give you some warning telling that you are using a variable that has not been initialized.

0xC0000005 error code in Windows means access violation. In this case, you are trying to write to some memory which you don't have access to.

You need to allocate memory before you can read or write to it.

If you know beforehand how many entries you will have, you can do static memory allocation, if you don't, then you need to do dynamic memory allocation.

For instance, if you knew that you would need only 20 entries at max, you could easily swap int *a; for int a[20];.

But since you are only getting to know how many entries there will be when the program runs, then you need to go for dynamic memory allocation: int *a = new int[n];.

So your code should be

#include <cstdio>
#include <iostream>

int main (){
    int n;
    std::cin>>n;
    int *a = new int[n];
    for (int i=0;i<n;i++){
        std::cin>>a[i];
    }
    for(int i=0;i<n;i++){
        std::cout<<a[i];
    }
    delete[] a; // Release allocated memory.
    return 0;
}
answered on Stack Overflow Oct 11, 2016 by Rafael Werlang • edited Jun 16, 2020 by Rafael Werlang
0

You need to allocate memory for a, otherwise the behaviour of the program is undefined. Writing

int* a = new int[n];

would to it, followed by delete[] a; when you're all done (put this just before return 0;).

Alternatively, use a std::vector<int> a(n); and all the memory allocation will be taken care of for you.

answered on Stack Overflow Oct 11, 2016 by Bathsheba
0

int* is a pointer to int, not an array.
To create an array of int, example: int a[100]; - where 100 is the size

Moreover, you should use a std::vector<int> instead:

vector<int> vec;
for (int i = 0; i != n; ++i) {
    int temp;
    cin >> temp;
    vec.emplace_back(temp);
}
answered on Stack Overflow Oct 11, 2016 by Andreas DM • edited Oct 11, 2016 by Andreas DM
0

Try : int a[20]; rather than int *a;

answered on Stack Overflow Oct 11, 2016 by manianis
0

Your a doesn't point to any valid memory, resulting in undefined behaviour.

What you need is an std::vector :

#include <vector>
int n;
std::cin>>n;
std::vector<int> numbers;
for (int i=0;i<n;i++){
    int val;
    std::cin>>val;
    numbers.push_back(val);
}
for(int i=0;i<n /* or numbers.size()*/ ;i++){
    std::cout<< numbers[i];
}

This takes care of dynamic allocation for you so that you don't have to do the dirty stuff yourself.

answered on Stack Overflow Oct 11, 2016 by Sombrero Chicken
0

There are a few issues with your code. Primarily, you request the number 'n' then need to allocate enough space to store that many integers.

The best way to do that is to use vector. You can create this with:

std::vector< int > numbers( n );

You can also create it allocating the memory but waiting until you have data:

std::vector< int > numbers;
numbers.reserve( n );

You also should probably validate your input, for example your input stream (cin) will be invalid if the user enters something that is not an integer, and the original 'n' should be positive if you are going to try to create a vector of that size, and you may need to set a limit or you will suffer a bad_alloc. If you do not mind suffering a bad_alloc you should catch that exception and print an error such as "There is insufficient space to allocate as many numbers".

Of course if you enter a high number like 10 million, you may find the compiler is able to allocate that many but your user will get bored in your loop when you ask him to enter integers 10 million times.

You do not need <cstdio> as a header. You will need <vector> and <iostream>.

answered on Stack Overflow Oct 11, 2016 by CashCow

User contributions licensed under CC BY-SA 3.0