1

I am having an issue with outputting an array. When I output each element without a for loop, the program runs fine. When I try to output with a for loop, the program crashes the first time I set an element in the array. I have marked the line where the program crashes when I uncomment out the for loop. My sort seems to work fine and the program is crashing way before that, so I'm pretty sure that isn't the issue. Any ideas why the 2nd for loop would crash the program at the specified line?

int main()
{
   int* Array;
   int j = 5;
   for(int i=0; i<5; i++)
   {
       Array[i] = j; //Crashes here with 2nd for loop uncommented
       cout << Array[i] << endl;
       j--;
   }
    Array = insertion_sort(Array);

    cout << Array[0] << endl;
    cout << Array[1] << endl;
    cout << Array[2] << endl;
    cout << Array[3] << endl;
    cout << Array[4] << endl;



   /*for(int k=0; k <5; k++)
   {
      cout << Array[k] << endl;
   }*/


}
5
  • 2
    Using an uninitialized pointer never works well. That's why the new keyword exists. Commented Sep 19, 2013 at 21:52
  • well for this, it's better to just declare as an array instead of a pointer -- as in, int Array[5]; Commented Sep 19, 2013 at 21:53
  • @thang This line Array = insertion_sort(Array); will fail to compile if Array really was an array. Commented Sep 19, 2013 at 21:55
  • @jon, you're right, but that line, while syntactically correct, is probably riddled with bugs. How does it know how big the array is? Commented Sep 19, 2013 at 22:06
  • I have since modified that function to send the size. The reason I didn't in the first place is because I was using a fixed size for testing reasons. Sorry for the confusion. Commented Sep 19, 2013 at 22:13

2 Answers 2

3

You are accessing the pointer before initializing it. You should change

int* Array;

to

int* Array = new int[5]; // There are 5 ints in my array

and make sure to

delete[] Array;

at the end, or even better:

int Array[5]; // This way you don't need the new keyword, so you won't need to delete[] later
Sign up to request clarification or add additional context in comments.

2 Comments

Yeah. that fixed it. Just seemed weird that it worked without the 2nd for loop as I had it. Stupid error on my part, but weird bug to try to fix if you don't notice that, like me. Thanks.
Yes, errors like this can be hard to spot because undefined behavior sometimes looks like it's working, even when it's not!
1

Right now, your array is not instanciated. It's just a pointer. You need to choose how large of an array you want before you can start writing to it.

int* Array = new int[5];
int j = 5;
for(int i=0; i<5; i++)
{
    Array[i] = j; //Crashes here with 2nd for loop uncommented
    cout << Array[i] << endl;
    j--;
}

4 Comments

Why does the program run fine without the 2nd for loop then?
@JoeBradish Because sometimes bugged program run correctly. Your program has Undefined Behaviour which means exactly what it says.
Okay, I fixed the issue. I was very confused why the for loop was making it crash at another for loop. Thanks for the help.
If this helped you come to the solution, please mark it as answered, so others with this problem will know where to look.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.