我有这些结构:
typedef struct _Frag{
struct _Frag *next;
char *seq;
int x1;
int length;
}Frag;
typedef struct _Fragment{
int type;
Frag *frag_list;
}Fragment;
Run Code Online (Sandbox Code Playgroud)
然后我创建了一个数组
Fragment *fragments=malloc(1,sizeof(Fragment)); // or more
fragments->frag_list=malloc(1,sizeof(Frag)); // or more
Frag *frag=malloc(10,sizeof(Frag));
frag->seq="test str\n";
...
frag->next=malloc(1,sizeof(Frag));
frag->next->seq="test str\n";
Run Code Online (Sandbox Code Playgroud)
在程序结束时,我想释放内存,功能是:
static void free_frags(){
int i;
Fragment *fragment;
Frag *current,*next;
for(i=0;i<1;i++){
fragment=&snp_frags[i];
current=fragment->frag_list;
next=current->next;
while(next!=NULL){
free(current->seq);
//free(current->next);
free(current);
current=next;
next=current->next;
}
free(current->seq);
//free(current->next);
free(current);
//free(fragment->frag_list);
free(&snp_frags[i]);
}
free(snp_frags);
}
Run Code Online (Sandbox Code Playgroud)
如果我使用valgrind来调试它,valgrind说:
=============================================
==3810== Invalid read of size 4
==3810== at 0x80490FD: …Run Code Online (Sandbox Code Playgroud) 最后编辑在OP结束
我用Valgrind测试了一个项目中使用的函数,它说"memcpy中的源和目标重叠",并且还给出了"无效读取"和"无效写入"错误.我修复了代码,以便不重叠这两个缓冲区但没有结果.这是代码:
static
int download_build_buffer(char **seq_numbered_buffer, int seq_n, int dim_payload, char *buffer) {
if(!seq_numbered_buffer)
return 1;
/* allocates seq_numbered_buffer */
if(*seq_numbered_buffer != NULL) {
free(*seq_numbered_buffer);
*seq_numbered_buffer = NULL;
}
if(!(*seq_numbered_buffer = malloc((SIZE_SEQ_N + SIZE_DIM_S + dim_payload + 1) * sizeof(char))))
return 1;
#if DEBUG_DOWNLOAD
fprintf(stderr, "download_build_buffer %d: seq->%d, dim->%d\n",
getpid(), seq_n, dim_payload);
#endif
/* prints sequence number in its string */
seq_n = htonl(seq_n);
if(!memcpy(*seq_numbered_buffer, &seq_n, SIZE_SEQ_N))
return 1;
dim_payload = htonl(dim_payload);
if(!memcpy(&(*seq_numbered_buffer)[SIZE_SEQ_N], &dim_payload, SIZE_DIM_S))
return 1;
/* creates payload …Run Code Online (Sandbox Code Playgroud) 在以下代码中我收到一个错误:
333 glGenBuffers(surftotal, uiVBO);
334 {
335 for(surfnum=0; surfnum<surftotal; ++surfnum)
336 {
337 glBindBuffer(GL_ARRAY_BUFFER, uiVBO[surfnum]);
338 size_t buf_size = 9*sizeof(GLfloat)*triNum[surfnum];
339 GLfloat* const pData = (GLfloat*)malloc(buf_size);
340 for(i=0; i<triNum[surfnum]; ++i)
341 printf("%d...",triNum[surfnum]);
342 {
343 memcpy(pData+i*9, triArray[surfnum][i].pt1, 3*sizeof(GLfloat));
344 memcpy(pData+i*9+3, triArray[surfnum][i].pt2, 3*sizeof(GLfloat));
345 memcpy(pData+i*9+6, triArray[surfnum][i].pt3, 3*sizeof(GLfloat));
346 }
347 glBufferData(GL_ARRAY_BUFFER, buf_size, pData, GL_STATIC_DRAW);
348 free(pData);
349 }
350 glBindBuffer(GL_ARRAY_BUFFER, 0);
351 glEnableVertexAttribArray(VERTEX_ARRAY);
352 for(surfnum=0; surfnum<surftotal; ++surfnum)
353 {
354 glBindBuffer(GL_ARRAY_BUFFER, uiVBO[surfnum]);
355 glVertexAttribPointer(VERTEX_ARRAY, 3, GL_FLOAT, GL_FALSE, 0, 0);
356 glDrawArrays(GL_TRIANGLES, …Run Code Online (Sandbox Code Playgroud) 我是Valgrind的新手(我的C/C++生锈了)我收到一个错误:
40 bytes in 1 blocks are definitely lost in loss record 35 of 111
==26930== at 0x4C275C2: operator new(unsigned long) (vg_replace_malloc.c:261)
==26930== by 0x5EFAFDB: cassie_init_with_timeout (cassie.cc:49)
==26930== by 0x46E647: ngx_cassandra_upstream_get_peer (ngx_cassandra_upstream.c:274)
==26930== by 0x41E00B: ngx_event_connect_peer (ngx_event_connect.c:25)
Run Code Online (Sandbox Code Playgroud)
我猜char*last_error_string给了我适合,但我如何追踪它呢?
这是我的来源:
创建Cassie对象:
cassie_t cassie;
char *error = NULL;
cassie = new Cassie(host,port,error,CASSIE_ERROR_NONE); /* this is line 49 in the above valgrind trace */
cassie->cassandra = cassandra;
Run Code Online (Sandbox Code Playgroud)
cassie_t是Object的结构.
typedef struct Cassie *cassie_t;
Run Code Online (Sandbox Code Playgroud)
我有这个,因为我正在包装一个C++ lib,以便从C调用它.
这是我们的对象cassie_private.h
#include <string>
#include "libcassandra/cassandra.h"
#include "libcassie/cassie.h"
#ifdef __cplusplus …Run Code Online (Sandbox Code Playgroud) 我正在编写我的第一个大c ++项目,但在运行程序时遇到了分段错误.我已经尝试使用valgrind调试它,但到目前为止还没有成功.由于程序非常大,我只会显示发生错误的相关函数:
void RigidBody::RotateAroundAxis( dReal Angle,
dRealVector3 Axis,
dRealVector3 Anchor)
{
std::cout<<"RotateAroundAxis BodyID: " << this->GetBodyId()<<std::endl;
dRealMatrix3 RotationMatrix=HelperFunctions::RotateAroundAxis(Angle, Axis);
dRealMatrix3 CurrentRotation=this->GetRotation();
dRealVector3 Position=this->GetPosition();
dRealMatrix3 newRotationMatrix=boost::numeric::ublas::prod(RotationMatrix,CurrentRotation);
std::cout<<"RotateAroundAxis (Debug0) BodyID: " << this->GetBodyId()<<std::endl;
SetRotation(newRotationMatrix);
std::cout<<"RotateAroundAxis Debug"<<std::endl;// This is the last line that is processed without an error.
std::cout<<"RotateAroundAxis (Debug1) BodyID: " << this->GetBodyId()<<std::endl; // This line probably causes the error.
// object is now rotated but needs to be translated
boost::numeric::ublas::bounded_vector<dReal,3> PosRelToAnchor;
std::cout<<"RotateAroundAxis (Debug2) BodyID: " << this->GetBodyId()<<std::endl;
for(unsigned int i=0;i<3;i++){PosRelToAnchor[i]=Position[i]-Anchor[i];};
PosRelToAnchor=boost::numeric::ublas::prod(RotationMatrix,PosRelToAnchor); …Run Code Online (Sandbox Code Playgroud) 我正在学习C并经历一个包含valgrind使用的教程.我还在了解valgrind实际上在做什么,并想知道是否有人可以解释为什么它没有检测到以下代码中的任何错误:
#include <stdio.h>
int main(int argc, char *argv[])
{
int numbers[4] = {0,1,2,3,4};
// first, print them out raw
printf("numbers: %d %d %d %d %d\n",
numbers[0], numbers[1],
numbers[2], numbers[3],
numbers[4]);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我确实遇到编译器错误:
greggery@Lubu:~/code$ make lc
cc -Wall -g lc.c -o lc
lc.c: In function ‘main’:
lc.c:5:2: warning: excess elements in array initializer [enabled by default]
lc.c:5:2: warning: (near initialization for ‘numbers’) [enabled by default]
Run Code Online (Sandbox Code Playgroud)
但是当我对着valgrind运行时,它没有看到任何错误:
==2300== Memcheck, a memory error detector
==2300== Copyright (C) 2002-2012, and GNU GPL'd, …Run Code Online (Sandbox Code Playgroud) 我有一个valgrind错误,我不知道如何摆脱它们:
==5685== Invalid read of size 8
==5685== at 0x4008A1: main (in /home/mazix/Desktop/tests/filenames)
==5685== Address 0x5207670 is 608 bytes inside a block of size 609 alloc'd
==5685== at 0x4C2CD7B: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==5685== by 0x40075E: getFilenames (in /home/mazix/Desktop/tests/filenames)
==5685== by 0x40083C: main (in /home/mazix/Desktop/tests/filenames)
==5685==
==5685== Invalid read of size 8
==5685== at 0x4008BB: main (in /home/mazix/Desktop/tests/filenames)
==5685== Address 0x5207670 is 608 bytes inside a block of size 609 alloc'd
==5685== at 0x4C2CD7B: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==5685== by 0x40075E: getFilenames …Run Code Online (Sandbox Code Playgroud) 我已经遇到了这个问题几天了,并且已经做了一些解决方法,但我需要了解我哪里出错了.
这是valgrind错误,导致分段错误和崩溃
==14609== Invalid read of size 1
==14609== at 0x4E80F90: vfprintf (vfprintf.c:1655)
==14609== by 0x4E87F56: fprintf (fprintf.c:32)
==14609== by 0x4017ED: display_tickets (tm_options.c:261)
==14609== by 0x400E5D: main (tm.c:83)
==14609== Address 0xa is not stack'd, malloc'd or (recently) free'd
Run Code Online (Sandbox Code Playgroud)
我试图运行的代码非常简单.
void display_tickets(tm_type *tm) {
struct stock_data data;
struct stock_node *current;
memcpy(&data, tm->stock->head_stock->data, sizeof(tm->stock->head_stock->data));
printf("%s", data.ticket_name); /*THIS WORKS, name is as expected*/
fprintf(stdout, "Name is %s", 40, data.ticket_name); /*this causes the read error*/
}
Run Code Online (Sandbox Code Playgroud)
ticket_name只是一个字符串(大小为40),它是"struct stock_data"的成员.
任何人都可以解释它正在做我的头脑....
我正在尝试编写一个用于井字游戏的蒙特卡罗树搜索,但是在运行它时会出现真正的内存问题(例如我的计算机内存不足).
所以我决定调查情况valgrind.
下面是一个代码块,上面valgrind写着"绝对泄漏".
void player_init (Player **p, player_t symbol)
{
*p = (Player *) malloc (sizeof (Player));
(**p).symbol = symbol;
(**p).score = 0;
}
void player_init_all (Player ***p)
{
*p = (Player **) malloc (sizeof (Player *) * 2);
for (int i = 0; i < 2; i++)
{
(*p)[i] = (Player *) malloc (sizeof (Player));
}
player_init (&(*p)[0], PLAYER1);
player_init (&(*p)[1], PLAYER2);
}
void player_destroy (Player *p)
{
free (p);
}
Run Code Online (Sandbox Code Playgroud)
在哪里Player和player_t …
我写了一个程序,用五个字符串串起一个字符串.这是我的计划.
struct list
{
char *str;
struct list* next;
};
struct list* head = NULL;
void insert(char *cont)
{
struct list* temp = (struct list*)malloc(sizeof(struct list));
size_t len = strlen(cont);
char *heapString = (char*)malloc(len);
strcpy(heapString,cont);
temp->str = heapString;
temp->next = NULL;
if(head == NULL)
{
head = temp;
return ;
}
temp->next = head;
head = temp;
}
void print()
{
struct list* temp = head;
while(temp != NULL)
{
printf("%s\n",temp->str);
temp = temp->next;
}
}
void clearmem()
{
struct …Run Code Online (Sandbox Code Playgroud)