史迪仔吃辣条 2022-02-16 22:56 采纳率: 0%
浏览 45

引发了异常: 读取访问权限冲突,我写顺序表的时候出现的错误,如何解决?(语言-c语言|开发工具-visual studio)

问题遇到的现象和发生背景 引发了异常: 读取访问权限冲突
问题相关代码,请勿粘贴截图
运行结果及报错内容 运行起来就崩了
我的解答思路和尝试过的方法
我想要达到的结果 有无捞我一下

-

img

img

img

img

  • 写回答

1条回答 默认 最新

  • 关注

    结构体元素 TSeqListNode * Node是一级指针,Node中的元素是TSeqListNode 类型,也就是unsigned int 类型,
    所以在insert函数中,传入的是SeqListNode * node指针,在往Node里放的时候,放入的应该是node指向内存的值,而不应该直接用node
    同理,Get函数也是一个道理。你的代码写的复杂了,很容易绕晕了。先按你的思路给你改出来,稍后再给你写个精简版。
    另外,unsigned int转int可能会越界,这个注意下。
    代码修改如下:

    
    #include<stdio.h>
    #include<stdlib.h>
    typedef void  SeqList;
    typedef void  SeqListNode;
    typedef unsigned int TSeqListNode;//对数据元素的定义
    
    typedef struct _tag_SeqList
    {
        int capacity;//最大容量
        int length;//当前长度
        TSeqListNode* Node;//指向数组的指针
    }TSeqList;
    
    SeqList* Create(int capacity)
    {
        TSeqList* ret = NULL;//创建一个线性表的指针
        if ( capacity >= 0)
        {
            ret = (TSeqList*)malloc(sizeof(TSeqList));//申请线性表和数组的空间
            ret->Node = (TSeqListNode*)malloc(sizeof(TSeqListNode) * capacity);
    
            ret->capacity = capacity;
            ret->length = 0;
        }
        return ret;
    }
    
    int Insert(SeqList* list,SeqListNode* node, int pos)
    {
        TSeqList* slist = (TSeqList*)list;
        int ret = (list != NULL);
        int i = 0;
        ret = ret && (slist->length + 1 <= slist->capacity); 
        ret = ret && (pos >= 0);
        if (ret)
        {
            if (pos >= slist->length)
            {
                pos = slist->length;
            }
            for (i = slist->length; i > pos; i--)
            {
                slist->Node[i] = slist->Node[i - 1];
            }
            slist->Node[i] = *((TSeqListNode*)node); //修改
            slist->length++;
        }
    
        return ret;
    }
    
    SeqListNode* Get(SeqList* list, int pos)
    {
        TSeqList* slist = (TSeqList*)list;
        SeqListNode* ret = NULL;
        if (slist != NULL && (pos >= 0) && (pos < slist->length))//修改 pos < slist->length,pos从0开始,所以不能取slist->length
        {
            ret = (SeqListNode*)(&(slist->Node[pos])); //修改 
        }
        return ret;
    }
    
    int main()
    {
        SeqList *list = Create(5);
    
        int a = 0; int b = 1; int c = 2; int d = 3; int e = 4; int f = 5;
        int indext = 0;
        Insert(list, &a, 0);
        Insert(list, &b, 0);
        Insert(list, &c, 0);
        Insert(list, &d, 0);
        Insert(list, &e, 0);
        int* p = (int*)Get(list, 0);
    
        printf("%d", *p);
        return 0;
    
    }
    

    下面是精减版代码:

    #include<stdio.h>
    #include<stdlib.h>
    
    typedef unsigned int TSeqListNode;//对数据元素的定义
    
    typedef struct _tag_SeqList
    {
        int capacity;//最大容量
        int length;//当前长度
        TSeqListNode* Node;//指向数组的指针
    }TSeqList;
    
    TSeqList* Create(int capacity)
    {
        TSeqList* ret = NULL;//创建一个线性表的指针
        if ( capacity >= 0)
        {
            ret = (TSeqList*)malloc(sizeof(TSeqList));//申请线性表和数组的空间
            ret->Node = (TSeqListNode*)malloc(sizeof(TSeqListNode) * capacity);
    
            ret->capacity = capacity;
            ret->length = 0;
        }
        return ret;
    }
    
    int Insert(TSeqList* list,TSeqListNode node, int pos)
    {
        int ret = (list != NULL);
        int i = 0;
        ret = ret && (list->length + 1 <= list->capacity); 
        ret = ret && (pos >= 0);
        if (ret)
        {
            if (pos >= list->length)
            {
                pos = list->length;
            }
            for (i = list->length; i > pos; i--)
            {
                list->Node[i] = list->Node[i - 1];
            }
            list->Node[i] = node;
            list->length++;
        }
    
        return ret;
    }
    
    TSeqListNode Get(TSeqList* list, int pos)
    {
        TSeqListNode ret = 0;
        if (list != NULL && (pos >= 0) && (pos < list->length))
        {
            ret = list->Node[pos];
        }
        return ret;
    }
    
    int main()
    {
        TSeqList *list = Create(5);
    
        int a = 0; int b = 1; int c = 2; int d = 3; int e = 4; int f = 5;
        int indext = 0;
        Insert(list, a, 0);
        Insert(list, b, 0);
        Insert(list, c, 0);
        Insert(list, d, 0);
        Insert(list, e, 0);
        int p = (int)Get(list, 0);
    
        printf("%d", p);
        return 0;
    
    }
    
    
    评论 编辑记录

报告相同问题?

问题事件

  • 创建了问题 2月16日

悬赏问题

  • ¥20 非root手机,如何精准控制手机流量消耗的大小,如20M
  • ¥15 远程安装一下vasp
  • ¥15 自己做的代码上传图片时,报错
  • ¥15 Lingo线性规划模型怎么搭建
  • ¥15 关于#python#的问题,请各位专家解答!区间型正向化
  • ¥15 unity从3D升级到urp管线,打包ab包后,材质全部变紫色
  • ¥50 comsol温度场仿真无法模拟微米级激光光斑
  • ¥15 上传图片时提交的存储类型
  • ¥15 VB.NET如何绘制倾斜的椭圆
  • ¥15 arbotix没有/cmd_vel话题