newbie malloc problem: free memory allocated to structures 
Author Message
 newbie malloc problem: free memory allocated to structures

hello all,

   I have a program which continually runs in the background and now is
leaking a lot of memory. I run memprof and could find that the memory is
leaked because the memeory allocated to structures is not completely
freed when I do so.

   Below I have replicated the problem I face as simply as possible

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

typedef struct {
  char *s;
  int d;
  char name[100];
  char desc1[1023];
  char desc2[1023];
  char desc3[1023];

Quote:
}dataunit;

int main(int argc,char* argv){
  dataunit *d;
  d = malloc(sizeof(dataunit));
  d->s=(char *) malloc(10240);

  /* POSITION 1: memory allocated is 16600Bytes */
  sleep(5);
  free(d->s);

  /* POSITION 2: memory allocated is 6360Bytes */
  sleep(5);
  free(d);

  /* POSITION 3: memory allocated is 3180Bytes */
  sleep(5);
  exit(0);

Quote:
}

/* eof */

My problem is at POSITION 3 I expect the memory allocated will be zero
because *d is freed but the condition is not so

So how do I totally free *d?
I suspect the memory allocated to d->name[] , d->desc1[] etc are not
getting freed

Please help someone

--
Ramprasad A Padmanabhan
Sr Software Engineer
Netcore Solns Pvt Ltd
Mumbai
ph - (022) 4628000



Fri, 01 Jul 2005 15:33:54 GMT  
 newbie malloc problem: free memory allocated to structures
in comp.lang.c i read:

Quote:
>   I have a program which continually runs in the background and now is
>leaking a lot of memory. I run memprof and could find that the memory is
>leaked because the memeory allocated to structures is not completely
>freed when I do so.
>My problem is at POSITION 3 I expect the memory allocated will be zero
>because *d is freed but the condition is not so

that's because your expectations are fallacious.  implementations are
allowed to retain memory obtained from the system until the program
terminates.

--
bringing you boring signatures for 17 years



Fri, 01 Jul 2005 16:20:57 GMT  
 newbie malloc problem: free memory allocated to structures

Quote:

> in comp.lang.c i read:

> >   I have a program which continually runs in the background and now is
> >leaking a lot of memory. I run memprof and could find that the memory is
> >leaked because the memeory allocated to structures is not completely
> >freed when I do so.

> >My problem is at POSITION 3 I expect the memory allocated will be zero
> >because *d is freed but the condition is not so

> that's because your expectations are fallacious.  implementations are
> allowed to retain memory obtained from the system until the program
> terminates.

But why does free(d->s) return memory back to the system
at position 2
and free(d) return memory only back to the program and not to the
system. Or is memprof giving wrong results

Thanks
Ram



Fri, 01 Jul 2005 16:29:12 GMT  
 newbie malloc problem: free memory allocated to structures
in comp.lang.c i read:

Quote:
>But why does free(d->s) return memory back to the system at position 2 and
>free(d) return memory only back to the program and not to the system.

consult your implementation's documentation.  it is curious that exactly
the amount of memory is retained as is the size of your struct.

Quote:
>Or is memprof giving wrong results

perhaps.  i see no such results on my system, but my system isn't your
system so that may not mean anything.

--
bringing you boring signatures for 17 years



Fri, 01 Jul 2005 17:36:30 GMT  
 newbie malloc problem: free memory allocated to structures

Quote:
> int main(int argc,char* argv){
>   dataunit *d;
>   d = malloc(sizeof(dataunit));

        a) this is uncheck malloc can / will fails
        b) you alloc the size of the pointer
           try sizeof(*dataunit); which will
           give you the size of the data struct the
           pointer points to.


Fri, 01 Jul 2005 18:21:15 GMT  
 newbie malloc problem: free memory allocated to structures

typedef struct {
   char *s;
   int d;
   char name[100];
   char desc1[1023];
   char desc2[1023];
   char desc3[1023];

Quote:
}dataunit;
>>int main(int argc,char* argv){
>>  dataunit *d;
>>  d = malloc(sizeof(dataunit));

>         b) you alloc the size of the pointer
>            try sizeof(*dataunit); which will
>            give you the size of the data struct the
>            pointer points to.

You are mistaken. dataunit is not a pointer. It is a data
type specifier and the malloc call, if successful, will
allocate storage for the data type.

----
Al Bowers
Tampa, FL. USA

http://www.geocities.com/abowers822
comp.lang.c



Fri, 01 Jul 2005 21:31:46 GMT  
 newbie malloc problem: free memory allocated to structures

Quote:
> typedef struct {
>    char *s;
>    int d;
>    char name[100];
>    char desc1[1023];
>    char desc2[1023];
>    char desc3[1023];
> }dataunit;

> >>int main(int argc,char* argv){
> >>  dataunit *d;
> >>  d = malloc(sizeof(dataunit));

> >         b) you alloc the size of the pointer
> >            try sizeof(*dataunit); which will
> >            give you the size of the data struct the
> >            pointer points to.

> You are mistaken. dataunit is not a pointer. It is a data
> type specifier and the malloc call, if successful, will
> allocate storage for the data type.

sorry must have been early in the morning
sorry for the misdirection ......


Fri, 01 Jul 2005 23:13:40 GMT  
 newbie malloc problem: free memory allocated to structures


Quote:
>   I have a program which continually runs in the background and now is
>leaking a lot of memory. I run memprof and could find that the memory is
>leaked because the memeory allocated to structures is not completely
>freed when I do so.

Since "memprof" is not a part of Standard C, I cannot say much
about it.  You would probably get a much better / more-accurate
answer in a group that deals with your specific system, where
the people who read and write articles in that group are familiar
with your system's "memprof".  In any case, however:

Quote:
>   Below I have replicated the problem I face as simply as possible

(This is a Good Thing, simplifying the problem as much as possible
but not too much :-) )

[some snippage]

Quote:
>typedef struct {
>  char *s;
>  int d;
>  char name[100];
>  char desc1[1023];
>  char desc2[1023];
>  char desc3[1023];
>}dataunit;

If we add these sizes and assume no padding bytes, we get:

    sizeof(char *) + sizeof(int) + 100 + 3*1023

or:

    sizeof(char *) + sizeof(int) + 3169

which is probably about 3171 through 3179 or so (depending on
sizeof(char *) and sizeof(int)).  C does say that sizeof(char)==1.

We might round this to 3180 bytes, a number which seems to pop
up in a moment; or you could just add, somewhere, the call:

    printf("sizeof(dataunit) = %lu\n", (unsigned long)sizeof(dataunit));

to find out for sure.

Quote:
>int main(int argc,char* argv){
>  dataunit *d;
>  d = malloc(sizeof(dataunit));

I prefer the idiom:

   d = malloc(sizeof *d);

myself, but this is a style issue.

Quote:
>  d->s=(char *) malloc(10240);

It is a bit curious that you cast here, but not in the previous
assignment.  You did include <stdlib.h> (in a bit I snipped) so
this is OK.

Note that if both malloc()s work, as is likely, the system will
need to have allocated at least 3180 + 10240 = 13420 bytes.
Apparently "memprof" indicates a larger number:

Quote:
>  /* POSITION 1: memory allocated is 16600Bytes */

Next we have:

Quote:
>  sleep(5);

Note that sleep() is not a Standard C function.  Perhaps it allocates
memory, although the subsequent numbers suggest not.

Quote:
>  free(d->s);

This effectively erases the object allocated by the second malloc(),
and therefore should release 10240 bytes for later re-allocation.
16600 - 10240 = 6360, and:

Quote:
>  /* POSITION 2: memory allocated is 6360Bytes */
>  sleep(5);
>  free(d);

This should release another 3180 (assuming my guess at the size is
accurate) bytes.  6360 - 3180 = 3180, and:

Quote:
>  /* POSITION 3: memory allocated is 3180Bytes */

So all seems well.

The only unanswered question at this point is: who allocated the
remaining 3180 bytes?  It seems particularly odd that this is also,
apparently, sizeof *d.  It *could* be a coincidence, of course; it
might just happen that the C library needs 3180 bytes for itself,
independent of "sizeof *d".
--
In-Real-Life: Chris Torek, Wind River Systems (BSD engineering)
Salt Lake City, UT, USA (4039.22'N, 11150.29'W)

(you probably cannot email me -- spam has effectively killed email)



Sat, 02 Jul 2005 00:43:23 GMT  
 
 [ 8 post ] 

 Relevant Pages 

1. problem allocating/freeing memory of multidimensional arrays

2. Memory Leakage problem using malloc and free

3. dynamic memory allocation performance problems with malloc/free

4. allocating memory via malloc/calloc

5. Where does malloc allocate memory?

6. Determining amount of memory allocated by malloc()

7. Allocating memory to a multidimensional array using malloc ??

8. What is the proper way to allocate and free memory in this instance

9. freeing allocated memory from included files

10. freeing the memory allocated by calloc

11. doubt - freeing the memory allocated by calloc

12. Non-allocated memory- is it freed?

 

 
Powered by phpBB® Forum Software