Lines 19 and 20 are easy:  you allocate memory, store a pointer to 
those two memory blocks in ptr1 and ptr2, then never release them.  You 
could remove ptr1 and ptr2 from this program and it would otherwise 
remain the same.

Line 22 is a little more complex.  Here you allocate an array to hold 
pointers.  In lines 23 and 24 you fill the array with pointers.  Each 
pointer references a block of memory.  A little below, you create singly 
linked lists of records, each of which starts at an array entry.

At the end of your program you release one list of vectors.  You do not 
release every list of vectors, and you do not release the array.  All of 
the memory leaks in the full program (with everything uncommented) 
should go away if you replace the line "free_vector(array[3]);" with:

   free(ptr1);
   free(ptr2);
   for (i=0;i<10;i++)
     free_vector(array[3]);
   free(array);

A warning:  the memory you get from malloc() is not initialized.  Your 
free_vector() function may try to access uninitialized memory when it 
reads "ptr6->next".  Look at the difference between malloc() and 
calloc().  You don't have to use calloc(), but you should always 
initialize all fields of every memory record you allocate.  You're 
initializing "px" and "py" but don't always initialize "ptr5->next" 
(hint:  you only initialize it if there is another iteration in the 
loop; the last one is not initialized).  Valgrind should be able to tell 
you about this too.

You definitely need to spend some time learning about C pointers and 
memory allocation.  This little program is a good start; you should 
understand it well before trying to debug your complex application.

On 10/6/2010 5:42 AM, [email protected] wrote:
> 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
>


-- 
     David Chapman         [email protected]
     Chapman Consulting -- San Jose, CA


------------------------------------------------------------------------------
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

Reply via email to