将指针传递到一个弹力中

Passing and realloc a pointer into a funcion

本文关键字:一个 指针      更新时间:2023-10-16

嗨,我有一个我想在funciont中操纵的指针,因此我将双重指针用作函数的参数。.问题是,当它调用Realloc时,生成了segmantation故障..这里有我的代码

 Loader::Loader(char* filename)
{
    file_desc=open(filename,O_RDONLY);
    if(file_desc<0) {
        std::cout<<"Error to open file..."<<std::endl;
    }       
    offsets=(int*)malloc(sizeof(int));  
    this->detect_element(&offsets,'o',0);
}
void Loader:: detect_element(int** off,char p,int loffset,int end)
{
    char buffer;
    int count=1;
    int i=0;
    std::cout<<"Starting with caracter "<<p<<" from "<<loffset; 
    if(end!=-1)
    {
        std::cout<<" and ending to "<<end<<std::endl;
    }else{ 
        std::cout<<" and ending to the end"<<std::endl;
    }
    lseek(file_desc,loffset,SEEK_SET);
    while(read(file_desc,&buffer,1)>0)
    {
        if(buffer==p && state==CR)
        {
            *off[count-1]=i;
            *off=(int*)realloc(*off,
                sizeof(int)*(++count));
        }
        else if(buffer=='n'){
            state=CR;
        }
        else{
            state=-1;
        }
        i++;
        if(end!=-1&&end==i){break;}
    }
    std::cout<<"Number of objs detected is "<<this->Length(*off)
        <<count<<std::endl<<std::endl;
}

i使用称为int*的函数中的另一个指针求解,而我将其与之合作。到最后,我写 *off = offe;它起作用