You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

排序整数数组时出现Read Access Violation问题排查与修正

快速排序数组的访问冲突问题及修复

问题现象

针对整数数组的快速排序实现修改后,调试时在以下代码行触发读取访问违规(Read Access Violation):

while (v->data[i] < v->data[pivot]) i++;
while (v->data[j] > v->data[pivot]) j--;   

错误原因

代码中pivot被赋值为数组元素的数值平均值((v->data[first] + v->data[last]) / 2),但后续错误地将它当作数组索引去访问v->data[pivot],导致内存越界访问。

修复方案

将循环条件修改为直接和pivot的数值比较,而非用它作为索引访问数组:

while (v->data[i] < pivot) i++;
while (v->data[j] > pivot) j--;  

修复后的完整代码

vector.c(排序实现文件)

#include "vector.h"

void QuickSort(Vector* v, size_t first, size_t last) {
    size_t i = 0; size_t j = 0;
    int pivot = 0; // 改为int类型,匹配数值存储而非索引
    if (first <= last) {
        i = first; j = last; 
        pivot = (v->data[first] + v->data[last]) / 2; 
        do {
            while (v->data[i] < pivot) i++;
            while (v->data[j] > pivot) j--; 
            if (i <= j) {
                ElemSwap(&v->data[i], &v->data[j]); 
                i++; j--; 
            }
        } while (i <= j); 
        QuickSort(v, first, j); 
        QuickSort(v, i, last); 
    }
}
void VectorSort(Vector* v) {
    if (v == NULL) {
        return;
    }
    QuickSort(v, 0, v->size - 1);
}

int main(void) {
    ElemType v[16] = { 1, 3, 2, 9, 0, 4, 7, 8, 8, 7, 6, 5, 4, 3, 2, 1 };
    Vector* pv = calloc(1, sizeof(Vector)); 
    if (pv == NULL) {
        return 1; // main返回值应为int,修正原return NULL的错误
    }
    pv->size = pv->capacity = 16; 
    pv->data = v; 
    VectorSort(pv); 
    return 0; 
} 

elemtype.c

#define _CRT_SECURE_NO_WARNINGS
#include "elemtype.h"

#include <string.h>
#include <stdlib.h>

#define _unused(x) ((void)(x))

int ElemCompare(const ElemType *e1, const ElemType *e2) {
    return (*e1 > *e2) - (*e1 < *e2);
}

ElemType ElemCopy(const ElemType *e) {
    return *e;
}

void ElemSwap(ElemType *e1, ElemType *e2) {
    ElemType tmp = *e1;
    *e1 = *e2;
    *e2 = tmp;
}

void ElemDelete(ElemType *e) {
    _unused(e);
}

int ElemRead(FILE *f, ElemType *e) {
    return fscanf(f, "%d", e);
}

int ElemReadStdin(ElemType *e) {
    return ElemRead(stdin, e);
}

void ElemWrite(const ElemType *e, FILE *f) {
    fprintf(f, "%d", *e);
}

void ElemWriteStdout(const ElemType *e) {
    ElemWrite(e, stdout);
} 

elemtype.h

#ifndef ELEMTYPE_INT_H_
#define ELEMTYPE_INT_H_

#include <stdbool.h>
#include <stdio.h>

typedef int ElemType;


int ElemCompare(const ElemType *e1, const ElemType *e2);


ElemType ElemCopy(const ElemType *e);


void ElemSwap(ElemType *e1, ElemType *e2);


void ElemDelete(ElemType *e);

int ElemRead(FILE *f, ElemType *e);


int ElemReadStdin(ElemType *e);


void ElemWrite(const ElemType *e, FILE *f);


void ElemWriteStdout(const ElemType *e);

#endif // ELEMTYPE_INT_H_

vector.h

#pragma once
#include "ElemType.h"
#include <stdlib.h>
typedef struct {
    size_t capacity;
    size_t size;
    ElemType* data;
} Vector;

void VectorSort(Vector* v);

内容的提问来源于stack exchange,提问作者Gabriel Burzacchini

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.22 20:35:41