下一个代码使用igraph_neighborhood,它运行良好,我不知道为什么我得到
#include <igraph.h>
#include <fstream>
#include <iostream>
#include <stdlib.h>
#include <vector>
#include <algorithm>
void print_vector(igraph_vector_t *v, FILE *f) {
long int i;
for (i=0; i<igraph_vector_size(v); i++) {
fprintf(f, " %li", (long int) VECTOR(*v)[i]);
}
fprintf(f, "\n");
}
using namespace std;
int main() {
igraph_t g;
igraph_vector_t v;
igraph_vector_init(&v,32);
VECTOR(v)[0]=0; VECTOR(v)[1]=1;
VECTOR(v)[2]=0; VECTOR(v)[3]=4;
VECTOR(v)[4]=0; VECTOR(v)[5]=9;
VECTOR(v)[6]=1; VECTOR(v)[7]=7;
VECTOR(v)[8]=1; VECTOR(v)[9]=9;
VECTOR(v)[10]=2; VECTOR(v)[11]=9;
VECTOR(v)[12]=2; VECTOR(v)[13]=3;
VECTOR(v)[14]=2; VECTOR(v)[15]=5;
VECTOR(v)[16]=3; VECTOR(v)[17]=6;
VECTOR(v)[18]=3; VECTOR(v)[19]=9;
VECTOR(v)[20]=4; VECTOR(v)[21]=5;
VECTOR(v)[22]=4; VECTOR(v)[23]=8;
VECTOR(v)[24]=5; VECTOR(v)[25]=8;
VECTOR(v)[26]=6; VECTOR(v)[27]=7;
VECTOR(v)[28]=6; VECTOR(v)[29]=8;
VECTOR(v)[30]=7; VECTOR(v)[31]=8;
igraph_create(&g, &v, 0,0);
// Cycle 1
igraph_vector_ptr_t vn1;
igraph_vs_t vs;
long int i;
igraph_vector_ptr_init(&vn1, 10);
for (i=0; i<igraph_vector_ptr_size(&vn1); i++) {
VECTOR(vn1)[i] = calloc(1, sizeof(igraph_vector_t));
igraph_vector_init((igraph_vector_t *)VECTOR(vn1)[i], 0);
}
igraph_vs_vector_small(&vs, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1);
igraph_neighborhood(&g, &vn1,vs, 1, IGRAPH_OUT);
for (i=0; i<igraph_vector_ptr_size(&vn1); i++) {
print_vector((igraph_vector_t *)VECTOR(vn1)[i],stdout);
igraph_vector_destroy((igraph_vector_t *)VECTOR(vn1)[i]);
free((igraph_vector_t *)VECTOR(vn1)[i]);
}
igraph_vector_ptr_destroy(&vn1);
igraph_vs_destroy(&vs);
igraph_destroy(&g);
if (!IGRAPH_FINALLY_STACK_EMPTY) return 1;
return 0;
}
它运行良好,但我有下一个输出Valgrind: 它运行良好,但我有下一个输出Valgrind: 它运行良好,但我有下一个输出Valgrind:
==13517== Memcheck, a memory error detector
==13517== Copyright (C) 2002-2009, and GNU GPL'd, by Julian Seward et al.
==13517== Using Valgrind-3.6.0.SVN-Debian and LibVEX; rerun with -h for copyright info
==13517== Command: ./example
==13517==
--13517-- Valgrind options:
--13517-- --suppressions=/usr/lib/valgrind/debian-libc6-dbg.supp
--13517-- --leak-check=full
--13517-- -v
--13517-- Contents of /proc/version:
--13517-- Linux version 2.6.32-35-generic-pae (buildd@vernadsky) (gcc version 4.4.3 (Ubuntu 4.4.3-4ubuntu5) ) #78-Ubuntu SMP Tue Oct 11 17:01:12 UTC 2011
--13517-- Arch and hwcaps: X86, x86-sse1-sse2
--13517-- Page sizes: currently 4096, max supported 4096
--13517-- Valgrind library directory: /usr/lib/valgrind
--13517-- Reading syms from /lib/ld-2.11.1.so (0x4000000)
--13517-- Reading debug info from /lib/ld-2.11.1.so ..
--13517-- .. CRC mismatch (computed 45f50ae1 wanted 137bc614)
--13517-- Reading debug info from /usr/lib/debug/lib/ld-2.11.1.so ..
--13517-- Reading syms from /home/rvaca/workSpalceIgraphCpp/proyIgraph/example (0x8048000)
--13517-- Reading syms from /usr/lib/valgrind/memcheck-x86-linux (0x38000000)
--13517-- object doesn't have a dynamic symbol table
--13517-- Reading suppressions file: /usr/lib/valgrind/debian-libc6-dbg.supp
--13517-- Reading suppressions file: /usr/lib/valgrind/default.supp
--13517-- REDIR: 0x40160b0 (index) redirected to 0x3803e9b3 (vgPlain_x86_linux_REDIR_FOR_index)
--13517-- Reading syms from /usr/lib/valgrind/vgpreload_core-x86-linux.so (0x401f000)
--13517-- Reading syms from /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so (0x4022000)
==13517== WARNING: new redirection conflicts with existing -- ignoring it
--13517-- new: 0x040160b0 (index ) R-> 0x04025c30 index
--13517-- REDIR: 0x4016290 (strlen) redirected to 0x4026070 (strlen)
--13517-- Reading syms from /usr/local/lib/libigraph.so.0.0.0 (0x402b000)
--13517-- Reading syms from /usr/lib/libstdc++.so.6.0.13 (0x4213000)
--13517-- object doesn't have a symbol table
--13517-- Reading syms from /lib/tls/i686/cmov/libm-2.11.1.so (0x4308000)
--13517-- Reading debug info from /lib/tls/i686/cmov/libm-2.11.1.so ..
--13517-- .. CRC mismatch (computed a765a73c wanted 8d13be57)
--13517-- Reading debug info from /usr/lib/debug/lib/tls/i686/cmov/libm-2.11.1.so ..
--13517-- Reading syms from /lib/libgcc_s.so.1 (0x432e000)
--13517-- Reading debug info from /lib/libgcc_s.so.1 ..
--13517-- .. CRC mismatch (computed 338494ad wanted b1d69d39)
--13517-- object doesn't have a symbol table
--13517-- Reading syms from /lib/tls/i686/cmov/libc-2.11.1.so (0x434d000)
--13517-- Reading debug info from /lib/tls/i686/cmov/libc-2.11.1.so ..
--13517-- .. CRC mismatch (computed 2236eb0a wanted a071c0c3)
--13517-- Reading debug info from /usr/lib/debug/lib/tls/i686/cmov/libc-2.11.1.so ..
--13517-- Reading syms from /usr/lib/libxml2.so.2.7.6 (0x44a8000)
--13517-- Reading debug info from /usr/lib/libxml2.so.2.7.6 ..
--13517-- .. CRC mismatch (computed 8c0a5cb7 wanted d9f96831)
--13517-- object doesn't have a symbol table
--13517-- Reading syms from /usr/lib/libgmp.so.3.5.2 (0x45d3000)
--13517-- Reading debug info from /usr/lib/libgmp.so.3.5.2 ..
--13517-- .. CRC mismatch (computed b9cf7786 wanted 4db0a849)
--13517-- object doesn't have a symbol table
--13517-- Reading syms from /lib/tls/i686/cmov/libdl-2.11.1.so (0x4634000)
--13517-- Reading debug info from /lib/tls/i686/cmov/libdl-2.11.1.so ..
--13517-- .. CRC mismatch (computed 10f03463 wanted f15a6db6)
--13517-- Reading debug info from /usr/lib/debug/lib/tls/i686/cmov/libdl-2.11.1.so ..
--13517-- Reading syms from /lib/libz.so.1.2.3.3 (0x4638000)
--13517-- Reading debug info from /lib/libz.so.1.2.3.3 ..
--13517-- .. CRC mismatch (computed 53909dc3 wanted dc0e37c9)
--13517-- object doesn't have a symbol table
--13517-- REDIR: 0x43c0b10 (rindex) redirected to 0x4025ae0 (rindex)
--13517-- REDIR: 0x43c07f0 (__GI_strlen) redirected to 0x4026050 (__GI_strlen)
--13517-- REDIR: 0x43bc660 (calloc) redirected to 0x402417f (calloc)
--13517-- REDIR: 0x43bdec0 (realloc) redirected to 0x4024f4a (realloc)
--13517-- REDIR: 0x43c1930 (memset) redirected to 0x401f44c (_vgnU_ifunc_wrapper)
--13517-- REDIR: 0x445bef0 (__memset_sse2) redirected to 0x4027420 (memset)
--13517-- REDIR: 0x43bce60 (free) redirected to 0x4024ab5 (free)
--13517-- REDIR: 0x43c1f20 (memcpy) redirected to 0x401f44c (_vgnU_ifunc_wrapper)
--13517-- REDIR: 0x445d000 (__memcpy_ssse3) redirected to 0x40267f0 (memcpy)
--13517-- REDIR: 0x43c32b0 (strchrnul) redirected to 0x4027510 (strchrnul)
0 1 4 9
1 0 7 9
2 3 5 9
3 2 6 9
4 0 5 8
5 2 4 8
6 3 7 8
7 1 6 8
8 4 5 6 7
9 0 1 2 3
==13517==
==13517== HEAP SUMMARY:
==13517== in use at exit: 456 bytes in 21 blocks
==13517== total heap usage: 73 allocs, 52 frees, 3,428 bytes allocated
==13517==
==13517== Searching for pointers to 21 not-freed blocks
==13517== Checked 425,428 bytes
==13517==
==13517== 200 (120 direct, 80 indirect) bytes in 10 blocks are definitely lost in loss record 2 of 3
==13517== at 0x402425F: calloc (vg_replace_malloc.c:467)
==13517== by 0x8048DF7: main (startdIII.cpp:76)
==13517==
==13517== 256 bytes in 1 blocks are definitely lost in loss record 3 of 3
==13517== at 0x402425F: calloc (vg_replace_malloc.c:467)
==13517== by 0x40C9A78: igraph_vector_init (vector.pmt:123)
==13517== by 0x8048B43: main (startdIII.cpp:49)
==13517==
==13517== LEAK SUMMARY:
==13517== definitely lost: 376 bytes in 11 blocks
==13517== indirectly lost: 80 bytes in 10 blocks
==13517== possibly lost: 0 bytes in 0 blocks
==13517== still reachable: 0 bytes in 0 blocks
==13517== suppressed: 0 bytes in 0 blocks
==13517==
==13517== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 27 from 6)
--13517--
--13517-- used_suppression: 27 dl-hack3-cond-1
==13517==
==13517== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 27 from 6)
怎么了?我一直在尝试不同的方式,但它继续相同的输出。
感谢提前
vacing
答案 0 :(得分:1)
你的矢量v
永远不会被摧毁 - 这就是你在Valgrind得到第二个警告的原因。
您必须将初始化但空igraph_vector_ptr_t
传递给igraph_neighborhood
,否则它将简单地删除已存在的指针(和向量)并分配新的指针。这是因为igraph_neighborhood
不知道如何分配igraph_vector_ptr_t
中的指针以及应该在它们上调用什么析构函数(如果有的话),所以它试图做的损害最小并简单地忽略它们。因此,无需在第一个for循环中使用向量预加载igraph_vector_ptr_t
。这摆脱了第一次警告。