释放嵌套结构的内存 [英] Freeing memory for nested structures

查看:89
本文介绍了释放嵌套结构的内存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨再次,


我正在遇到的其他一些大问题......


typedef struct perBlockStru / *(结构)每个块长字

* /

{

unsigned long * lword [LASTLW];

} lwperBlockStru_t;


typedef struct实体/ *(结构)保存整个事物* /

{

perBlockStru_t * block [LASTBLK ];

} entityStru_t;


这是一个 - 相当简化 - 我正在使用的结构。

当然有更多元素,但它们在这里无关紧要。


远在下面,有明显的内存分配:

(pE = * entityStru_t)

for(i 0; i = LASTBLK; i ++)

{...

for(j = 0; j = LASTLW; j ++)

{...

pE-> block [i] - > lword [j] = malloc(sizeof(unsigned long));

...

}

}


在非常低端,在退出ap之前不久p,我想*我可以只是

做:


if(pE!= NULL)

free(pE) ;


荒谬的是,它有时会起作用,但有时我也会得到一个GPF。

因此我认为我需要去尴尬。在破坏例程中使用另一个

循环

的方式。

或者这应该通过简单地使用来自
结构'的基地址?


-Andreas

解决方案

< blockquote>

" Andreas Eibach" < ae ***** @ mail.comwrote in message


嗨再次,


另一个大牌之一困难我正在......


typedef struct perBlockStru / *(结构)长字每



* /

{

unsigned long * lword [LASTLW];

} lwperBlockStru_t;


typedef struct Entity / *(结构)包含整个事物* /

{

perBlockStru_t * block [LASTBLK];

} entityStru_t;


这是一个 - 相当简化 - 我正在使用的结构。

当然还有更多元素,但它们在这里无关紧要。


远远低于,发生明显的内存分配:

(pE = * entityStru_t)

for(i 0; i = LASTBLK ; i ++)

{...

for(j = 0; j = LASTLW; j ++)

{...

pE-> block [i] - > lword [j] = malloc(sizeof(unsigned long));

...

}

}


在非常低端,在退出应用程序前不久,我*我想*我可以

只需

do:


if(pE!= NULL)

free(pE);


荒谬的是,它有时会起作用,但有时我也会获得GPF。

因此我认为我需要去尴尬。在破坏例程中使用另一个

循环

的方式。

或者这应该通过简单地使用来自
结构'的基地址?



写一个kill函数。


void killentity_Stru_t(entity_Stru_t * e)

{

int i,ii;

if(e)

{

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

for(ii = 0; ii< LASTLW; ii ++)

free(e-> block [i] - > word [ii]):

free(e);

}

}


我想我已经做对了。一段时间后它变成样板代码。更多

带垃圾收集功能的现代语言为您自动完成整个过程,但是您需要手动释放所有内容。把它想象成一件小事。


-

免费游戏和编程好东西。
http://www.personal.leeds.ac.uk/~bgy1mm


" Andreas Eibach" < ae ***** @ mail.comwrites:


嗨再次,


其他一个大问题我有......


typedef struct perBlockStru / *(结构)每个块长字

* /

{

unsigned long * lword [LASTLW];

} lwperBlockStru_t;


typedef struct Entity / *(structure)持有整件事* /

{

perBlockStru_t * block [LASTBLK];

} entityStru_t;


这是一个 - 相当简单 - 我正在使用的结构。

当然有更多元素,但它们在这里无关紧要。


远低于,发生了明显的内存分配:

(pE = * entityStru_t)



pE =& entityStru_t,我假设。


顺便说一下,如果这是一个Unix / POSIX系统,AFAIK你不应该用_b $ b来命名以_t结尾的东西;它们是为系统保留的。


for(i 0; i = LASTBLK; i ++)

{...

for(j = 0; j = LASTLW; j ++)

{...

pE-> block [i] - > lword [j] = malloc(sizeof(unsigned long));



我认为这不是你的实际代码,并且你真的有
malloc(sizeof(unsigned long)* n)要分配n个未签名的数组

多头。分配一个似乎有点奇怪,我怀疑它不会是你想要的。


...

}

}


在非常低端,在退出应用程序之前不久,我想*我可能只是

do:


if(pE!= NULL)

free(pE);



编号你必须传递给free()正是那些从malloc()返回的指针

,而不是别的。 free()不知道你的更多

精心设计的数据结构,不能深入挖掘它来找到东西

你malloc''ed。


荒谬的是,它有时会起作用,但有时我也会得到一个GPF。

因此我认为我需要去尴尬。在破坏程序中使用另一个

循环

的方式。



正确。


但是,如果在退出之前这是真的,那可能不是
$ b完全免费提供这些东西需要$ b,因为任何理智的操作系统都会为你解决这个问题。请参阅 http://c-faq.com/malloc/freeb4exit.html

由于可维护性的原因,这可能仍然是一个好主意,

取决于你的情况。


或者这可以通过简单地使用

结构的基地址中的自由例程来工作吗?



No.


Andreas Eibach写道:


你好,


我正在遇到的其他一些大问题......


typedef struct perBlockStru / *(结构)每个块长字

* /

{

unsigned long * lword [LASTLW];

} lwperBlockStru_t;


typedef struct实体/ *(结构)保存整个事物* /

{

perBlockStru_t * block [LASTBLK ];

} entityStru_t;


这是一个 - 相当简化 - 我正在使用的结构。

当然有更多的元素,但它们在这里是无关紧要的。


远远低于,发生了明显的内存分配:

(pE = * entityStru_t)



你写的是胡说八道。我怀疑你想说的是什么

将被正确表达为:


entityStru_t * pE;


for(i 0; i = LASTBLK; i ++)

{...

for(j = 0; j = LASTLW; j ++)

{...

pE-> block [i] - > lword [j] = malloc(sizeof(unsigned long));

...

}

}


在非常低端,在退出应用程序之前不久,我想*我可以只是

做:


if(pE!= NULL)

free(pE);


荒谬的是,它有时会起作用,但有时我也会获得GPF。



你没有向我们展示pE获得价值的代码,所以告诉我们是否很难

来判断是否你应该能够安全地免费拨打(pE)。

但是,如果你不想泄漏内存,你最好免费(pE-


> block [i] - > lword [j])和j,在
之前



free()ing pE本身。


因此我假设我被要求去尴尬在破坏例程中使用另一个

循环

的方式。

或者这应该通过简单地使用来自
结构'的基地址?



结构本身的内存是使用malloc(),

calloc()还是realloc()分配的?你没有显示这样的分配,所以我们无法确定

。如果是,它可以是免费的()d。


Hi again,

one of the other big woes I''m having...

typedef struct perBlockStru /* (structure) Long words per block
*/
{
unsigned long *lword[LASTLW];
} lwperBlockStru_t;

typedef struct Entity /* (structure) Holds the whole thing */
{
perBlockStru_t* block[LASTBLK];
} entityStru_t;

This is a - fairly simplified - structure I''m using.
Of course there are more elements, but they are irrelevant here.

Far below, there''s the obvious mem allocation happening:
(pE = *entityStru_t)
for (i 0; i = LASTBLK; i++)
{ ...
for (j= 0; j = LASTLW; j++)
{ ...
pE->block[i]->lword[j] = malloc (sizeof(unsigned long));
...
}
}

At the VERY bottom, shortly before exiting the app, I *thought* I could just
do:

if (pE != NULL)
free(pE);

Preposterously, it sometimes does work, but sometimes I also get a GPF.
Hence I assume that I am required to go the "awkward" way by using another
loop
in the destruction routine.
Or is this supposed to work by simply using the free routine from the
structure''s base address?

-Andreas

解决方案


"Andreas Eibach" <ae*****@mail.comwrote in message

Hi again,

one of the other big woes I''m having...

typedef struct perBlockStru /* (structure) Long words per
block
*/
{
unsigned long *lword[LASTLW];
} lwperBlockStru_t;

typedef struct Entity /* (structure) Holds the whole thing */
{
perBlockStru_t* block[LASTBLK];
} entityStru_t;

This is a - fairly simplified - structure I''m using.
Of course there are more elements, but they are irrelevant here.

Far below, there''s the obvious mem allocation happening:
(pE = *entityStru_t)
for (i 0; i = LASTBLK; i++)
{ ...
for (j= 0; j = LASTLW; j++)
{ ...
pE->block[i]->lword[j] = malloc (sizeof(unsigned long));
...
}
}

At the VERY bottom, shortly before exiting the app, I *thought* I could
just
do:

if (pE != NULL)
free(pE);

Preposterously, it sometimes does work, but sometimes I also get a GPF.
Hence I assume that I am required to go the "awkward" way by using another
loop
in the destruction routine.
Or is this supposed to work by simply using the free routine from the
structure''s base address?

Write a "kill" function.

void killentity_Stru_t(entity_Stru_t *e)
{
int i, ii;

if(e)
{
for(i=0;i<LASBLK;i++)
for(ii=0;ii<LASTLW;ii++)
free(e->block[i]->word[ii]):
free(e);
}
}

I think I''ve got this right. It becomes boilerplate code after a while. More
modern languages with garbage collection automate the process for you, but
in C you have to free everything by hand. Think of it as a little chore.

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm


"Andreas Eibach" <ae*****@mail.comwrites:

Hi again,

one of the other big woes I''m having...

typedef struct perBlockStru /* (structure) Long words per block
*/
{
unsigned long *lword[LASTLW];
} lwperBlockStru_t;

typedef struct Entity /* (structure) Holds the whole thing */
{
perBlockStru_t* block[LASTBLK];
} entityStru_t;

This is a - fairly simplified - structure I''m using.
Of course there are more elements, but they are irrelevant here.

Far below, there''s the obvious mem allocation happening:
(pE = *entityStru_t)

pE = &entityStru_t , I assume.

By the way, if this is a Unix/POSIX system, AFAIK you are not supposed
to name things ending in _t ; they''re reserved for the system.

for (i 0; i = LASTBLK; i++)
{ ...
for (j= 0; j = LASTLW; j++)
{ ...
pE->block[i]->lword[j] = malloc (sizeof(unsigned long));

I presume this is not your actual code, and that you really have
malloc(sizeof(unsigned long) * n) to allocate an array of n unsigned
longs. Allocating just one seems kind of weird, I suspect it wouldn''t
be what you''d want.

...
}
}

At the VERY bottom, shortly before exiting the app, I *thought* I could just
do:

if (pE != NULL)
free(pE);

No. You must pass to free() exactly those pointers that were returned
from malloc(), and nothing else. free() does not know about your more
elaborate data structure and cannot dig down into it to find the things
you malloc''ed.

Preposterously, it sometimes does work, but sometimes I also get a GPF.
Hence I assume that I am required to go the "awkward" way by using another
loop
in the destruction routine.

Right.

However, if this really is right before exiting, it is probably not
necessary to free this stuff at all, as any sane operating system will
take care of it for you. See http://c-faq.com/malloc/freeb4exit.html .
It might still be a good idea to do it for maintainability reasons,
depending on your situation.

Or is this supposed to work by simply using the free routine from the
structure''s base address?

No.


Andreas Eibach wrote:

Hi again,

one of the other big woes I''m having...

typedef struct perBlockStru /* (structure) Long words per block
*/
{
unsigned long *lword[LASTLW];
} lwperBlockStru_t;

typedef struct Entity /* (structure) Holds the whole thing */
{
perBlockStru_t* block[LASTBLK];
} entityStru_t;

This is a - fairly simplified - structure I''m using.
Of course there are more elements, but they are irrelevant here.

Far below, there''s the obvious mem allocation happening:
(pE = *entityStru_t)

What you wrote is nonsense. I suspect that what you were trying to say
would be correctly expressed as:

entityStru_t *pE;

for (i 0; i = LASTBLK; i++)
{ ...
for (j= 0; j = LASTLW; j++)
{ ...
pE->block[i]->lword[j] = malloc (sizeof(unsigned long));
...
}
}

At the VERY bottom, shortly before exiting the app, I *thought* I could just
do:

if (pE != NULL)
free(pE);

Preposterously, it sometimes does work, but sometimes I also get a GPF.

You don''t show us the code whereby pE acquires a value, so it''s hard
to tell whether or not you should be able to safely call free(pE).
However, if you don''t want to leak memory, you had better free(pE-

>block[i]->lword[j]) for all relevant values of "i" and "j", before

free()ing pE itself.

Hence I assume that I am required to go the "awkward" way by using another
loop
in the destruction routine.
Or is this supposed to work by simply using the free routine from the
structure''s base address?

Was the memory for the structure itself allocated using malloc(),
calloc() or realloc()? You show no such allocation, so there''s no way
for us to be sure. If it was, it can be free()d.


这篇关于释放嵌套结构的内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆