heapInsert Array Corruption error?

I am trying to insert a number into a built heap using an array. However when i add a number and rebuild the array then print i receive a - "Stack around the variable 'A' was corrupted" error? How would i fix this?



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
40
41
42
43
int main()
{

	int A[] = {4, 1, 3, 2, 16, 9, 10, 14, 8, 7};
	int Asize = sizeof A/sizeof(int);

heapInsert(A, Asize, 20);
	cout << endl;
	cout << endl;

	heapInsert(A, Asize, 17);
	cout << endl;
	cout << endl;
}

bool heapInsert(int A[], int &size, int Item)
{

    cout << "After inserting the number " << Item << " into the heap A:" << endl;

    
    int Asize = sizeof A/sizeof(int);
    if(Asize == size)
    return false;
	

    A[size] = Item;  
    build_heap(A, size);  

    size++;  
    return true;  



    int counter;

    for (counter = 0; counter < size; counter++)
    {
		cout << A[counter] << " ";
    }


}
Last edited on
At least from what I see it looks as if your heapInsert() function will always return false. You essentially store the size of A in main into Asize then pass it to heapInsert which by reference there you call it "size". Then you essentially defined Asize again as the same value of "size" and you wrote and if statement to check and see if they are equal, which at that point, they are equal because nothing ever changed. You would have to insert the "item" into the index of your choosing to have a different Asize value. This use of array in your program is very awkward, you should consider looking into using vectors instead.

And also a tip for your for-loop at the bottom where you define a counter. You can rewrite it like this to look better and use less lines of code.

 
for (int i = 0; i < size; i++) { cout << A[i] << " "; }
Line 22 is useless and does not do what you think. It will probably return either 1 or 2 as it is equivalent to int Asize = sizeof (int*) / sizeof (int)

Also on line 7 you pass size of array to function, on line 30 you increment size and try to access out of boundaries as a result on line 37-39
As far as I know you can't just "extend" an array by writing past its last item!
You are essentially writing into memory that doesn't belong to your array, possibly overwriting other parts of your programs memory.
Apparently there is no boundary checking, because its old C array style or something.

http://stackoverflow.com/questions/1239938/c-accesses-an-array-out-of-bounds-gives-no-error-why

Just because it doesn't give you an error doesn't mean it's working correctly.
Also line 35 and onwards will never execute because your function already returns on line 31, IF it would even go that far, which it won't, because it will always return on line 24, because of what MiiNiPaa mentioned.
Inside of your function the size of the array is not known, its essentially just a variable that holds a number which represents the adress in memory where the FIRST element of your array resides (a so called pointer, in this case to an int, so an int*), and sizeof(arr) gives you only the size of that pointer, which should always be 4 bytes if your program runs in 32bit mode, or 8 if 64 bit mode.
Thats why you always have to pass in the size beforehand, because inside of the function its not possible to determine the size anymore.
Topic archived. No new replies allowed.