English 中文(简体)
我本人版本的线上的部分过失
原标题:Seg fault on my own version of getline

我试图简明扼要地介绍一下。 它应读到一条从中线,必要时重新分配缓冲面积。 还应当将品号重新编号。 它需要一个果园**,以便重新定位的缓冲能够随后释放。 我为什么会getting?

Heres my version:

int get_input_line(char **buff, int start_size) {
char c;
int stop = 0, length = 0, k = start_size;
while(!stop) {
    if(length > k) {
        k += 50;
        buff = (char *)(realloc(buff, start_size + 1));
    }
    c = getchar();
    if(c ==  
 ){
        stop = 1;
    }
    buff[length] = c; 
    length++;
}
return length;
}

在此呼吁:

char *buff = (char *)(malloc(50 + 1));
get_input_line(&buff, 50);
printf("%s", buff);
最佳回答

你们不会可靠地发现低地轨道F。 您须在<代码>int上节省getchar(>,而不是char/code>。 您不应试图在您的缓冲中存储EOF

你不再检查你的记忆分配。

您不再否认产出指示数的终止,因此f(>,载于main(<>>。

你们通过分配51个 by子和告诉它只有50个 by子才能发挥的职能,使某人(也许我,可能是汇编者,也许会放弃)感到困惑。

尤其是,you需要使用*buff,在职能的多数内容中,特别包括增加特性:

(*buff)[length++] = c;

确实,你应当更加关注所有汇编者警告。 如果你的汇编者不给你,就会有更好的编纂者(或转而登上警告旗帜,但你应当由汇编者按其违约方式加以hri弄。

另外,你还以三个理由误导<代码>realloc()。 一个问题是<代码>*buff。 第二项是,你希望这个规模成为<代码>k,而不是<代码>start_size + 1。 另一种是,你将结果分配给了输入参数。 这不是什么,因为如果分配失败,你就失去了以前(现在)分配的数据的指点。 航道:

void *new_data = realloc(old_data, new_size);
if (new_data == 0)
    ...deal with out of memory error...
else
{
    old_data = new_data;
    old_size = new_size;
}

Applied to your code, that means:

char *new_buff = (char *)realloc(*buff, k); // NOT start_size+1!!!
if (new_buff == 0)
    ...deal with out of memory error...
else
    *buff = new_buff;

有些人反对在<条码>、和<代码>realloc(>和<条码>上的投放;有些人倾向于投放。 双方都有不同程度的有效性。 我更喜欢这段话——我尊重那些宁愿不投的人。 出于不同原因,我们得出了不同的结论。

我没有研究过其他非一次性错误的守则。 我怀疑其中可能有几个。

问题回答

也许意味着:

*buff = (realloc(*buff, new_size));
^                ^

而且

(*buff)[length] = c;

您也失踪了0名确定者。

EDIT

作为nos 各点,length > k应为length >= k

段 次

buff = (char *)(realloc(buff, start_size + 1));

应当

*buff = (char *)(realloc(*buff, k + 1));

buf[length] = c

应当

*buf[length] = c

此外,我认为你愿意储存最后的<代码>。





相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...

热门标签