0

I am busy working on a simple concept to display pointer arrays and iterations of a for loop in C++

My compiler is not giving much away and when I run the program the console is saying the following and returning 3 "The application has requested the Runtime to terminate in an unusual way.

The crash occurs on this line:

      cout << i + 1 << " " << *(pArray + i) << endl;

but when I run this program ommiting either i + 1 or *(pArray + i) it runs without errors or crashing.

Is it illegal to try and output as I am trying to do above?

See below for the code:

#include <iostream>
#include <cstdlib>
using namespace std;

int main() {

int * pArray;
int SIZE;
int module;
pArray = new int[SIZE];  

cout <<"Enter the number of Assignments ";
cin >> SIZE;

cout <<"input assignment number " ;

    for (int i = 0; i < SIZE; i++)    
   {    
        cin >> module;    
        *(pArray + i) = module;

   }
   // Print array
   for (int i = 0; i < SIZE; i++)
   {
  cout << i + 1 << " " << *(pArray + i) << endl;
   }
   cout << endl;

   delete[] pArray;  // Deallocate array via delete[] operator

   return 0;
}

I am admittedly a little nervous to ask this question but I just need someone to explain why this is happening as I am battling to find any reference on this type of situation.

Thanks

3
  • 1
    It could be an idea to assign some value to SIZE, before using it. Commented Oct 10, 2015 at 12:38
  • 3
    Enable compiler warnings? Commented Oct 10, 2015 at 12:39
  • @KarolyHorvath Thanks, I'll do this. Appreciate feedback. Commented Oct 10, 2015 at 12:45

2 Answers 2

1

You use SIZE two lines before you initialize it.

Move

pArray = new int[SIZE];  

to after where you obtain the value of SIZE.

(Also: this would be so much easier with std::vector.)

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! I feel intensely dim. I feel despondent each time I ask a question on this forum as I am continuously marked down. I am very new C++ and occasionally as a student studying long distance I feel overwhelmed with these concepts and just appreciate a bit of interaction. Thanks again :)
1
int * pArray;
int SIZE;
int module;
pArray = new int[SIZE];

SIZE is not initialised yet, so, it would be some junk value. Initialise it before using it.

You could also Check for success/failure of new.

pArray = new(nothrow) int[SIZE];

if(pArray)
//logic

5 Comments

Thank you for your input. This makes sense. Not sure how I missed that.
It's very unlkely that new would fail here; and if it did the resulting exception would seem to be a perfectly good way to expose that.
@AlanStokes So then are you are saying that if I build an exception handler like above it would catch this is size hadn't been initialized?
No. I'm saying that @basav's second suggestion wouldn't improve things. If the new fails you will find out about it anyway. (It might fail given uninitialised input, but it might not.)
@Alan: agreed .in normal circumstances, not needed.i come from an embedded background and again, it really depends on the use case

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.