U2y
U2y

Reputation: 13

Why is my 'length' changed by my computer?

My length just changed for no reason.

Here is the code:

#include<stdio.h>
#include<windows.h>

void HeapAdjust(int *a,int s,int length);
void swap(int *a,int m,int n);

int main(){
    int a[] = {99,19,2,98,103,3,9,20,78,201,88,9,23,46,77,59,67,69,75,38};

    /* 
      bug here 👇
    */
    int length = sizeof(a)/sizeof(a[0]);

    for(int i=length/2;i>=0;i--)
    {
        HeapAdjust(a,i,length);
    }

    for(int i=length;i>0;i--)
    {
        swap(a,0,i-1);
        HeapAdjust(a,0,i-2);
    }

    /* Show the result of Heap_sort */
    for(int i=0;i<length;i++)
    {
        printf("%d ",a[i]);
    }
    system("pause");
}

void HeapAdjust(int *a,int s,int length)
{
    int temp = a[s];
    for(int j=s*2;j<=length;j*=2)
    {
        if(j<length&&a[j+1]>a[j])
            j++;
        if(temp>=a[j])
            break;
        a[s] = a[j];
        s = j;
    }
    a[s] = temp;
}

void swap(int *a,int m,int n)
{
    int temp = a[m];
    a[m] = a[n];
    a[n] = temp;
}

I don't have any pointer or reference of the length. I found that it'll change when I don't get it a const. The length will strangely change to 3, but it should be 10. I don't get it.

enter image description here

Upvotes: 0

Views: 94

Answers (2)

U2y
U2y

Reputation: 13

Just found the bug,its the function HeapAdjust.

void HeapAdjust(int *a,int s,int length)
{
    int temp = a[s];
    for(int j=s*2;j<=length;j*=2)
    {
        if(j<length&&a[j+1]>a[j])
            j++;
        if(temp>=a[j])
            break;
        a[s] = a[j];
        s = j;
    }
    a[s] = temp;
}
when i is 5,which means s is 5 here,it will finally make s be 20.yeah,just above length,and I believe it is the result of bug.

just make >= be >,length went well.

I'm new at programming,if there is any other advice for coding,plz just do it.

Upvotes: 0

sidcoder
sidcoder

Reputation: 460

Typical error in C coding: You write beyond the size of array (e.g. on element below 0 or greater equal array size). Proof: int a[21] will work. But then define: int length = 20;

Here, I found this:

for(int i=length;i>0;i--)
{
    swap(a,0,i-1);
    HeapAdjust(a,0,i-2);
}

which means i can be 1, and thus last argument in HeapAdjust will be -1.

Some additional "printf" commands show that array elements beyond 19 are accessed, which is wrong anyway - and which can lead to some undefined behaviour.

void HeapAdjust(int *a,int s,int length)
{
    int temp = a[s];
    for(int j=s*2;j<=length;j*=2)
    {
        printf("redfined a %i %i\n",j+1,j);
        if(j<length && a[j+1]>a[j]){
            j++;}
            printf("redfined b %i %i\n",s,j);
        if(temp>=a[j])
            break;
            
        a[s] = a[j];
        s = j;
    }
    printf("redfined c %i\n",s);
    a[s] = temp;
}

void swap(int *a,int m,int n)
{
    printf("redfined d %i\n",m);
    printf("redfined e %i\n",n);  
    int temp = a[m];
    a[m] = a[n];
    a[n] = temp;
}


./a.out
redfined a 21 20
redfined b 10 20
redfined c 10
redfined a 19 18
redfined b 9 18
redfined c 9
redfined a 17 16
redfined b 8 17
redfined c 8
redfined a 15 14
redfined b 7 14
redfined c 14
redfined a 13 12
redfined b 6 13
redfined c 13
redfined a 11 10
redfined b 5 10
redfined a 21 20
redfined b 10 20

Suggestion: Rewrite the code and check to access elements only between 0 and 19 (for this example of array length = 20). Personal suggestion: Try to make the code clearer for human reading for correct understanding. It does not have to be as short as possible (to be correct).

Upvotes: 3

Related Questions