Dynamic memory problem?

Hello,
I'm starting to learn C++ and I decided to create a simple program using dynamic memory from the tutorial on this website just for practice.
But I don't know why my program shows an error sometimes and other times it runs fine. I would like an explanation and how I can fix it.
Here is my code:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>
#include <new>

using namespace std;

int main()
{
int i = 0;
int c = 0;
int * dynamicArray;  // Pointer
dynamicArray = new (nothrow)int[i];    


  if (dynamicArray == 0)
    cout << "Error: memory could not be allocated";
    
for(i=0; c==0 ; i++)
{

 cout << "Input value for Array #" << i << ": ";
 cin >> dynamicArray[i];
 
 cout << "Do you want to input more numbers? (Y=0, N=1) :" ;
 cin >> c;
 cout << endl;  
}

for(int n=0; n<i; n++)
{
cout << "Array #" << n << " = " << dynamicArray[n] << endl;
}


delete[] dynamicArray; // Deleting the data from memory

system("pause");
return 0;
}


Thank you!
1
2
3
4
5
6
7
8
9
int i = 0;
int c = 0;
int * dynamicArray;
dynamicArray = new (nothrow)int[i]; //new int[0]

for(i=0; c==0 ; i++)
{
 cout << "Input value for Array #" << i << ": ";
 cin >> dynamicArray[i]; //invalid access 

The array does not automagically resize.

> why my program shows an error sometimes and other times it runs fine.
undefined behaviour is undefined

Also, consider using meaningful names for the variables.
closed account (zb0S216C)
On line 11, you're allocating an array of "i" integers. However, prior to the allocation on line 11, "i" has a value of zero and does not change. Therefore, on line 11, you're allocating zero integers. Strangely enough, the compiler permits this, and "new" actually gives you a pointer, but that does not mean you actually allocated memory; thus, attempting to access the dynamically-allocated memory will generate a run-time exception.

Before allocating your memory, ensure that "i" is a value greater than zero.

Wazzak
Okay, but I still don't get it. What do I need to do to fix it?
I tried changing line 11 to:
dynamicArray = new (nothrow)int[i];
But I still have the same problem. The program runs sometimes and other times stops during running.
I also tried changing the loop so it starts at 1 instead of 0, but same result.


Did you set i to a value larger than zero?
Yes, I did.
I meant to write:
dynamicArray = new (nothrow)int[1];
As ne555 already told you:

The array does not automagically resize.


You're only defining your array to have memory for a single element. Yet, in your loop, you're allowing the user to enter more than one number, in which case it will attempt to store more than one element in your array. Which means you're attempting to write beyond the bounds of the array, leading to undefined behaviour (but, very likely, your program crashing).

You'd be much better off using vectors instead of C-style arrays.
In which case, you only have enough space for one element.
As I said before, arrays don't automagically resize.

1
2
3
4
std::vector<int> array;
int value;
while( std::cin>>value )
   array.push_back( value );
Alright!
Now I get it.
Thanks!
Topic archived. No new replies allowed.