C++ 变量“nom”周围的堆栈溢出

C++ 变量“nom”周围的堆栈溢出,c++,C++,您好,在CONV函数中写入第二个FOR之后,我遇到了这个错误,每个FOR都可以独立工作,但是由于某种原因,当我将这两个FOR都放入函数中时,我得到了溢出。请帮忙 #pragma warning(disable: 4996) #include<stdio.h> #include<stdlib.h> #include<string.h> typedef struct nodo{ char dato[100]; struct nodo *sig; }

您好,在CONV函数中写入第二个FOR之后,我遇到了这个错误,每个FOR都可以独立工作,但是由于某种原因,当我将这两个FOR都放入函数中时,我得到了溢出。请帮忙

#pragma warning(disable: 4996)
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct nodo{
    char dato[100];
    struct nodo *sig;
}*pun;
typedef struct nodos{
    char nom[50];
    int num[20];
    struct nodos *sig;
}*puns;

void cargar(pun &top,pun &fin);
void mostrar(pun &top);
void conv(pun &top,pun &fin,puns &tops,puns &fins);
int main(){
    pun top=NULL,fin=NULL;
    puns tops=NULL,fins=NULL;
    cargar(top,fin);
    conv(top,fin,tops,fins);
    mostrar(top);
}

void cargar(pun &top,pun &fin){
    FILE *arch;
    pun aux=NULL;
    arch=fopen("nombres.txt","rt");
    if(arch==NULL){
        printf("No se puede abrir el archivo \n");
    }else{
        while(!feof(arch)){
            if(top==NULL){
                top=(nodo *)malloc(sizeof(nodo));
            fgets(top->dato,100,arch);
                        fin=top;
            }else{
                 aux=(nodo *)malloc(sizeof(nodo));
             fgets(aux->dato,100,arch);
             fin->sig=aux;
             fin=aux;
            }
        }
        fin->sig=NULL;
    }
}

你只初始化j一次。然后在while循环中多次执行for循环

该循环在第一次执行时就可以了。对于while循环的后续迭代,j是num的末尾

事实上,第一次执行num[j]='\0',j是50,因此您正在访问num的末尾


我真的不知道您的代码试图实现什么,因此无法提出解决方案。显然,你需要避免访问NUT.有效值边界之外的.< /P>你确定这是C++吗?你的问题到底在哪里?您可以删除代码中不相关的部分吗?你已经尝试过解决这个问题了吗?什么有效,什么无效?你知道为什么你的解决方案不起作用吗?非常感谢!在函数末尾添加一个j=0就解决了这个问题。很抱歉代码不够清晰,它还没有完成,这让我很沮丧。再次感谢你,我认为你需要做的不止这些。您不能像当前那样执行num[50]='\0'。如果还有其他越界错误,我也不会感到惊讶。你标记了这个C++。如果你真的在使用C++,标准的容器会让生活变得简单很多。幸运的是,已经不再有超出界限的错误了。我不确定你用标准容器是什么意思,但是你建议我做什么而不是Num [50 ]=‘0’?我是指标准C++容器。比如std::string、std::vector等等。你已经标记了C++的问题,但是它看起来更像是在编写C。至于做什么而不是Num(50)=‘0’,只有你知道。很明显,你不能抹去结局。您可以声明num的长度为51。
void mostrar(pun &top){
    pun aux=top;
    while(aux!=NULL){
        puts(aux->dato);
        aux=aux->sig;
    }
}

void conv(pun &top,pun &fin,puns &tops,puns &fins){
    pun aux=top;
    puns auxs=tops;
    char nom[50],num[50],*p;
    int i,j=0,a;
    while(aux!=NULL){
        p=strchr(aux->dato,' ');
        for(i=0;i<p-aux->dato+1;i++){
             nom[i]=aux->dato[i];
        }
        nom[i]='\0';
        for(i;i<50;i++){
            num[j]=aux->dato[i];
            j++;
        }
        num[j]='\0';
        aux=aux->sig;
    }
}
for(i;i<50;i++){
    num[j]=aux->dato[i];
    j++;
}