Search code examples
carrayssortingbubble-sort

memory read failed for 0x600000000


i am doing my simple C language practice on xcode Mac

this is simple bubble sort algorithm.

i guess there is no logical or grammar error in the codes but i see the error warning after debugging

error: memory read failed for 0x600000000

maybe this is about memory problem but i don't know what it means about and how i can solve this problem without any warning messages

P.S: i am not native english speaker. Thank you for understanding my poor english in advance :)

int p[]={0},i,j,num,temp;

printf("number: ");
scanf("%d",&num);

for(i=0;i<num;i++)
    scanf("%d",&p[i]);

printf("arry numbers show-----");
printf("\n");
for(i=0;i<num;i++)
    printf("%d ",p[i]);
printf("\n");

for(i=0;i<num;i++)
{
    for(j=0;j<num-1;j++)
    {
        if(p[j]>p[j+1])
        {
            temp=p[j];
            p[j]=p[j+1];
            p[j+1]=temp;
        }
    }
}

for(i=0;i<num;i++)
    printf("%d ",p[i]);
printf("\n");

Solution

  • You have explicitly initialized array int p[]={0} with only one element. In case of sorting if you have only one element that means it is already sorted! See this documentation of Arrays Arrays documentation.