1

Topic: Memory leak, I can not understand where

I want to implement a pool  (node) on the basis of the doubly-connected list for With, and such code turned out: #include <stdlib.h> #include <string.h> struct item_t {void *next;}; typedef struct item_t item_t; item_t *pool_root = NULL, *pool_last = NULL; void put_elem_mem (item_t *sv) {if (! sv || sv-> next! = NULL) return; if (pool_last == NULL) pool_root = pool_last = sv; else pool_last-> next = sv;} item_t* get_elem_mem () {item_t *r = NULL; if ((r = pool_root) == NULL) {r = malloc (sizeof (struct item_t)); r-> next = NULL; return r;} if ((pool_root = r-> next) == NULL) pool_last = NULL; r-> next = NULL; return r;} int main () {while (1) {void *p0 = get_elem_mem (); void *p1 = get_elem_mem (); void *p2 = get_elem_mem (); put_elem_mem (p0); put_elem_mem (p1); put_elem_mem (p2);} } Here in main () if to create one or two  - storage does not leak. But if to create three and more - leaks. , three hours I can not understand where I was mistaken. Help please.

2

Re: Memory leak, I can not understand where

Hello, Tasheehoo, you wrote: T> T>#include <stdlib.h> T>#include <string.h> T> struct item_t {T> void *next; T>}; T> typedef struct item_t item_t; T> item_t *pool_root = NULL, *pool_last = NULL; T> void put_elem_mem (item_t *sv) T> {T> if (! sv || sv-> next! = NULL) T> return; T> if (pool_last == NULL) T> pool_root = pool_last = sv; T> else T> pool_last-> next = sv; T>} T> item_t* get_elem_mem () T> {T> item_t *r = NULL; T> if ((r = pool_root) == NULL) T> {T> r = malloc (sizeof (struct item_t));//and free to it where? r->> next = NULL; T> return r; T>} T> if ((pool_root = r-> next) == NULL) T> pool_last = NULL; r->> next = NULL; T> return r; T>} T> int main () {T> while (1) {T> void *p0 = get_elem_mem (); T> void *p1 = get_elem_mem (); T> void *p2 = get_elem_mem (); T> T>} T>} T> T> Here in main () if to create one or two  - storage does not leak. But if to create three and more - leaks. T> Chesslovo, three hours I can not understand where I was mistaken. Help please. free I did not see Any, and the list at you one-connected.

3

Re: Memory leak, I can not understand where

Hello, Tasheehoo, you wrote: T> T> void put_elem_mem (item_t *sv) T> {T> if (! sv || sv-> next! = NULL) T> return; T> if (pool_last == NULL) T> pool_root = pool_last = sv; T> else T> pool_last-> next = sv; T>} T>

4

Re: Memory leak, I can not understand where

Hello, Kernan, you wrote: K> free I did not see Any, and the list at you one-connected. Then why at creation one or two  in a cycle storage does not leak?

5

Re: Memory leak, I can not understand where

Hello, andrey.desman, you wrote: AD>//pool_last it would be necessary to update I do not understand that I should make here...

6

Re: Memory leak, I can not understand where

Hello, andrey.desman, you wrote: AD>//pool_last it would be necessary to update Novernoe so? void put_elem_mem (item_t *sv) {if (! sv || sv-> next! = NULL) return; if (pool_last == NULL) pool_root = pool_last = sv; else pool_last-> next = sv; pool_last = pool_last-> next;}

7

Re: Memory leak, I can not understand where

Hello, Tasheehoo, you wrote: AD>>//pool_last it would be necessary to update T> I do not understand that I should make here... At element adding in the list it is necessary to expose both root and last after adding last should specify in just interposed element root it is exposed only once, when the list empty, that is when root == NULL

8

Re: Memory leak, I can not understand where

Hello, Tasheehoo, you wrote: AD>>//pool_last it would be necessary to update T> I do not understand that I should make here... void put_elem_mem (item_t *sv) {if (! sv || sv-> next! = NULL) return; if (pool_last == NULL) pool_root = pool_last = sv; else {pool_last-> next = sv; pool_last = sv;}//pool_last at you here does not change. It is once installed in the first branch if and//as a matter of fact is always equal pool_root. You cause function three times and the second  you lose.//How to make see above}

9

Re: Memory leak, I can not understand where

Hello, Tasheehoo, you wrote: AD>>//pool_last it would be necessary to update T> Novernoe so? Not so. If went on the first branch pool_last = pool_last-> next it is impossible to do, differently you there and then will lose the pool_last (it becomes NULL). T> T> void put_elem_mem (item_t *sv) T> {T> if (! sv || sv-> next! = NULL) T> return; T> if (pool_last == NULL) T> pool_root = pool_last = sv; T> else T> pool_last-> next = sv; T> pool_last = pool_last-> next; T>} T>

10

Re: Memory leak, I can not understand where

Hello, Tasheehoo, you wrote: T> Novernoe so? The mistake can, so?: void put_elem_mem (item_t *sv) {if (! sv || sv-> next! = NULL) return; if (pool_root == NULL) pool_root = sv; else pool_last-> next = sv;//pool_last! = NULL here pool_last = sv;}

11

Re: Memory leak, I can not understand where

Hello, uzhas, you wrote: U> at element adding in the list it is necessary to expose both root and last U> after adding last should specify in just interposed element U> root it is exposed only once, when the list empty that is when root == NULL I understood! All thanks!

12

Re: Memory leak, I can not understand where

Hello, andrey.desman, you wrote: AD> Not so. If went on the first branch pool_last = pool_last-> next it is impossible to do, differently you there and then will lose the pool_last (it becomes NULL). I understood! Thanks!