memory leak: pointer->pointer->pointer->struct 
Author Message
 memory leak: pointer->pointer->pointer->struct

The following works beautifully if x==y, but if they are inequal, I get a
memory leak... the leak seems a very minor one, and doesnt prevent the code
from 'working' fine, it just annoys me to have sloppy allocation... was
wondering if somebody could tell me how I might avoid it.

#include <stdlib.h>

typedef struct {
  int dummy;
  int dummy2;

Quote:
} * pt_stupid;

int main() {
int i,x=500,y=500;
pt_stupid **bubba;

bubba = (pt_stupid **) malloc(y * sizeof(pt_stupid  *));
for(i=0;i<x;i++)
        bubba[i] = (pt_stupid *) malloc(x * sizeof(pt_stupid));

return 0;

Quote:
}

--



Thu, 25 Mar 2004 14:24:04 GMT  
 memory leak: pointer->pointer->pointer->struct

Quote:

> The following works beautifully if x==y, but if they are inequal, I
> get a
> memory leak... the leak seems a very minor one, and doesnt prevent the
> code
> from 'working' fine, it just annoys me to have sloppy allocation...
> was
> wondering if somebody could tell me how I might avoid it.

Two observations:  First, you're not doing any frees at all.  That's
fine in this case, since the program ends and you're guaranteed that all
its memory will be reclaimed.  As such, I don't know how you're
concluding that memory _isn't_ getting reclaimed at the end of its
execution (which is I presume what you mean), since you haven't even
said what operating system you're running this under.

Second, the Subject says this is about a pointer to a pointer to a
pointer to a struct, but it's only a doubly-indirected pointer to a
struct, not a triply-indirected one.

--

 __ San Jose, CA, US / 37 20 N 121 53 W / ICQ16063900 / &tSftDotIotE
/  \ An ounce of hypocrisy is worth a pound of ambition.
\__/ Michael Korda
    Rules for Buh / http://www.alcyone.com/max/projects/cards/buh.html
 The official rules to the betting card game, Buh.
--



Fri, 26 Mar 2004 05:13:38 GMT  
 memory leak: pointer->pointer->pointer->struct

Quote:

> The following works beautifully if x==y, but if they are inequal, I get a
> memory leak... the leak seems a very minor one, and doesnt prevent the code
> from 'working' fine, it just annoys me to have sloppy allocation... was
> wondering if somebody could tell me how I might avoid it.

> #include <stdlib.h>

> typedef struct {
>   int dummy;
>   int dummy2;
> } * pt_stupid;

pt_stupid is now a new name for the type "pointer to struct { int dummy;
int dummy2; }". It is not clear that that is what you intended.

Quote:

> int main() {
> int i,x=500,y=500;
> pt_stupid **bubba;

bubba is a pointer to a pointer to a pointer to a struct. It is not
clear that that is what you intended.

Quote:
> bubba = (pt_stupid **) malloc(y * sizeof(pt_stupid  *));

Better: bubba = malloc(y * sizeof *bubba);

Now check it worked.

Quote:
> for(i=0;i<x;i++)
>         bubba[i] = (pt_stupid *) malloc(x * sizeof(pt_stupid));

Better: bubba[i] = malloc(x * sizeof *bubba[i]);

Now check it worked.

In general, use   p = malloc(numobjs * sizeof *p)  when calling malloc;
the casts are not necessary, don't buy you anything, can in some
circumstances cost you, and look bug-ugly in any case.

Now, as for your bug, it's in your loop.

Quote:
> for(i=0;i<x;i++)

should be

for(i=0;i<y;i++)

n'est-ce-pas? :-)

--

"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
--



Fri, 26 Mar 2004 05:13:41 GMT  
 memory leak: pointer->pointer->pointer->struct


Quote:
>The following works beautifully if x==y, but if they are inequal, I get a
>memory leak... the leak seems a very minor one, and doesnt prevent the code
>from 'working' fine, it just annoys me to have sloppy allocation... was
>wondering if somebody could tell me how I might avoid it.

Memory leak? Gosh you are lucky:) That code is insane, consider what
happens if x>y. What on Earth were you thinking. Of course it is always
possible to write insanely bad code but that is not an excuse for
deliberately doing so. And why should you expect the compiler to stop
you?

Quote:

>#include <stdlib.h>

>typedef struct {
>  int dummy;
>  int dummy2;
>} * pt_stupid;

>int main() {
>int i,x=500,y=500;
>pt_stupid **bubba;

>bubba = (pt_stupid **) malloc(y * sizeof(pt_stupid  *));
>for(i=0;i<x;i++)
>        bubba[i] = (pt_stupid *) malloc(x * sizeof(pt_stupid));

Francis Glassborow
I offer my sympathy and prayers to all those who are suffering
as a result of the events of September 11 2001.
--



Fri, 26 Mar 2004 05:15:28 GMT  
 memory leak: pointer->pointer->pointer->struct

Quote:
> The following works beautifully if x==y, but if they are inequal, I
get a
> memory leak... the leak seems a very minor one, and doesnt prevent the
code
> from 'working' fine, it just annoys me to have sloppy allocation...
was
> wondering if somebody could tell me how I might avoid it.

> #include <stdlib.h>

> typedef struct {
>   int dummy;
>   int dummy2;
> } * pt_stupid;

> int main() {
> int i,x=500,y=500;
> pt_stupid **bubba;

> bubba = (pt_stupid **) malloc(y * sizeof(pt_stupid  *));
> for(i=0;i<x;i++)
>         bubba[i] = (pt_stupid *) malloc(x * sizeof(pt_stupid));

> return 0;
> }
> --


Can't say much about the memory leak because you don't show where the
store is freed. However your loop will give undefined behaviour if x > y
because your first allocation (if it succeeds, which you should check)
provides storage for y pointers, but your loop goes round x times to set
up the pointers.

    Des Walker
--



Fri, 26 Mar 2004 05:15:30 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. memory Leak: pointer->pointer->pointer->struct

2. struct.array works - struct->pointer not

3. map->vector->pointer example

4. pointer->pointer problem

5. Please help <pointer to the struct>

6. Memory leak in ATL service ->>HELP

7. B>C>C++>VC++(MFC)>RAW API PROGRAMMING>ATL>COM>OLE>ADO>ASP.............

8. >>>>>>>>> What would you like in a debugger?

9. <<<<<<<Parsing help, please>>>>>>>>

10. C->->->->C++

11. >>>> PUBLICATION QUALITY SOFTWARE MANUAL

12. File Format conversion, ascii freeform -->.csv <-->.wk1<-->dbf<-->?HELP

 

 
Powered by phpBB® Forum Software