c - Valgrind 和 strlen() 函数错误

标签 c memory-leaks valgrind

我有一些考试计划。考试系统与 valgrind 配合使用。

请帮帮我 我在 valgrind 中有一些错误,我不知道如何解决我的程序中的错误: 我想将 stdin 复制到 *in 指针并从 stdin 中删除所有空格和换行符

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>

#define DELTA 2
unsigned int my_strlen(char *str);
char *get_expression(void);
int main(void)
{
    char *in, *out;
    in = get_expression();
    if (!in) {
        return 1;
    }
    out = calloc(my_strlen(in), 1);
    free(out);
    free(in);
    return 0;
}
char *get_expression(void)
{
    char *in, *in_bckp, t;
    in = malloc(DELTA);
    if (!in) {
        return NULL;
    }
    int i, c;
    for (i = 0, c = DELTA; (t = getchar()) != EOF; i++) {
        if (i >= c) {
            in_bckp = in;
            in = realloc(in, c + DELTA);
            if (!in) {
                free(in_bckp);
                return NULL;
            }
            c += DELTA;
        }
        if (c == ' ' || c == '\n') { // i need to remove all of the spaces or newlines
            continue;
        }
        in[i] = t;
    }
    if (i >= c) {
        in_bckp = in;
        in = realloc(in, c + DELTA);
        if (!in) {
            free(in_bckp);
            return NULL;
        }
    }
    in[i] = '\0';
    return in;
}

unsigned int my_strlen(char *str)
{
    unsigned int i;
    for (i = 0; str[i] != '\0' && i < 40000; i++);
    return i;
}

以及我来自 valgrind 的错误:

ivr@debian:/tmp
$ valgrind --leak-check=full --track-origins=yes ./a.out < ~/work/programming/kursovik/parsing/test/data
==5044== Memcheck, a memory error detector
==5044== Copyright (C) 2002-2011, and GNU GPL'd, by Julian Seward et al.
==5044== Using Valgrind-3.7.0 and LibVEX; rerun with -h for copyright info
==5044== Command: ./a.out
==5044== 
==5044== Conditional jump or move depends on uninitialised value(s)
==5044==    at 0x4007D5: my_strlen (test.c:59)
==5044==    by 0x400666: main (test.c:16)
==5044== 
==5044== 
==5044== HEAP SUMMARY:
==5044==     in use at exit: 0 bytes in 0 blocks
==5044==   total heap usage: 17 allocs, 17 frees, 280 bytes allocated
==5044== 
==5044== All heap blocks were freed -- no leaks are possible
==5044== 
==5044== For counts of detected and suppressed errors, rerun with: -v
==5044== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 4 from 4)

请帮我解决这个错误。

最佳答案

您在 in 数组中留下了间隙。基本上,你会说:

int i, t;

for (i = 0; (t = getchar()) != EOF; i++) {
    if (t == ' ' || t == '\n') continue;
    in[i] = t;
}

当您读取空格或换行符时,您会跳过赋值,但会增加 i++。 Valgrind(正确地)将 in 中的这些间隙识别为 unilialized 内存。

for 循环在这里不是一个好的选择。 while 循环可能会更好:

int t;
int i = 0;

while ((t = getchar()) != EOF) {
    if (t != ' ' && t != '\n') in[i++] = t;
}

注意计数器i如何仅在发生赋值时递增。这两个密切相关的事件几乎同时进行。

关于c - Valgrind 和 strlen() 函数错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28124896/

相关文章:

c++ - 为什么结构体的 sizeof 不等于每个成员的 sizeof 之和?

iphone - iOS CALayer 内存泄漏仅在设备上

c++ - Valgrind 大小为 4 的无效读取 (OpenCV)

c++ - 在 C++ 中对 unordered_map 使用 getter 会创建大小为 8 的无效读取

c - 调查 Valgrind 无效读取

c - 链表值被改变

c++ - 如何在 Windows 上编译 encfs4win

c - g_io_scheduler_job_send_to_mainloop问题

c++ - 嵌套对象中的内存不可用

memory-leaks - 在 flutter 中预加载图像 Assets