Valgrind中大小为4的无效读/写

布利

我目前正在研究一个C程序,以调试Valgrind报告错误的位置。

我已经将一些代码剥离到一个小项目中,以测试我认为问题出在哪里,我相信我已经复制了很多问题。

下面是我的主要功能

int main(int argc, char** argv) {

    FILE *csvFile = NULL;
    csvFile = fopen("test.txt", "wb");
    int arraySize = 10;
    int i = 0;

    targetsStruct *targets;
    targetSummaryResultStruct *targetSummaryResult;
    targets = malloc(arraySize + 10 * sizeof(targets));
    targetSummaryResult = malloc(arraySize + 10 * sizeof(targetSummaryResultStruct));

    for (i = 0; i < arraySize; i++)
    {
        asprintf(&targets[i].target, "TargetStruct: %i", i);
        targets[i].rowID = i;
    }


    for (i = 0; i < arraySize; i++)
    {
        asprintf(&targetSummaryResult[i].target, "Target: %s", targets[i].target);
        free(targets[i].target);
        printf("%s\n", targetSummaryResult[i].target);
        targetSummaryResult[i].callAttempts = i * 10;

       fprintf(csvFile, "%s = %i\n", targetSummaryResult[i].target, targetSummaryResult[i].callAttempts);
       free(targetSummaryResult[i].target);
    }
    fclose(csvFile);
    printf("Structure completed");
    free(targetSummaryResult);
    free(targets);

    return (EXIT_SUCCESS);
}

我故意分配内存以表示其arraySize +10。其原因是我尝试调试的程序分配了比实际所需更多的数组元素,因此我正在测试这是否是潜在问题因此,尽管数组大了10个元素,但实际上我只向arraySize填充了0,剩下了10个。

以下是我的结构的定义方式

typedef struct TargetSummaryResultStruct
{
    char * target;
    int callAttempts;
} targetSummaryResultStruct;

typedef struct TargetsStruct
{
    char * target;
    int rowID;
} targetsStruct;

以下是我的valgrind报告:

==10244== HEAP SUMMARY:
==10244==     in use at exit: 0 bytes in 0 blocks
==10244==   total heap usage: 43 allocs, 43 frees, 2,892 bytes allocated
==10244==
==10244== All heap blocks were freed -- no leaks are possible
==10244==
==10244== ERROR SUMMARY: 20 errors from 5 contexts (suppressed: 12 from 8)
==10244==
==10244== 4 errors in context 1 of 5:
==10244== Invalid read of size 4
==10244==    at 0x8048619: main (main.c:48)
==10244==  Address 0x40181e8 is 48 bytes inside a block of size 50 alloc'd
==10244==    at 0x40072D5: malloc (vg_replace_malloc.c:291)
==10244==    by 0x804856D: main (main.c:35)
==10244==
==10244==
==10244== 4 errors in context 2 of 5:
==10244== Invalid read of size 4
==10244==    at 0x80485EC: main (main.c:47)
==10244==  Address 0x40181e8 is 48 bytes inside a block of size 50 alloc'd
==10244==    at 0x40072D5: malloc (vg_replace_malloc.c:291)
==10244==    by 0x804856D: main (main.c:35)
==10244==
==10244==
==10244== 4 errors in context 3 of 5:
==10244== Invalid write of size 4
==10244==    at 0x80485C2: main (main.c:41)
==10244==  Address 0x40181ec is 2 bytes after a block of size 50 alloc'd
==10244==    at 0x40072D5: malloc (vg_replace_malloc.c:291)
==10244==    by 0x804856D: main (main.c:35)
==10244==
==10244==
==10244== 4 errors in context 4 of 5:
==10244== Invalid read of size 4
==10244==    at 0xAF770B: vasprintf (in /lib/libc-2.12.so)
==10244==    by 0xAD956A: asprintf (in /lib/libc-2.12.so)
==10244==    by 0x80485B2: main (main.c:40)
==10244==  Address 0x40181e8 is 48 bytes inside a block of size 50 alloc'd
==10244==    at 0x40072D5: malloc (vg_replace_malloc.c:291)
==10244==    by 0x804856D: main (main.c:35)
==10244==
==10244==
==10244== 4 errors in context 5 of 5:
==10244== Invalid write of size 4
==10244==    at 0xAF76DD: vasprintf (in /lib/libc-2.12.so)
==10244==    by 0xAD956A: asprintf (in /lib/libc-2.12.so)
==10244==    by 0x80485B2: main (main.c:40)
==10244==  Address 0x40181e8 is 48 bytes inside a block of size 50 alloc'd
==10244==    at 0x40072D5: malloc (vg_replace_malloc.c:291)
==10244==    by 0x804856D: main (main.c:35)
==10244==
--10244--
--10244-- used_suppression:     12 U1004-ARM-_dl_relocate_object /usr/local/lib/valgrind/default.supp:1391
==10244==
==10244== ERROR SUMMARY: 20 errors from 5 contexts (suppressed: 12 from 8)

我不确定如何解决这些错误,我对无效读取的理解是,我试图访问一些可能已经被释放但部分内存由于某种原因没有被释放的内存。

我是C的新手。

塞姆·奥泽(Cem Ozer)

有一个算术优先级错误。*在之前求值+,因此您得到的内存块大小错误。同样,首先malloc,您正在分配内存以容纳targetsStruct结构本身的指针

targets = malloc((arraySize + 10) * sizeof(targetsStruct)); //change targets to targetsStruct
targetSummaryResult = malloc((arraySize + 10) * sizeof(targetSummaryResultStruct));

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

无效的读写valgrind

来自分类Dev

Valgrind:大小为1的无效读/写

来自分类Dev

无效索引3,在Android中大小为0

来自分类Dev

C ++无效的读取大小4 valgrind

来自分类Dev

Valgrind 错误无效读取大小 4

来自分类Dev

大小为4的读取无效

来自分类Dev

cpp-valgrind-大小为8的无效读取

来自分类Dev

Valgrind-strcpy的大小为1的无效写入

来自分类Dev

Valgrind大小为8的神秘无效写入错误

来自分类Dev

cpp-valgrind-大小为8的无效读取

来自分类Dev

c Valgrind读取大小4无效->分段错误

来自分类Dev

读取的大小无效-C valgrind

来自分类Dev

分段错误,大小为4的无效写入

来自分类Dev

在线性时间中找到数组中大小为4的排序子序列

来自分类Dev

为什么Valgrind在PETSc中分配char *时报告大小写为8的无效写入?

来自分类Dev

Valgrind:抛出自定义异常时为“无效的读取大小1”

来自分类Dev

Valgrind错误:使用由struct和malloc构成的列表时,大小为8的无效写入

来自分类Dev

我在C中运行Valgrind时收到大小为8的无效读取

来自分类Dev

Valgrind:抛出自定义异常时,“读取大小为1无效”

来自分类Dev

大小8的无效读取,大小8(Valgrind)的无效写入

来自分类Dev

使用valgrind时无效读取大小1

来自分类Dev

C分段错误,大小为4的读取无效

来自分类Dev

Valgrind ...大小为8的块中的4个字节已释放

来自分类Dev

数组中大小为k的最小词典顺序

来自分类Dev

Valgrind无效读取4号错误?

来自分类Dev

使用Valgrind在C中无效读取大小8的分段错误

来自分类Dev

即使对于NULL分配,大小8 valgrind的无效写入

来自分类Dev

使用boost :: smatch在valgrind中读取大小无效1

来自分类Dev

valgrind中带有函数指针的读取大小无效

Related 相关文章

热门标签

归档