嗨我有一个指针,我想在函数中操作所以我使用双指针作为函数的参数..问题是,当它调用realloc时生成一个segmantation错误..这里有我的代码< / p>
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;
}
答案 0 :(得分:0)
我在名为int * offe的函数中使用了另一个指针解决了问题,而是使用它来代替关闭。最后我写了* off = offe;它的工作原理