小编Nat*_*Kup的帖子

realloc()问题,没有分配

我正在尝试读一个字符串

char *string=malloc(sizeof(char));
char *start_string=string; //pointer to string start
while ((readch=read(file, buffer, 4000))!=0){ // read
    filelen=filelen+readch; //string length
    for (d=0;d<readch;d++)
        *start_string++=buffer[d]; //append buffer to str
    realloc(string, filelen); //realloc with new length
Run Code Online (Sandbox Code Playgroud)

有时这会崩溃并出现以下错误:

   malloc: *** error for object 0x1001000e0: pointer being realloc'd was not allocated
Run Code Online (Sandbox Code Playgroud)

但有时不是,我不知道如何解决它.

c c++ unix linux

1
推荐指数
1
解决办法
1232
查看次数

为什么read()比getc()慢

为什么syscall read()比getc()函数慢?

for (;;) {
        chr++;
        amr=read(file1, &wc1, 1);
        amr2=read(file2, &wc2, 1);
        if (wc1 == wc2) {
            if (wc1 == '\n')
                line++;
            if (amr == 0) {
                if (eflg)
                    return (1);
                return (0);
            }
            continue;
        }
Run Code Online (Sandbox Code Playgroud)

比...慢

for (;;) {
    chr++;
    c1 = getc(file1);
    c2 = getc(file2);
    if (c1 == c2) {
        if (c1 == '\n')
            line++;
        if (c1 == EOF) {
            if (eflg)
                return (1);
            return (0);
        }
        continue;
    }
Run Code Online (Sandbox Code Playgroud)

当getc()调用它时使用read()系统调用,为什么慢?

c c++ unix linux

-4
推荐指数
1
解决办法
481
查看次数

标签 统计

c ×2

c++ ×2

linux ×2

unix ×2