标签: valgrind

Valgrind /内存错误

当我使用valgrind时,我在代码中反复收到以下错误.我不太确定这些意思是什么,我无法确定未初始化的值.

==16795== Conditional jump or move depends on uninitialised value(s)
==16795==    at 0x4A06E8A: strcmp (mc_replace_strmem.c:412)
==16795==    by 0x4009C7: dictionary_add (libdictionary.c:44)
==16795==    by 0x40061B: main (part2.c:28)
==16795== 
==16795== Invalid write of size 1
==16795==    at 0x4A082E7: strcpy (mc_replace_strmem.c:303)
==16795==    by 0x400AA8: dictionary_add (libdictionary.c:57)
==16795==    by 0x40061B: main (part2.c:28)
==16795==  Address 0x4c361a3 is 0 bytes after a block of size 3 alloc'd
==16795==    at 0x4A05E1C: malloc (vg_replace_malloc.c:195)
==16795==    by 0x400931: node_newnode (libdictionary.c:28)
==16795==    by 0x400A8C: dictionary_add (libdictionary.c:54)
==16795==    by 0x40061B: main (part2.c:28) …
Run Code Online (Sandbox Code Playgroud)

c memory valgrind linked-list list

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

函数中的malloc()结构,结束程序前的free()

我正在学习C的基础知识,现在正在使用malloc().假设我有一个函数要求用户输入,用一个数据填充一个结构,并保存在一个数组中(所有结构都通过main函数引用传递).

我通过Valgrind运行程序,我得到"仍然可以访问"的字节(这应该是正确的?不被认为是泄漏),但是任何保存的数据我都会丢失块.

程序运行完成后,如何释放内存?此外,我在代码中有一些(2)问题只是为了澄清一些事情,如果有人可以向我解释,我会很感激.

这是一些类似于我想要做的代码:

我声明了以下结构:

struct Person {
  char name[MAX_INPUT];
  int age;
};
Run Code Online (Sandbox Code Playgroud)

我正在写一个像这样的函数:

int function2(struct Person *list, int *index) {
  struct Person *prsn = malloc(sizeof(struct Person)); 
  // !Why do we sometimes cast the malloc or not? 
  // I sometimes get errors when I do, sometimes when I don't, 
  // while the surrounding code is pretty much the same.
  assert(prsn != NULL);

  // User input code goes here ... 

  // Now to save the Person created
  strcpy(prsn->name, nameInput);
  prsn->age = …
Run Code Online (Sandbox Code Playgroud)

c malloc valgrind pointers

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

c:valgrind"条件跳转或移动取决于未初始化的值"

我在valgrind中遇到错误,不知道出了什么问题.错误是:valgrind输出:

==1112== Conditional jump or move depends on uninitialised value(s)
==1112==    at 0x402BF0D: realloc (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
Run Code Online (Sandbox Code Playgroud)

并且它表明问题发生在第226行:

if(reallocate ==TRUE)
{
    char** temp_values = NULL;


    temp_values = (char**) realloc(theBoard->_values, theBoard->_size_r*sizeof(char*) );

    if(temp_values!=NULL)
    {
        theBoard->_values = temp_values;
    } else
    {
        reportError(MEM_OUT);
        return FALSE;
    }

    int i = 0;
    for (i=0; i<theBoard->_size_r; i++)
    {
        char* temp_values_c = NULL;
HERE( line 226)->   temp_values_c = realloc(theBoard->_values[i], theBoard->_size_c*sizeof(char) );

        if(temp_values_c != NULL)
        {
            theBoard->_values[i] = temp_values_c;
        } else
        {
            reportError(MEM_OUT);
            return FALSE;
        }
    }

    // initialize …
Run Code Online (Sandbox Code Playgroud)

c valgrind

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

读取大小4无效

我有以下程序:

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

typedef struct a{
    int a;
} Player;

typedef struct b{
    Player players[5];
}* Formation;

int main()
{
    Player a; a.a = 1;
    Player b; b.a = 2;
    Player c; c.a = 3;
    Player d; d.a = 4;
    Player e; e.a = 5;

    Formation team = malloc(sizeof(*team));
    team->players[0] = a;
    team->players[1] = b;
    team->players[2] = c;
    team->players[3] = d;
    team->players[4] = e;

    for (int i = 0; i < 5; i++){
        team->players[i] = team->players[i + 1]; …
Run Code Online (Sandbox Code Playgroud)

c valgrind

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

为什么valgrind显示泄漏,即使释放包含动态分配对象的向量?

我使用new在堆上分配了一个向量:

std::vector<t*> *vec = new std::vector<t*>;
Run Code Online (Sandbox Code Playgroud)

此向量包含类"t"的类对象,这些类对象是使用new创建的

t *ptr1 = new t();
t *ptr2 = new t();
t *ptr3 = new t();
t *ptr4 = new t();
Run Code Online (Sandbox Code Playgroud)

现在,当我删除矢量时,预计添加到它的所有这些对象也应该被销毁,我的意思是:

std::vector<t*> *vec = new std::vector<t*>;
 vec->push_back(ptr1);
 vec->push_back(ptr2);
 vec->push_back(ptr3);
 vec->push_back(ptr4);
Run Code Online (Sandbox Code Playgroud)

ptr1指向的内存,ptr2,ptr3,ptr4也应该被释放.

但是Valgrind将此视为泄漏!!! Valgrind有问题吗?

==15634==
==15634== HEAP SUMMARY:
==15634==     in use at exit: 48 bytes in 8 blocks
==15634==   total heap usage: 12 allocs, 4 frees, 128 bytes allocated
==15634==
==15634== LEAK SUMMARY:
==15634==    definitely lost: 32 bytes in 4 blocks
==15634==    indirectly lost: …
Run Code Online (Sandbox Code Playgroud)

c++ linux valgrind vector

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

使用calloc和free后内存泄漏?

我用"valgrind --leak-check = full"测试了我的软件,它显示:

==90862== 7,627 bytes in 4 blocks are definitely lost in loss record 858 of 897
==90862==    at 0x4C2FB55: calloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==90862==    by 0xD64991C: concat(int, ...) (Client.cpp:150)
Run Code Online (Sandbox Code Playgroud)

我无法理解为什么,因为我在calloc之后使用free().这是我的代码:

char* p = concat(2, buffOld, buff);
char *x;
    while(true) {
        x = p;
        p = strstr(p,"\\final\\");
        if(p == NULL) { break; }
        *p = 0;
        p+=7;
        parseIncoming((char *)x,strlen(x));
    }
free(p);
Run Code Online (Sandbox Code Playgroud)

而"concat"功能:

char* concat(int count, ...)
{
    va_list ap;
    int i;

    // Find required length to store merged string …
Run Code Online (Sandbox Code Playgroud)

c valgrind memory-leaks

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

删除堆后,对Valgrind错误感到困惑

我有一段非常简单的代码:

#include <iostream>

int main()
{
    int *val = new int;
    *val = 12;
    std::cout << *val << std::endl;
    delete &val;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

当我在这上面运行Valgrind时,我收到以下错误:

SUMMARY: 3 errors from 3 contexts (suppressed: 8 from 8)

1 errors in context 1 of 3:
Invalid free() / delete / delete[] / realloc()
    at 0x1000ABB6D: free (vg_replace_malloc.c:533)
    by 0x100000D1E: main (pointers.cpp:8)
  Address 0x1048a09f0 is on thread 1's stack
  in frame #1, created by main (pointers.cpp:4)
Run Code Online (Sandbox Code Playgroud)

我如何删除有val什么问题?

c++ valgrind c++11

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

为什么内存泄漏在一个案例而不是另一个案件中

我正在创建一个c++具有两种略有不同的方式的对象,在以下代码CASE0存在内存泄漏,但在这种else情况下没有内存泄漏.

#include <string>
#define CASE 1

class A {
private:
  std::string *s;
public:
  A(std::string *p_s) { s = p_s; }
};

int main() {
#if CASE==0
  auto a = A(new std::string("Hello"));
#else
  auto s = std::string("Hello");
  auto a = A(&s);
#endif
}
Run Code Online (Sandbox Code Playgroud)

当我设置CASE 0valgrind说有内存泄漏

valgrind ./a.out 
==24351== Memcheck, a memory error detector
==24351== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==24351== Using Valgrind-3.13.0 and …
Run Code Online (Sandbox Code Playgroud)

c++ valgrind memory-leaks

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

如何在C中为链表头分配空间?

    struct node {
        int data;
        struct node *next;
    };

    int main() {

        struct node *head = malloc(sizeof(struct node));
        struct node *current = head;
        ...
    };
Run Code Online (Sandbox Code Playgroud)

虽然这段代码可以在没有任何警告或错误的情况下运行,但Valgrind会给出一些消息Conditional jump or move depends on uninitialised value(s),Uninitialised value was created by a heap allocation

我无法弄清楚出了什么问题.我们在node函数外定义了一个struct main.所以我认为我们可以使用sizeof(struct node),不是吗?

c valgrind linked-list

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

我不明白为什么我肯定会丢失内存块

作为一个项目,我正在创建一个学生数据库。但是根据valgrind,我的程序中存在内存泄漏,我不知道为什么。我真的不能说太多:我不明白为什么记忆肯定会丢失。

学生结构:

typedef struct {
    char student_number[7];
    char *first_name;
    char *last_name;
    int round_points[6];
} Student;
Run Code Online (Sandbox Code Playgroud)

免责声明:我正在使用gcc选项-std=c99,因此必须实现自己的strdup()

重要的代码段:

char *copy_string(const char *string) {
    int len = strlen(string);
    char *copy = calloc(len + 1, sizeof(char));
    if (copy == NULL)
        return NULL;
    strcpy(copy, string);
    /* copy[len] = '\0'; */
    return copy;
}

char **parse_one_line_params(const char *one_line, int param_count) {
    char *copy = copy_string(one_line);
    if (copy == NULL)
        return NULL;
    //copy_start is used to free the copy string in the …
Run Code Online (Sandbox Code Playgroud)

c valgrind memory-leaks memory-management

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