"Andreas Eibach" <> writes:
> "Nate Eldredge" <> schrieb im Newsbeitrag
> news:...
>> "Andreas Eibach" <> writes:
>> > 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,
>
> You're right about that it's shortened a lot.
>
>> and that you really have
>> malloc(sizeof(unsigned long) * n) to allocate an array of n unsigned
>> longs.
>
> Eh? I have a LASTLW amount of longs, and LASTLW is a #define'd constant, e.
> g. 64. I do not use "hard-coded" numbers, it's a habit (but a good one this
> time, I guess
)
>
>> Allocating just one seems kind of weird, I suspect it wouldn't
>> be what you'd want.
>
> But I'm allocating LASTLW ones in a loop, you did notice the for() loop,
> didn't you?
> Come to think of it, I could get rid of the for loop and use n times the
> ULONGs in the malloc, if that is what you mean.
You have an array of LASTLW pointers, and you set each of them to point
to a single (malloc'ed) unsigned long. It would be easier just to have
an array of LASTLW unsigned longs.
>
>> free() does not know about your more
>> elaborate data structure and cannot dig down into it to find the things
>> you malloc'ed.
>
> Yes, that was my suspicion and the same what Malcolm already pointed out.
> Thanks to the both of you.
>
>> However, if this really is right before exiting, it is probably not
>> necessary to free this stuff at all [...]
>> It might still be a good idea to do it for maintainability reasons,
>> depending on your situation.
>
> I'd always do it. Period. Never "rely" on how well the garbage collection
> works.
> (except for JAVA, the GC of which I do fully trust)
This is a different thing from garbage collection. Garbage collection
relies on the compiler keeping track of all the pointers you malloc'ed,
knowing when they're no longer referenced, and freeing the memory when
needed. What I'm talking about is the operating system reclaiming every
byte of memory that belonged to your program, whether it was obtained by
malloc() or contained program code, all at one blow. There are no
subtleties and no wondering about how efficient it is. So as far as the
behavior of the program is concerned, that means that calling free()
just before exiting is a waste of code space, CPU time, and programmer
time.
Possible reasons why you might want to do it anyway:
1. Practice
2. In case you someday make the program do something more after the
structure is no longer needed, or repeat its process many times, or be
encapsulated in a library
3. In case you are using a primitive, embedded, or ancient system that
doesn't have a proper OS