C - lokalis tomb, vagy pointer+malloc

Rancz Lajos csiga at fosch.com
Mon Jan 23 10:10:45 CET 2006


Helló!


WLC02E, írta:

>Sziasztok,
> 
>egy -talan hulye- kerdesem lenne dinamikus valtozohasznalattal
>kapcsolatban:
> 
>Mi a kulonbseg egy fuggvenyen belul definialt/hasznalt tomb, illetve
>pointer elettartama kozott? Funkciojat tekintve azt hiszem ertem a
>kulonbsegeket, de miutan mindegyik a hivas pillanataban jon letre es a
>fuggveny vegeztevel megszunik ezert gyakorlatilag nincs elonye egyik
>megoldasnak sem a masikkal szemben..
>
Sima C-ben (ahol nincs garbage collector) a függvény futása után nem 
szabadulnak fel a dinamikusan foglalt memória területek!!! A másik fő 
különbség, hogy a lokális változók a stackbena vannak, a dinamikusak 
pedig a heapen. Sokáig futó rendszerben (eg. beágyazott) nem ajánlatos 
dinamikusan memóriát használni, pl. MISRA nem is engedi. IAR doksi szerint:
"For each allocated memory block, a few bytes of data for administrative 
purposes is required. For applications that allocate a large number of 
small blocks, this administrative overhead can be substantial. There is 
also the matter of fragmentation; this means a heap where small sections 
of free memory is separated by memory used by allocated objects. It is 
not possible to allocate a new object if there is no piece of free 
memory that is large enough for the object, even though the sum of the 
sizes of the free memory exceeds the size of the object. Unfortunately, 
fragmentation tends to increase as memory is allocated and released. 
Hence, applications that are designed to run for a long time should try 
to avoid using memory allocated on the heap."

Szóval a malloc-kal vigyázni kell.

Üdv,
Lajos




More information about the Elektro mailing list