[an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] (none) [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] (none) [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive]
 
[an error occurred while processing this directive] [an error occurred while processing this directive]
Skåne Sjælland Linux User Group - http://www.sslug.dk Home   Subscribe   Mail Archive   Forum   Calendar   Search
MhonArc Date: [Date Prev] [Date Index] [Date Next]   Thread: [Date Prev] [Thread Index] [Date Next]   MhonArc
 

Re: SV: [PROGRAMMERING] Hvad koster en malloc og free



In <sslug@sslug> "Lars Riisgaard Ribe" <sslug@sslug> writes:

>[klip]
>> GNU's malloc er ret god til at håndtere den slags allokeringer. Min
>> erfaring er i al fald at det ikke er noget problem.
>> 
>Det lyder godt!
>> Alternativt kan man komme uden om det ved at erklære dit midlertidige
>> array som "static", og så kun malloc'e det første gang det kaldes.
>> Sådan noget som
>> 
>> void minfunc(void)
>> {
>> 	static float **mitarray = NULL;
>> 
>> 	if (mitarray == NULL) mitarray = malloc(96*sizeof(float));
>> 
>>  	/* Resten af din kode */
>> }
>> 
>> Ulempen ved *den* metode er så at funktionen ikke er re-entrant,
>> så det duer ikke hvis funktionen skal kunne kaldes nested.

>Det skal den ikke kunne. Men jeg har svært ved at se, hvordan jeg får
>free'et mit array?

Det gør du så ikke. Men det bliver kun allokeret een gang så der er ikke
nogen memory leak med den metode. Den overlader så bare oprydningen til
når programmet terminerer (så rydder operativsystemet op og frigiver al
memory fra processen).


Henrik



 
Home   Subscribe   Mail Archive   Index   Calendar   Search

 
 
Questions about the web-pages to <www_admin>. Last modified 2005-11-01, 02:01 CET [an error occurred while processing this directive]
This page is maintained by [an error occurred while processing this directive]MHonArc [an error occurred while processing this directive] # [an error occurred while processing this directive] *