I'm still somewhat new to C++, and I'm attempting to write an array from an users input inside an object. I keep getting the error "CRT detected that the application wrote to memory after end of heap buffer." From my understanding that means i'm trying to call a value out of the array bounds, but If I make the variable a static number outside the class and refer to that value when creating the array I receive no error so I'm a bit stumped.
does it fail if you move the 'new' statement into your constructor?
what is the value of arraysize before the constructor runs, when you call the 'new' statement?
because you do 2 different constructors here, that each affect the 'new' statement, you should put the 'new' into the constructors (both) and remove new (you can init to nullptr) from the class body area.
arrayStuff() { arraySize = 5; objArray = new int [arraySize];}
arrayStuff(int X) { arraySize = X; objArray = new int [arraySize];}
After having my diagnostic tools not fail me, its show its triggering a breakpoint at line 72 which is where I delete the array. I'm not sure what I am doing wrong there
I made just a few small changes. I moved the news to the constructor, and set ptr in the constructor as well, and offered an alternate to pointer math, ... compare vs what you posted... this one works, as far as I can tell... I threw in a couple of 'suggestions' that I didn't test on your destructor as well. Cleanup like this helps prevent the user of the class from making mistakes -- its not necessary, but its better. The user would really have to do something extra stupid to find a way to use the class after it was destroyed, so this is just extra paranoia for your class, but these ideas are useful with pointers 'in general' --- if you null after delete, if you accidentally delete again its safe. If you zero out the size, it is correct if accessed, etc. Since the object is gone after destructor, not a big deal, but in other code if you had the same ideas, but not protected by the destructor, it would save you debugging woes.
int arraySize;
int * objArray = newint [arraySize];
The original problem is that when new is executed to initialise objArray, arraySize is not initialised and so has an unknown value - possibly very large or 0. Member variable initialisations (if specified) are performed before the constructor is called.
In the constructor you are initialising arraySize, but after the value has been used to initialise objArray. If you initialise a variable in the constructor, then any other variable that is based upon that also needs to be initialised as well - even if already initialised in the definition.
is this some kind of homework?
If not you should forget about this old C style pointers.
I would be happy to show how these tasks should be done in modern C++ (i.e. C++17)
To elaborate on seeplus's explanation, when you call the constructor of a class, first the class's data members are constructed (in the order that they appear in the declaration), then the body of the class constructor is called. So when you have
1 2 3 4 5 6 7 8
class arrayStuff {
private:
int arraySize;
int * objArray = newint [arraySize];
int * ptr = objArray + 0;
public:
arrayStuff() { arraySize = 5; }
...
First it constructs arraySize because it's the first member of class arrayStuff. There's no default constructor for int, so arraySize gets whatever happens to be in the memory of the object.
Next it constructs objArray using newint[arraySize];. Since arraySize contains random junk, the size of the array constructed could be anything.
Next it constructs int *ptr using objArray+0;
Finally, it enters the body of the arrayStuff constructor, which sets arraySize to 5.