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 00:34: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.

> #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;
> }

There may *also* be a leak here as you haven't shown us how you free the
allocations but that's not the main problem.  The main problem is that
there's a bug in your code.  Your loop should iterate y times not x times.


Thu, 25 Mar 2004 01:04:02 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.

--snip---

Quote:

> There may *also* be a leak here as you haven't shown us how you free the
> allocations but that's not the main problem.  The main problem is that
> there's a bug in your code.  Your loop should iterate y times not x times.

DUH!!!!! ... god, I hate it when I look at something for too long and miss
the simple things... ... ...
thanx 8-)


Thu, 25 Mar 2004 01:07:27 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;
>}

Looking at the code as it is, why do you need both x and y? If there's
some context, show it!

--
rihad



Thu, 25 Mar 2004 13:04:09 GMT  
 memory Leak: pointer->pointer->pointer->struct

Quote:

>>}
> Looking at the code as it is, why do you need both x and y? If there's
> some context, show it!

Because I included the minimum ammount of compiled code to reflect the
specific issues I had.

the specific issue I had, dealt with dynamic allocation of a
multidimensional array of pointers to structs
(pointer->pointer->pointer->struct actualy).

It would seem rather silly to dynamicly allocate something without knowing
at executiong time what size it needed to be allocated to. x,y Represent
those values, but in a more convenient manor for debugging purposes...

would you like me to send you the entire project I was working on??

incidentaly, the problem was quite simple, as was pointed out to me earlier
(in this group) as I was allocating x x times instead of y times...



Thu, 25 Mar 2004 05:34:28 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