您好,我通过 valgrind 运行了我的程序,这是报告
堆摘要:退出时使用:1 个块中的 8 个字节总堆使用情况:1 个分配,0 个释放,已分配 8 个字节泄漏摘要:肯定丢失:1 个块中的 8 个字节
这是我的程序
int main() {
NodeData nodedata1(1, 'a');
List list1;
list1.insert(&nodedata1);
return 0;
}
//---my List class
class List {
public:
List();
bool insert(NodeData*); // insert one Node into list
bool isEmpty() const;
private:
struct Node { // the node in a linked list
NodeData* data; // pointer to actual data, operations in NodeData
Node* next;
};
Node* head; // pointer to first node in list
}; …Run Code Online (Sandbox Code Playgroud) ==17209== Conditional jump or move depends on uninitialised value(s)
==17209== at 0x402E7C5: __GI___rawmemchr (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
==17209== by 0x40CE921: _IO_str_init_static_internal (strops.c:45)
==17209== by 0x40B0B76: __isoc99_vsscanf (isoc99_vsscanf.c:42)
==17209== by 0x8048647: main (lala.c:23)
==17209== Uninitialised value was created by a stack allocation
==17209== at 0x8048659: gatherInfoSalt (lala.c:28)
==17209==
Run Code Online (Sandbox Code Playgroud)
。
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
struct sysinfo_s {
char *salt_id;
};
void gatherInfoSalt(char ** );
int main (int argc, char *argv[]) {
struct sysinfo_s si;
si.salt_id = …Run Code Online (Sandbox Code Playgroud) 我正在使用命令编译我的项目cmake .. -G "CodeBlocks - Ninja,它工作正常,但是当我运行 valgrind 时,它不显示任何行号。这是我正在使用的 valgrind 命令:valgrind --leak-check=full --show-leak-kinds=all --track-origins=yes --log-file="log.txt" ./pony_gpExe。
以下是输出的示例:
==5402== Use of uninitialised value of size 8
==5402== at 0x40447B: get_double_arr_length (in /home/robbie/Dropbox/MIT/pony_gp_ubuntu/build/pony_gpExe)
==5402== by 0x405184: get_test_and_train_data (in /home/robbie/Dropbox/MIT/pony_gp_ubuntu/build/pony_gpExe)
==5402== by 0x4027B3: setup (in /home/robbie/Dropbox/MIT/pony_gp_ubuntu/build/pony_gpExe)
==5402== by 0x402861: main (in /home/robbie/Dropbox/MIT/pony_gp_ubuntu/build/pony_gpExe)
==5402== Uninitialised value was created by a stack allocation
==5402== at 0x405149: get_test_and_train_data (in /home/robbie/Dropbox/MIT/pony_gp_ubuntu/build/pony_gpExe)
Run Code Online (Sandbox Code Playgroud)
不确定这是否有帮助,但这是我的主要 CMakeLists.txt:
cmake_minimum_required(VERSION 3.4.3)
project (pony_gp)
set(DIR ${pony_gp_SOURCE_DIR})
file(GLOB_RECURSE pony_gp_SOURCES "${DIR}/src/*.c")
file(GLOB_RECURSE pony_gp_HEADERS …Run Code Online (Sandbox Code Playgroud) output_s = (char**)malloc(sizeof(char*));
output_s[0] = (char*)malloc(sizeof(char));
output_s[0] = NULL;
Run Code Online (Sandbox Code Playgroud)
所以我想要创建的是一个 char 数组,通常是 char* 字符串的 char 数组,但对于这个特定的输入情况,我想创建一个仅包含 NULL 条目的数组,并且出于某种原因,当我创建我可以释放我的数组的条目,但无法释放设置为 null 的条目并且泄漏了 1 个字节。
free(result[0]);
free(result);
Run Code Online (Sandbox Code Playgroud)
稍后调用它来释放内存,结果是上面返回的 output_s 变量
==18402== HEAP SUMMARY:
==18402== in use at exit: 1 bytes in 1 blocks
==18402== total heap usage: 3 allocs, 2 frees, 1,033 bytes allocated
==18402==
==18402== 1 bytes in 1 blocks are definitely lost in loss record 1 of 1
==18402== at 0x4C2DB8F: malloc (in /usr/lib/valgrind/vgpreload_memcheck- amd64-linux.so)
==18402== by 0x4007D1: camel_caser (camelCaser.c:33)
==18402== …Run Code Online (Sandbox Code Playgroud) 据我了解,当代码包含未初始化变量的使用时,Valgrind 应该报告错误。在下面的玩具示例中,printer未初始化,但程序“愉快”地打印了消息。
#include <iostream>
class Printer {
public:
void print() {
std::cout<<"I PRINT"<<std::endl;
}
};
int main() {
Printer* printer;
printer->print();
};
Run Code Online (Sandbox Code Playgroud)
当我用 Valgrind 测试这个程序时,它没有报告任何错误。
这是预期的行为吗?如果是的话,为什么会这样?
我正在测试valgrind并有一个泄漏 4 个字节的小 C 程序:
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int* x = malloc(sizeof(int));
printf( "Address: %p\n", x);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我用: 编译它gcc -g -o leak leak.c,然后运行它:
$ leak
Address: 0x55a72e303260
$ leak
Address: 0x55f370273260
Run Code Online (Sandbox Code Playgroud)
因此它显示了两次单独运行的两个不同地址。但是,如果我在 valgrind 下运行它,它总是显示相同的地址0x4a66040::
$ valgrind --leak-check=full --show-leak-kinds=all leak
==8186== Memcheck, a memory error detector
==8186== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==8186== Using Valgrind-3.14.0 and LibVEX; rerun with -h for copyright info …Run Code Online (Sandbox Code Playgroud) 请看下面的代码
#include <stdio.h>
#include <pthread.h>
#include <assert.h>
#include <stdlib.h>
pthread_mutex_t g = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t m1 = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t m2 = PTHREAD_MUTEX_INITIALIZER;
void* worker(void* arg)
{
pthread_mutex_lock(&g);
if ((long long) arg == 0) {
pthread_mutex_lock(&m1);
pthread_mutex_lock(&m2);
} else {
pthread_mutex_lock(&m2);
pthread_mutex_lock(&m1);
}
pthread_mutex_unlock(&m1);
pthread_mutex_unlock(&m2);
pthread_mutex_unlock(&g);
return NULL;
}
int main(int argc, char *argv[]) {
pthread_t p1, p2;
pthread_create(&p1, NULL, worker, (void *) (long long) 0);
pthread_create(&p2, NULL, worker, (void *) (long long) 1);
pthread_join(p1, NULL);
pthread_join(p2, NULL);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
Helgrind …
我是编程新手,我正在编写一个程序,通过比较 4 个连续字节来恢复“card.raw”中存在的 JPEG 文件。如果他们划定一个 JPEG,程序必须将一个 512 字节的块复制到一个新文件中,保存为 xxx.jpg(000.jpg、001.jpg 等)。如果在复制块后,找到了新 JPEG 的开头,则将关闭当前文件并打开下一个文件以复制下一个 JPG。否则下一个块将被复制到同一个文件中。
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
int main(int argc, char *argv[])
{
int counter = 0;
if(argc != 2)
{
printf("Usage: ./recover image\n");
return 1;
}
typedef uint8_t BYTE;
FILE *recover = fopen(argv[1], "r");
if(recover == NULL)
{
printf("ERRNO 1 IS %s\n", strerror(errno));
exit(EXIT_FAILURE);
}
fseek(recover, 0L, SEEK_END);
unsigned long size = ftell(recover);
fseek(recover, 0L, SEEK_SET);
BYTE *CHUNK = (BYTE*)malloc(size * …Run Code Online (Sandbox Code Playgroud) 当我运行 Valgrind 时出现以下错误,我尝试释放所有使用的函数,但仍然有相同的错误消息
==303912== HEAP SUMMARY:
==303912== in use at exit: 348 bytes in 2 blocks
==303912== total heap usage: 1,192 allocs, 1,190 frees, 153,918 bytes allocated
==303912==
==303912== 348 bytes in 2 blocks are still reachable in loss record 1 of 1
==303912== at 0x483B7F3: malloc (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==303912== by 0x490050E: strdup (strdup.c:42)
==303912== by 0x109B8E: main (minishell.c:178)
==303912==
==303912== LEAK SUMMARY:
==303912== definitely lost: 0 bytes in 0 blocks
==303912== indirectly lost: 0 bytes in 0 blocks
==303912== …Run Code Online (Sandbox Code Playgroud) 在这个创建列表对象的小函数上
typedef struct list {
list_item* head;
list_item* tail;
int count;
pthread_mutex_t* mutex;
} list;
list* createList(){
list* list = calloc(1, sizeof(list));
list->mutex = calloc(1, sizeof(pthread_mutex_t));
pthread_mutex_init(list->mutex, NULL);
return list;
}
Run Code Online (Sandbox Code Playgroud)
当我运行 valgrind 时,我有这个输出
==8362== Invalid write of size 4
==8362== at 0x11A30: createList (linkedlist.c:8)
==8362== by 0x11203: create_new_game (engine.c:82)
==8362== by 0x10817: createGame (check_engine.c:29)
==8362== by 0x10F43: test_cropping (check_engine.c:146)
==8362== by 0x10F6F: main (check_engine.c:152)
==8362== Address 0x4a65514 is 8 bytes after a block of size 4 alloc'd
==8362== at …Run Code Online (Sandbox Code Playgroud)