I was programming a quick-sort program, and even before I coded the quick-sort part, my ARRAY, toSort which had been allocated for 6 values, got 8 values when I ran through the findLast part of my program. Here's the code
Value at location 0 not NULL, moving to next location.
toSort[0] = 41
Value at location 1 not NULL, moving to next location.
toSort[1] = 18467
Value at location 2 not NULL, moving to next location.
toSort[2] = 6334
Value at location 3 not NULL, moving to next location.
toSort[3] = 26500
Value at location 4 not NULL, moving to next location.
toSort[4] = 19169
Value at location 5 not NULL, moving to next location.
toSort[5] = 15724
Value at location 6 not NULL, moving to next location.
toSort[6] = 4201568
Value at location 7 not NULL, moving to next location.
toSort[7] = 4736400
Value at location 8 not NULL, moving to next location.
toSort[8] = 2130567168
Last Value found for ARRAY toSort, value #8 is the final value.
ARRAY toSort at memory location 0x28fef8
RUN SUCCESSFUL (total time: 41ms)
as you can see, the length I allocated for was six and what was output was eight. could you tell me what happened how to fix it, should I use malloc or realloc to allocate this in c-style?
It's a code error. You are assigning an array of size 6. All that means is that an area of memory the right size for 6 objects is being reserved.
toSort[3] means *(toSort+3), which means take the pointer called toSort, move along in memory an amount equal to the size of three objects, and read that. It contains no checking whatsoever that three places along is part of the array.
So toSort[8] means *(toSort+8) which means take the pointer called toSort (which points to the first element of your array), move along eight object sizes in memory, and read that. It does not check at all what size array you set. It's just pointer arithmetic, and C++ trusts you to know what you're doing.
If you are lucky, when you make this mistake your program crashes with a segFault. If you're unlucky, it does not. You've been unlucky.
If what you're saying is true, how do I get the last value of the array I created without crashing the system. Is there a built in feature for this? or will I have to crack out assembly and create my own way of going around this? Also, why didn't the program keep going or create an infinite array?
Also, why didn't the program keep going or create an infinite array?
Because this is C++. You are trusted to know what you are doing, and there is a principal in C++ that you shouldn't have to pay for what you don't use. Why should everyone else have to pay for array checking and auto-expansion that they don't need? Additionally, does that sound sensible to you? The program blindly creating an infinite array and trashing memory left, right and centre?
If what you're saying is true, how do I get the last value of the array I created without crashing the system.
In your case, last[5] gives you the last element of the array. You could generalise things a little as follows:
1 2
constint sizeOfArray = 6;
int toSort[sizeOfArray];
Then, the last element would be toSort[sizeOfArray - 1];
1 2
delete &toSort;
delete &last;
These deletes are nonsensical. Completely wrong. As a rule of thumb, you use a delete for something that you previously used a new for.
OP, rather than using delete &toSort; // or whatever else just set them = NULL like this &toSort = NULL; As Moschops said delete should only be used when new is used.
To expand on that: new is used to allocate memory and create an object, it returns a POINTER to a NEW instance of whatever class you pass to new. That being said, you should only ever call delete on a POINTER because new returns a POINTER. I hope that helps you understand the use of new and delete.
1 2 3 4 5 6 7 8 9
MyBaseClass * pBaseClass = new MyBaseClass;
// now we have a pointer to a newly allocated MyBaseClass object.
// this object has been allocated on the heap (not the stack)
delete pBaseClass;
pBaseClass = NULL;
// memory has been freed from the heap and is now ready to be allocated to something else
// also the pointer is now NULL, this is good practice