Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

enter image description here

>Solution :

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.

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading