Hello all.
I'm just a noob linux programmer and am trying to debug a
complex application that gives me all sorts of errors.
Since i'm buried
deep in pointers I put up a stupid program just to play around with
pointers and valgrind's error reporting.
To understand things better.
The full source code is at the end of this email.
Lines 19, 20 and 22
are partially commented out.
If i use the full declaration, valgrind
reports this:
==4519== 16 bytes in 1 blocks are definitely lost in
loss record 1 of 6
==4519== at 0x4024F20: malloc (vg_replace_malloc.
c:236)
==4519== by 0x8048759: main (pointers.c:19)
==4519==
==4519== 16 bytes in 1 blocks are definitely lost in loss record 2 of 6
==4519== at 0x4024F20: malloc (vg_replace_malloc.c:236)
==4519==
by 0x8048769: main (pointers.c:20)
==4519==
==4519== 1,480 (40 direct,
1,440 indirect) bytes in 1 blocks are definitely lost in loss record 3
of 3
==4519== at 0x4024F20: malloc (vg_replace_malloc.c:236)
==4519== by 0x80486F9: main (pointers.c:22)
Which i don't
understand.
If I do not explicitly initialize the pointers, accessing
any element of the structure results in an error.
This should mean
(according to my knowledge and understanding) that the real structure
has not been allocated (which should be the case since only the pointer
has been initialized). But the valgrind report seems to say the
contrary.
If I comment the explicit declarations (like you find in the
source below) another error comes out
==4472== 160 (16 direct, 144
indirect) bytes in 1 blocks are definitely lost in loss record 4 of 5
==4472== at 0x4024F20: malloc (vg_replace_malloc.c:236)
==4472==
by 0x8048710: main (pointers.c:24)
I don't understand how is it
possibile to loose memory by allocating new pointers.
I loose if i
reallocate an already existing pointer, then the memory previously
pointed will be lost.
But what is this?
I have very big problems i
can't seem to pinpoint.
Let's start here and see if i can understand
them.
Thank you very much
Claudio Carbone
#include <stdlib.h>
#include <stdio.h>
typedef struct map_cell_vector {
int px;
int
py;
float distance;
struct map_cell_vector *next;
}
map_cell_vector;
void free_vector (map_cell_vector *ptr);
int main
(int argc, const char **argv) {
int i,j;
map_cell_vector *ptr1;
//=malloc (sizeof(map_cell_vector));
map_cell_vector *ptr2; //=malloc
(sizeof(map_cell_vector));
map_cell_vector *ptr3;
map_cell_vector
**array; // = (map_cell_vector**) malloc (10 * sizeof
(map_cell_vector*));
for (i=0;i<10;i++)
array[i]=malloc(sizeof
(map_cell_vector));
map_cell_vector *ptr4,*ptr5;
for (i=0;i<10;
i++){
ptr4=array[i];
ptr4->px=0+(10*i);
ptr4->py=0+(10*i);
for (j=1;j<10;j++) {
ptr5=(map_cell_vector*)malloc(sizeof
(map_cell_vector));
ptr4->next=ptr5;
ptr4=ptr5;
ptr4-
>px=j+(10*i);
ptr4->py=j+(10*i);
}
}
for
(i=0;i<10;i++) {
printf("Starting back trace of array[%d]: \n",i);
map_cell_vector *ptr6=array[i]->next;
printf("-->(%2d,%2d)",
array[i]->px,array[i]->py);
while (ptr6->next != NULL) {
printf("-->(%2d,%2d)",ptr6->px,ptr6->py);
ptr6=ptr6->next;
}
printf("-->(%2d,%2d)\n",ptr6->px,ptr6->py);
}
free_vector(array
[3]);
}
void free_vector (map_cell_vector *ptr) {
printf ("now
in (%d,%d)\n",ptr->px,ptr->px);
if (ptr->next != NULL) {
printf
("entering cell -> ");
free_vector(ptr->next);
}
printf
("\nfreeing cell (%d,%d)\n",ptr->px,ptr->px);
free(ptr);
}
------------------------------------------------------------------------------
Beautiful is writing same markup. Internet Explorer 9 supports
standards for HTML5, CSS3, SVG 1.1, ECMAScript5, and DOM L2 & L3.
Spend less time writing and rewriting code and more time creating great
experiences on the web. Be a part of the beta today.
http://p.sf.net/sfu/beautyoftheweb
_______________________________________________
Valgrind-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/valgrind-users