简体   繁体   English

C程序在我运行它的所有时间都无法正常运行

[英]C program doesn't work all the times i run it

I have got the following code and it doesn't work fine all the time I run it: 我有以下代码,并且在我每次运行它时都无法正常工作:

 🍻  🐢  ->> ./a.out gnl1_2.txt
1234567
abcdefg
 🍻  🐢  ->> ./a.out gnl1_2.txt
1234567
abcdefg
 🍻  🐢  ->> ./a.out gnl1_2.txt
1234567

It returned only the first line and sometimes gives me nothing (I might have some leaks). 它仅返回第一行,有时什么也没给我(我可能会有一些泄漏)。 Basically it should give me the next line from a file in the line parameter ( godlike = structure that has a string and a file descriptor) 基本上,它应该给我line参数中文件的下一行( godlike =具有字符串和文件描述符的结构)

int get_next_line(const int fd, char **line)
{
    static t_gnl *godlike;
    int i;

    i = 0;
    if (fd < 0 || !line || !fd)
        return (-1);
    if (!godlike)
        godlike = malloc(sizeof *godlike);
    //printf("%s\n", godlike[0].xx);
    while ((godlike[i].xx) && (godlike[i].fd != fd))
        i++;
    if (!godlike[i].xx)
    {
        godlike[i].fd = fd;
        godlike[i].xx = read_f(fd);
        if (godlike[i].xx == NULL)
            return (-1);
    }
    *line = ft_strcdup(godlike[i].xx, '\n');
    while ((godlike[i].xx[0]) && (godlike[i].xx[0] != '\n'))
    {
        godlike[i].xx[0] = '\0';
        godlike[i].xx++;
    }
    if (godlike[i].xx[0] == '\n')
    {
        godlike[i].xx[0] = '\0';
        godlike[i].xx++;
        return (1);
    }
    else
        return (0);
}

There's some undefined behaviour in this one: 此行为有一些未定义的行为:

godlike = malloc(sizeof *godlike);
while ((godlike[i].xx) && (godlike[i].fd != fd))
    i++;

You are allocating memory to hold 1 t_gnl object. 您正在分配内存以容纳1个t_gnl对象。 To start with, the condition in your while loop evaluates to: 首先,您的while循环中的条件计算为:

while((godlike[0].xx) && (godlike[0].fd != fd))

Which is perfectly valid, and equivalent to: 这是完全有效的,等效于:

while(godlike->xx && godlike->fd != fd)

However, because the allocated memory is never initialized, this condition could hold true, especially when know that godlike has static storage. 但是,因为从未初始化分配的内存,所以这种情况可能成立,特别是当知道godlike具有静态存储时。 Each time the function is called, there's a chance your while condition holds true, and you end up evaluating something like 每次调用该函数时,您的while条件都有可能成立,最终您会评估类似

while((godlike[1].xx) && (godlike[1].fd != fd))

Which is accessing memory out of bounds. 这超出了访问内存的范围。

You appear to be using your godlike pointer as an array, which it isn't. 您似乎正在使用像godlike指针作为数组,事实并非如此。 It's just 1 object, allocated on heap. 它只是1个对象,分配在堆上。 If you really want to append to that memory, and have the data you store grow over time, then you ought to consider using a linked list, or at the very least: realloc the memory every time you add an element. 如果你真的要追加到的内存,并且您存储随时间增长的数据,那么你应该考虑使用链表,或者至少是: realloc每次添加一个元素时的记忆。

The main problem with doing that using the godlike pointer you have is that it's static , and function scoped at the same time. 使用您拥有的类似godlike指针进行操作的主要问题是它是static ,并且函数的作用域在同一时间。 You'll alocate it, but you can only access it within that function, and that function is never free 'ing the memory. 您将对其进行分配,但只能在该函数中访问它,并且该函数永远不会 free内存。 That means that the memory you allocate will remain allocated for as long as the application runs (effectively behave like a runtime memory leak). 这意味着您分配的内存将在应用程序运行期间一直保持分配状态(有效地表现为运行时内存泄漏)。 If you use a list, or use realloc , the memory usage will grow every time the function is called, and memory won't get freed until your program exits. 如果使用列表或使用realloc ,则每次调用该函数时,内存使用量都会增加,并且直到程序退出,内存才会释放。 Any tool that analyses code like valgrind will flag this function as a potential memory leak for this reason. 由于这个原因,任何分析代码的工具(如valgrind)都会将此功能标记为潜在的内存泄漏。

You have major problems in your code: 您的代码中存在主要问题:

  • godlike is allocated to point to a single structure t_gnl allocated by malloc() and left uninitialized. godlike被分配为指向由malloc()分配t_gnl初始化的单个结构t_gnl

  • you access members that have not been initialized 您访问尚未初始化的成员

  • once you increment i , you access elements beyond the first that have not been allocated. 一旦增加i ,就可以访问第一个以外的尚未分配的元素。

As posted, the code has undefined behavior. 如前所述,该代码具有未定义的行为。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM