【问题标题】:C Bubble sort read access viloation issueC冒泡排序读取访问冲突问题
【发布时间】:2021-05-30 04:40:45
【问题描述】:
#ifdef _MSC_VER
#define _CRT_SECURE_NO_WARNING
#endif

#include <stdio.h>
#include <stdlib.h>
void bubbleSort(int matrix[9], int n);

int main(void) {


    int matrix[9];
    int n = 9;
    printf("enter 10 values to the matirx \n");

    for (int i = 0; i < 10; i++) {
        printf("now %dth componet\n ", i+1);
        scanf("%d", &matrix[i]);
    }

    bubbleSort(matrix[9], n);
    return 0;

}



void bubbleSort(int matrix [9], int n) {

    //bubble sort the given matrix
    int temp = 0;

    for (int i=n-1; i > 0; i--) {

        // compare two values at j and j+1 and move left when j+1 is smaller than j 
        for (int j = 0; j < i; j++) {

            if (matrix[j] > matrix[j + 1]) {
                temp = matrix[j];
                matrix[j] = matrix[j + 1];
                matrix[j + 1] = temp;
            }
        }
        
        printf("Check the matrix \n");

        for (int i = 0; i < 9; i++) {
            printf("%d ", matrix[i]);
            }
        
        printf("\n");

    }

}

您好,我收到一个读取访问冲突错误

            if (matrix[j] > matrix[j + 1]) {
                **temp = matrix[j];**
                matrix[j] = matrix[j + 1];
                matrix[j + 1] = temp;
            }

这部分代码。代码构建正确,但是当我运行程序时出现错误。谁能帮我解决这个问题?我搜索了一下,基于此,我认为它与指针有关,但我不知道为什么指针会出现问题,因为我从未在我的代码中使用过它。

【问题讨论】:

  • 如何为数组 int matrix[9] 输入 10 个数字;只包含 9 个元素?
  • assert (j+1 &lt; n)
  • bubbleSort(matrix[9], n):这是使用数组的元素 #9 作为参数,而不是 9 元素数组的地址。你想要bubbleSort (matrix, n)。 @VladfromMoscow 在数组大小上是正确的:int matrix [9] 创建了一个包含 0 到 8 元素的 9 元素数组。您希望 int matrix [10] 作为您的声明。
  • @VladfromMoscow paxdiablo,(760k 代表)今天早上显然需要更多咖啡:)
  • @paxdiablo,声明:matrix[9] 的有效索引为 0.1.2.3.4.5.6.7.8 索引 9 将超出数组的末尾。

标签: arrays c sorting bubble-sort function-definition


【解决方案1】:

对于初学者,您声明了一个仅包含 9 元素的数组。

int matrix[9];

另一方面,您正在尝试输入10 元素。

printf("enter 10 values to the matirx \n");

for (int i = 0; i < 10; i++) {
    printf("now %dth componet\n ", i+1);
    scanf("%d", &matrix[i]);
}

所以程序已经有未定义的行为。

在这个函数声明中

void bubbleSort(int matrix[9], int n);

第一个参数声明中使用的幻数9 没有多大意义。随便写

void bubbleSort(int matrix[], int n);

void bubbleSort(int *matrix, int n);

在本次通话中

bubbleSort(matrix[9], n);

而不是像传递数组matrix

bubbleSort(matrix, n);

您正在传递数组matrix[9] 中不存在的元素。编译器应该发出一条消息,表明您正在尝试将整数转换为指针。

在函数中,您在此循环中使用幻数 9 而不是参数 n

for (int i = 0; i < 9; i++) {

注意一维数组命名为matrix时看起来很奇怪。

使用您的方法,程序可以如下所示。

#include <stdio.h>

void bubbleSort( int matrix[], size_t n ) 
{
    if ( n )
    {
        for ( size_t i = n - 1; i != 0;  i-- ) 
        {
            // compare two values at j and j+1 and move left when j+1 is smaller than j 
            for ( size_t j = 0; j < i; j++ ) 
            {
                if ( matrix[j + 1] < matrix[j] ) 
                {
                    int temp = matrix[j];
                    matrix[j] = matrix[j + 1];
                    matrix[j + 1] = temp;
                }
            }
        }
    }
}

int main(void) 
{
    enum { N = 9 };
    int matrix[N];

    printf( "enter %d values to the matirx \n", N );

    for ( int i = 0;  i < N; i++ ) 
    {
        printf( "now %dth componet\n", i+1 );
        scanf( "%d", matrix + i );
    }

    bubbleSort( matrix, N );
    
    for ( int i = 0;  i < N; i++ ) 
    {
        printf( "%d", matrix[i] );
    }
    putchar( '\n' );
    
    return 0;
}

程序输出可能看起来

enter 9 values to the matirx 
now 1th componet
9
now 2th componet
8
now 3th componet
7
now 4th componet
6
now 5th componet
5
now 6th componet
4
now 7th componet
3
now 8th componet
2
now 9th componet
1
1 2 3 4 5 6 7 8 9

【讨论】:

  • 感谢您的友好回答!
  • @TrueRoughly 选择最佳答案以结束问题。
【解决方案2】:
bubbleSort(matrix[9], n);

这仅传递列表的最后一个元素(强制转换为地址),而不是列表的实际地址,这可能是您想要的。

结果不会很好:-)

你应该传入matrix


一个体面的编译器应该警告你这一点,例如gcc:

prog.c: In function ‘main’:
prog.c:21:22: warning: passing argument 1 of ‘bubbleSort’
              makes pointer from integer without a cast
              [-Wint-conversion]
   21 |     bubbleSort(matrix[9], n);
      |                ~~~~~~^~~
      |                      |
      |                      int
prog.c:7:21: note: expected ‘int *’ but argument is of
             type ‘int’
    7 | void bubbleSort(int matrix[9], int n);
      |                 ~~~~^~~~~~~~~

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-12-19
    • 2021-08-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多