malloc/free question with Purify 
Author Message
 malloc/free question with Purify

Hi,

I'm still messing around with malloc/frees of structures,
esp. structures that point to other ones. (I'm trying to get
at the reason for a seg fault.) Purify dislikes this; it
says there is a Free Memory Read as well as a Freeing of
Unallocated Memory on the line marked with ***. I apologize
for the length; I tried to pare it down as much as possible.

Thanks in advance for any help you can give. This is very
similar to the problem I'm trying to solve; when I eliminate
the "free" in the change_values function, Purify is happier
but reports memory leaks (duh).

btw, Purify is a debugging tool on UNIX; it does an
evaluation of how memory is used in your programs.

-Jennie

---header file my_structs.h------
typedef struct{
    char c;
    int i;
    float *values;

Quote:
} smallStruct;

typedef struct {
    float f;
    char *s;
    smallStruct *ss;

Quote:
} myStruct;

===================================my_structs.c=======
#include <stdio.h>
#include <stdlib.h>

#include "my_structs.h"
#define ENOUGH 25

int change_values(myStruct *this, int num);

int main(void)
{
    myStruct *arrayOstructs;
    int i,k;
    int returnVal;
    int numStructs = 3;

    arrayOstructs = malloc(sizeof(myStruct) * numStructs);

    for (i = 0; i < numStructs; i++)
    {
        arrayOstructs[i].f = 1.0*i;
        arrayOstructs[i].ss = (smallStruct *) malloc(sizeof(smallStruct));
        arrayOstructs[i].ss->c = 'a';
        arrayOstructs[i].ss->i = i;
        arrayOstructs[i].ss->values = (float *) malloc(sizeof(float)*(i+1)*10);
        for (k = 0; k < (i+1)*10; k++)
        {
            arrayOstructs[i].ss->values[k] = (i+1.0) * k;
        }
    }

    change_values(arrayOstructs,numStructs);

    for (i = 0; i < numStructs; i++)
    {
        free(arrayOstructs[i].ss);
        free(arrayOstructs[i].ss->values); /* *** */
    }
    free(arrayOstructs);

    return EXIT_SUCCESS;

Quote:
}

int change_values(myStruct this[], int num)
{
    int j;
    float *newarray;

    newarray = (float *) malloc (sizeof(float) * 10);
    for (j = 0; j < 10; j++)
    {
        newarray[j] = j;
    }

    for (j = 0; j < num; j++)
    {
        free(this[j].ss->values);
        this[j].ss->values = newarray;
    }

    return 1;

Quote:
}

--
<>~<>~<>~<>~<>~<>~<>~<>~<>~<>~<>~<>~<>~<>~<>~<>~<>~<>~<>~<>~<>
Jennie Van Heuit, Alameda, CA     http://www.*-*-*.com/
(remove the letter before the dot to respond via email)
"Wear sunscreen." -Mary Schmich (*not* Kurt Vonnegut)


Tue, 08 Feb 2000 03:00:00 GMT  
 malloc/free question with Purify

Quote:

> Hi,

> I'm still messing around with malloc/frees of structures,
> esp. structures that point to other ones. (I'm trying to get
> at the reason for a seg fault.) Purify dislikes this; it
> says there is a Free Memory Read as well as a Freeing of
> Unallocated Memory on the line marked with ***. I apologize
> for the length; I tried to pare it down as much as possible.

> Thanks in advance for any help you can give. This is very
> similar to the problem I'm trying to solve; when I eliminate
> the "free" in the change_values function, Purify is happier
> but reports memory leaks (duh).

> btw, Purify is a debugging tool on UNIX; it does an
> evaluation of how memory is used in your programs.

> -Jennie

> ---header file my_structs.h------
> typedef struct{
>     char c;
>     int i;
>     float *values;
> } smallStruct;

> typedef struct {
>     float f;
>     char *s;
>     smallStruct *ss;
> } myStruct;

> ===================================my_structs.c=======
> #include <stdio.h>
> #include <stdlib.h>

> #include "my_structs.h"
> #define ENOUGH 25

> int change_values(myStruct *this, int num);

> int main(void)
> {
>     myStruct *arrayOstructs;
>     int i,k;
>     int returnVal;
>     int numStructs = 3;

>     arrayOstructs = malloc(sizeof(myStruct) * numStructs);

>     for (i = 0; i < numStructs; i++)
>     {
>         arrayOstructs[i].f = 1.0*i;
>         arrayOstructs[i].ss = (smallStruct *) malloc(sizeof(smallStruct));
>         arrayOstructs[i].ss->c = 'a';
>         arrayOstructs[i].ss->i = i;
>         arrayOstructs[i].ss->values = (float *) malloc(sizeof(float)*(i+1)*10);
>         for (k = 0; k < (i+1)*10; k++)
>         {
>             arrayOstructs[i].ss->values[k] = (i+1.0) * k;
>         }
>     }

>     change_values(arrayOstructs,numStructs);

>     for (i = 0; i < numStructs; i++)
>     {
>         free(arrayOstructs[i].ss);
>         free(arrayOstructs[i].ss->values); /* *** */
>     }
>     free(arrayOstructs);

>     return EXIT_SUCCESS;
> }

> int change_values(myStruct this[], int num)
> {
>     int j;
>     float *newarray;

>     newarray = (float *) malloc (sizeof(float) * 10);
>     for (j = 0; j < 10; j++)
>     {
>         newarray[j] = j;
>     }

>     for (j = 0; j < num; j++)
>     {
>         free(this[j].ss->values);
>         this[j].ss->values = newarray;
>     }

>     return 1;
> }

> --
> <>~<>~<>~<>~<>~<>~<>~<>~<>~<>~<>~<>~<>~<>~<>~<>~<>~<>~<>~<>~<>
> Jennie Van Heuit, Alameda, CA     http://www.rahul.net/peapod/
> (remove the letter before the dot to respond via email)
> "Wear sunscreen." -Mary Schmich (*not* Kurt Vonnegut)

Looks like you're freeing memory:

free(arrayOstructs[i].ss);

then you're accessing memory formerly pointer to by .ss (you just
freed):

free(arrayOstructs[i].ss->values); /* *** */

You must free ss->values before freeing ss! Just flip the order of the
two statements.



Tue, 08 Feb 2000 03:00:00 GMT  
 malloc/free question with Purify

Quote:

> Hi,

> I'm still messing around with malloc/frees of structures,
> esp. structures that point to other ones. (I'm trying to get
> at the reason for a seg fault.) Purify dislikes this; it
> says there is a Free Memory Read as well as a Freeing of
> Unallocated Memory on the line marked with ***. I apologize
> for the length; I tried to pare it down as much as possible.

> Thanks in advance for any help you can give. This is very
> similar to the problem I'm trying to solve; when I eliminate
> the "free" in the change_values function, Purify is happier
> but reports memory leaks (duh).

> btw, Purify is a debugging tool on UNIX; it does an
> evaluation of how memory is used in your programs.

> -Jennie

> ---header file my_structs.h------
> typedef struct{
>     char c;
>     int i;
>     float *values;
> } smallStruct;

> typedef struct {
>     float f;
>     char *s;
>     smallStruct *ss;
> } myStruct;

> ===================================my_structs.c=======
> #include <stdio.h>
> #include <stdlib.h>

> #include "my_structs.h"
> #define ENOUGH 25

> int change_values(myStruct *this, int num);

> int main(void)
> {
>     myStruct *arrayOstructs;
>     int i,k;
>     int returnVal;
>     int numStructs = 3;

>     arrayOstructs = malloc(sizeof(myStruct) * numStructs);

>     for (i = 0; i < numStructs; i++)
>     {
>         arrayOstructs[i].f = 1.0*i;
>         arrayOstructs[i].ss = (smallStruct *) malloc(sizeof(smallStruct));
>         arrayOstructs[i].ss->c = 'a';
>         arrayOstructs[i].ss->i = i;
>         arrayOstructs[i].ss->values = (float *) malloc(sizeof(float)*(i+1)*10);
>         for (k = 0; k < (i+1)*10; k++)
>         {
>             arrayOstructs[i].ss->values[k] = (i+1.0) * k;
>         }
>     }

>     change_values(arrayOstructs,numStructs);

>     for (i = 0; i < numStructs; i++)
>     {
>         free(arrayOstructs[i].ss);
>         free(arrayOstructs[i].ss->values); /* *** */
>     }
>     free(arrayOstructs);

>     return EXIT_SUCCESS;
> }

> int change_values(myStruct this[], int num)
> {
>     int j;
>     float *newarray;

>     newarray = (float *) malloc (sizeof(float) * 10);
>     for (j = 0; j < 10; j++)
>     {
>         newarray[j] = j;
>     }

>     for (j = 0; j < num; j++)
>     {
>         free(this[j].ss->values);
>         this[j].ss->values = newarray;
>     }

>     return 1;
> }

> --
> <>~<>~<>~<>~<>~<>~<>~<>~<>~<>~<>~<>~<>~<>~<>~<>~<>~<>~<>~<>~<>
> Jennie Van Heuit, Alameda, CA     http://www.rahul.net/peapod/
> (remove the letter before the dot to respond via email)
> "Wear sunscreen." -Mary Schmich (*not* Kurt Vonnegut)

Jennie:

Try freeing first the ss.values and afterwards ss:

         free(arrayOstructs[i].ss->values); /* *** */
         free(arrayOstructs[i].ss);

Juan Urrutia//

--



Wed, 09 Feb 2000 03:00:00 GMT  
 malloc/free question with Purify



Quote:
>        free(arrayOstructs[i].ss);
>        free(arrayOstructs[i].ss->values); /* *** */

Oh dear! You have freed the object pointed at by pointer (arraryOstructs[i].ss)
and in the very next line you dereference that pointer. Once you call free(),
on a non-NULL pointer that was obtained from malloc(), that pointer's value
becomes indeterminate; any use of it leads to undefined behavior, even just
assigning its value or doing pointer arithmetic without dereferencing.

Quote:
>    free(arrayOstructs);

>    return EXIT_SUCCESS;
>}

>int change_values(myStruct this[], int num)
>{
>    int j;
>    float *newarray;

>    newarray = (float *) malloc (sizeof(float) * 10);

It's somewhat poor practice to cast the return value of malloc. Since
the return type is void *, no explicit conversion operator is required.
The nice thing about leaving the cast out is that not only do you have
less stuff to type, you also get a diagnostic message in case you forget
to declare malloc() beforehand, whereas if you have the cast in place,
the implementation is not required to diagnose the problem.


Wed, 09 Feb 2000 03:00:00 GMT  
 malloc/free question with Purify

Quote:

> Hi,

> I'm still messing around with malloc/frees of structures,
> esp. structures that point to other ones. (I'm trying to get
> at the reason for a seg fault.) Purify dislikes this; it
> says there is a Free Memory Read as well as a Freeing of
> Unallocated Memory on the line marked with ***. I apologize
> for the length; I tried to pare it down as much as possible.

> Thanks in advance for any help you can give. This is very
> similar to the problem I'm trying to solve; when I eliminate
> the "free" in the change_values function, Purify is happier
> but reports memory leaks (duh).
>     for (i = 0; i < numStructs; i++)
>     {
>         free(arrayOstructs[i].ss);
>         free(arrayOstructs[i].ss->values); /* *** */
>     }

 Hi, Jennie.
 Look at the sequence of your calls to free(). First you do free
the memory p[ointed to by ss and then you do try to free ss->values -
but the pointer ss is already invalid! You cat't access anything
through it. Hence tje first Purify message. Of course, if you simply
leave the marked line out, the memory pointed to by ss->values is never
freed and you do have memory leak - hence the second message.
 So - you must swap the lines; first free ss->values and only then -
ss itself.

        Regards,
                Alex Krol



Thu, 10 Feb 2000 03:00:00 GMT  
 malloc/free question with Purify


Quote:
> I'm still messing around with malloc/frees of structures,
> esp. structures that point to other ones. (I'm trying to get
> at the reason for a seg fault.) Purify dislikes this; it
> says there is a Free Memory Read as well as a Freeing of
> Unallocated Memory on the line marked with ***. I apologize
> for the length; I tried to pare it down as much as possible.
>     arrayOstructs = malloc(sizeof(myStruct) * numStructs);

The cast on malloc() is not needed, and could hide a failure to
include stdlib.h.

Quote:
>     for (i = 0; i < numStructs; i++)
>     {
>         free(arrayOstructs[i].ss);
>         free(arrayOstructs[i].ss->values); /* *** */

You should free this *before* the previous line.  What you do is free
arrayOstrutcs[i].ss, then immediately look in the memory you freed to
find the address of values.  You're not allowed to dreference freed
pointers.

--

            http://www.geocities.com/SiliconValley/Lakes/7537/



Thu, 10 Feb 2000 03:00:00 GMT  
 malloc/free question with Purify

Hi Jennie,

You may get the segmentation fault because of

Quote:
>     newarray = (float *) malloc (sizeof(float) * 10);
>     for (j = 0; j < 10; j++)
>     {
>         newarray[j] = j;
>     }

>     for (j = 0; j < num; j++)
>     {
>         free(this[j].ss->values);
>         this[j].ss->values = newarray;
>     }

in your change_values() function. The statement

Quote:
>         this[j].ss->values = newarray;

lets point every 'this[j].ss->values' to the same block of memory. You then
call several free()'s to already freed memory in your for()-loop in main(),
after the variable i becomes greater as 0:

Quote:
>     for (i = 0; i < numStructs; i++)
>     {
>         free(arrayOstructs[i].ss);
>         free(arrayOstructs[i].ss->values); /* *** */ /* after i>0 free()

tries to free already freed memory */

Quote:
>     }
>     free(arrayOstructs);

The operating system usually dislikes this and reacts with a segmentation
fault.

I assume you wanted to write something like this:

Quote:
>     for (j = 0; j < num; j++)
>     {
>         free(this[j].ss->values);
>         newarray = (float *) malloc (sizeof(float) * 10);
>         for (k = 0; k < 10; k++)
>         {
>            newarray[k] = k;
>         }
>         this[j].ss->values = newarray; /* every values-pointer refers to
different memory */
>     }

- and please never forget to assure that malloc() really has allocated
memory (newarray should be tested to be not NULL before it is used).

Greetings,
Andreas



Wed, 16 Feb 2000 03:00:00 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. Purify -- Super-duper Malloc Debugger

2. need free/cheap Purify-like memory debugger

3. malloc, realloc, free questions

4. malloc/free questions

5. malloc - free question

6. malloc/free question

7. Dumb question concerning malloc/free

8. a question about malloc and free

9. simple question about malloc & free

10. basic malloc/free question

11. dumb malloc free question

12. malloc(), free(), and strtok() questions

 

 
Powered by phpBB® Forum Software