Azurestar_Hu 2021-07-29 14:51 采纳率: 100%
浏览 753
已结题

引发了异常: 读取访问权限冲突。 **_Ptr_user** 是 0x0。

    背景

这是我尝试最大公共子序列时用到的一份代码,代码大量用到了指针,并且以DP和dfs作为解题的主要突破方向。

    使用环境

系统:64位 windows7 旗舰版 SP1

可视化编译工具:Visual Studio 2019 社区版 

    问题

我尝试一组数据,程序成功输出了我想要的结果,但是在通过图示标注的断点之后,爆出了题干的异常。 (这也是我最疑惑的其中一个点,因为以往从未见过顺利通过了return 0;的main语句还会出错的情况)即:

线程 0x2674 已退出,返回值为 0 (0x0)。

引发了异常: 读取访问权限冲突。

**_Ptr_user** 是 0x0。

    曾有的尝试

  我曾认为可能是dfs函数内大量声明string导致局部变量爆栈,于是添加了全局的BunkOfString的string组和STRScale的标记用来维护栈,并将dfs函数的string形参类型修改为引用,想要降低dfs函数对栈的负担,但是最后的结果仍旧一样,并无任何改良成功的迹象。因此我想知道,导致这份代码异常的原因到底是什么。

如果能够给我指一条方向就好了。

    下附图片

img

    下附代码(c++)


#pragma warning(disable:4996)
#include<cstdio>
#include<iostream>
#include<string>
#include<algorithm>
using std::string;

struct node {
    char val;
    bool isitmother;//mark the main node
    int rank;
    node* left;
    node* upright;
};
node BUNK[10000];
int num;
char val1[90], val2[90];
node* table[90][90];
int N1, N2;
string strlib[10000];
int strnum;
string BunkOfString[10000];
int STRScale;
void dfs(node* point, const string& str) {
    if (point->rank == 0) {//in one end of the dfs
        strlib[strnum].clear();
        int i = str.size() - 1;
        for (; i >= 0; i--) {
            strlib[strnum] += str[i];
        }
        strnum++;
        return;
    }
    if (point->isitmother == true) {//in the prime node
        string& stre = BunkOfString[STRScale++];
        stre = str + point->val;
        dfs(point->left, stre);
        stre.clear();
    }
    else {//in the subprime node
        while (point->isitmother != true) {
            dfs(point->upright, str);
            point = point->left;
        }
        dfs(point, str);
    }

}
int main() {
    //load
    char c;
    while (scanf("%c", &c), c == ' ');
    val1[(N1 = 0)++] = c;
    while (scanf("%c", &c), c != ' '&&c!='\n') {
        val1[N1++] = c;
    }
    while (scanf("%c", &c), c == ' ' || c == '\n');
    val2[(N2 = 0)++] = c;
    while (scanf("%c", &c) != EOF && c != ' '&&c!='\n') {
        val2[N2++] = c;
    }
    //initial
    BUNK[0].isitmother = true;
    BUNK[0].left = NULL;
    BUNK[0].upright = NULL;
    BUNK[0].val = '\0';
    BUNK[0].rank = 0;
    num = 1;
    for (int i = 0; i < 100; i++) {
        table[i][0] = BUNK;
        table[0][i] = BUNK;
    }
    for (int i = 0; i < N1; i++) {
        for (int j = 0; j < N2; j++) {
            if (val1[i] == val2[j]) {//inherit From triangle side
                node* oper = table[i + 1][j + 1] = &BUNK[num++];
                oper->isitmother = true;
                oper->left = table[i][j];
                oper->upright = oper;//self to self , easier to transmit
                oper->rank = table[i][j]->rank + 1;
                oper->val = val1[i];
            }
            else {//inherit from side two elements
                if (table[i + 1][j] == table[i][j + 1]) {//inherit directly
                    table[i + 1][j + 1] = table[i + 1][j];
                }
                else if(table[i+1][j]->rank==table[i][j+1]->rank) {//inherit from two
                    node* oper = table[i + 1][j + 1] = &BUNK[num++];
                    oper->isitmother = false;
                    oper->left = table[i + 1][j];
                    oper->upright = table[i][j + 1]->upright;//easier to transmit
                    oper->rank = table[i + 1][j]->rank;
                    oper->val = '\0';//nonsense actually
                }
                else {//inherit from one
                    if (table[i + 1][j]->rank < table[i][j + 1]->rank) {
                        table[i + 1][j + 1] = table[i][j + 1];
                    }
                    else {
                        table[i + 1][j + 1] = table[i + 1][j];
                    }
                }
            }
        }
    }
    string str0;
    strnum = 0;//no intialization in the begin of the cpp file
    STRScale = 0;
    dfs(table[N1][N2],str0);
    std::sort(strlib, strlib + strnum);
    for (int i = 0; i < strnum; i++) {
        std::cout << strlib[i] << "\n";
        strlib[i].clear();
    }
    return 0;
}


  • 写回答

2条回答 默认 最新

  • Azurestar_Hu 2021-07-29 21:26
    关注

    谢谢大家的查看,解决方案已经找到了,貌似是使用string类导致内存爆了。事实上,将string组的大小减小到10的时候是不会异常的,而到100的时候又会异常。所以我猜测可能是string类的不定长特性导致的内存爆存,所以我停用string类,自己编写了一个定长的string类之后就没有异常抛出了。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 8月6日
  • 已采纳回答 7月29日
  • 创建了问题 7月29日

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效