I’m trying to loop through a pointer array of objects and it works just fine until it reaches the input variable. Somehow it raises the size variable and keeps doing that until the application crashes. What is causing this strange behavior?
Array::Array(int in)
{
size = in;
Heltal *h[sizeof(size)];
for(int i = 0; i < size; i++){
h[i] = new Heltal(Array::Random(1, 100));
}
}
Replace this line:
With this:
sizeof(size)is a compile-time constant, and evaluates to the length, in bytes, of the typesizeis declared to be of. This is likely to be a value of 4, which means thathwill always be 4 elements long. You are likely overflowing the array as a result.The vector constructor will instead allocate a dynamic number of elements on the heap.