gpt4 book ai didi

c++ - 为什么这个程序在我插入前 2 个输入后崩溃

转载 作者:行者123 更新时间:2023-12-03 17:45:12 24 4
gpt4 key购买 nike

我必须在函数中进行动态分配(双指针),然后我必须在矩阵中放入一些数字,然后释放它

当我必须在第一次动态分配后创建矩阵时,我也尝试在第一个函数中编写 **matrice 而不是 *(matrice[i]) 但它总是错误的,在这种情况下,程序在我插入后退出矩阵的第一行

#include <iostream>
#include <stdlib.h>

using namespace std;

void allocaMatrice(int *** matrice,int * r,int * c);
void leggiMatrice(int ** matrice,int r,int c);
void scriviMatrice(int ** matrice,int r,int c);
void deallocaMatrice(int ** matrice,int r);

int main (){
int **matrice;
int r, c;
allocaMatrice(&matrice,&r,&c);
leggiMatrice(matrice,r,c);
scriviMatrice(matrice,r,c);
deallocaMatrice(matrice,r);

cin.get();
return 0;
}

void allocaMatrice(int *** matrice,int * r,int * c){
cout<<"Dimmi il numero di righe della matrice"<<endl;
cin>>*r;
while(cin.get()!='\n');
cout<<"Dimmi il numero di colonne della matrice"<<endl;
cin>>*c;
while(cin.get()!='\n');

*matrice=(int **) calloc (*r,sizeof(int));

for(int i=0;i<*c;i++){
*matrice[i]=(int *) malloc ((*c)*sizeof(int));
}
}

void leggiMatrice(int ** matrice,int r,int c){
for(int i=0;i<r;i++){
for(int j=0;j<c;j++){
cout<<"Dimmi l'elemeno alla riga e colonna corrente"<<endl;
cin>>matrice[i][j];
while(cin.get()!='\n');
}
}
}

void scriviMatrice(int ** matrice,int r,int c){

for(int i=0;i<r;i++){
for(int j=0;j<c;j++){
cout<<matrice[i][j]<<"\t";
}
cout<<endl;;
}

}

void deallocaMatrice(int ** matrice,int r){
int i;
for(i=0;i<r;i++){
free((*matrice)+1);
}
free(matrice);
}

调试错误是“程序收到信号 SIGSEGV,段错误”

最佳答案

线

*matrice=(int **) calloc (*r,sizeof(int));

是不正确的。它为 *r 分配内存 int 的数量s。您需要为 *r 分配内存 int* 的数量s(指针不是 int s)

它需要是:
*matrice=(int **) calloc (*r, sizeof(int*));

您使用 C++ 作为语言标记,但您的代码设计为像 C 程序一样工作。正确使用 C++,让你的程序更干净。
  • 使用std::vector用于容器并消除管理内存的需要。
  • 使用引用类型在函数之间传递参数。

  • 因此:
    #include <iostream>
    #include <vector>

    template<typename T>
    using VecVec = std::vector<std::vector<T>>;
    using IntVecVec = VecVec<int>;

    void allocaMatrice(IntVecVec& matrice);
    void leggiMatrice(IntVecVec& matrice);
    void scriviMatrice(IntVecVec const& matrice);

    // Not necessary.
    // void deallocaMatrice(int ** matrice,int r);

    int main() {
    IntVecVec matrice;
    allocaMatrice(matrice);
    leggiMatrice(matrice);
    scriviMatrice(matrice);

    std::cin.get();
    }

    void allocaMatrice(IntVecVec& matrice) {
    int righe, colonne;
    std::cout << "Dimmi il numero di righe della matrice\n";
    std::cin >> righe;
    //while (cin.get() != '\n');???
    std::cout << "Dimmi il numero di colonne della matrice\n";
    std::cin >> colonne;
    //while (cin.get() != '\n');??

    matrice.resize(righe, std::vector<int>(colonne, 0));
    }

    void leggiMatrice(IntVecVec& matrice) {
    for (auto& riga : matrice) {
    for (auto& elemeno : riga) {
    std::cout << "Dimmi l'elemeno alla riga e colonna corrente\n";
    std::cin >> elemeno;
    //while(cin.get()!='\n'); // what does this add?
    }
    }
    }

    void scriviMatrice(IntVecVec const& matrice) {
    for (auto const& riga : matrice) {
    for (auto const& elemeno : riga) {
    std::cout << elemeno << '\t';
    }
    std::cout << '\n';
    }
    }

    关于c++ - 为什么这个程序在我插入前 2 个输入后崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58632370/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com