Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Should you free your pointers?

Reply
Thread Tools

Should you free your pointers?

 
 
Billy Mays
Guest
Posts: n/a
 
      06-24-2011
In the case where you know that your program will be terminating after
it is done using the pointers, should you even bother freeing the space up?

Example:
int main(void)
{
int i, * data;
data = malloc(sizeof(int) * 100);
if(data) {
for(i = 0; i < 100; i++) {
data[i] = i'
}
}
/* free(data) ? */
return 0;
}

--
Bill
 
Reply With Quote
 
 
 
 
Shao Miller
Guest
Posts: n/a
 
      06-24-2011
On 6/24/2011 16:41, Billy Mays wrote:
> In the case where you know that your program will be terminating after
> it is done using the pointers, should you even bother freeing the space up?
>
> Example:
> int main(void)
> {
> int i, * data;
> data = malloc(sizeof(int) * 100);
> if(data) {
> for(i = 0; i < 100; i++) {
> data[i] = i'
> }
> }
> /* free(data) ? */
> return 0;
> }


I've seen arguments for:
- Program performance
- Program size

And against:
- Programmer habit
- Cases where program termination doesn't free memory allocated by the
program

I think it's a worth-while habit to 'free' them, in general.
 
Reply With Quote
 
 
 
 
Nobody
Guest
Posts: n/a
 
      06-24-2011
On Fri, 24 Jun 2011 16:41:00 -0400, Billy Mays wrote:

> In the case where you know that your program will be terminating after
> it is done using the pointers, should you even bother freeing the space up?


No.

In complex programs where freeing data may be mixed with other clean-up,
I may set __free_hook to an empty function when termination is imminent.

 
Reply With Quote
 
Shao Miller
Guest
Posts: n/a
 
      06-24-2011
On 6/24/2011 16:51, Shao Miller wrote:
> On 6/24/2011 16:41, Billy Mays wrote:
>> In the case where you know that your program will be terminating after
>> it is done using the pointers, should you even bother freeing the
>> space up?
>>
>> Example:
>> int main(void)
>> {
>> int i, * data;
>> data = malloc(sizeof(int) * 100);
>> if(data) {
>> for(i = 0; i < 100; i++) {
>> data[i] = i'
>> }
>> }
>> /* free(data) ? */
>> return 0;
>> }

>
> I've seen arguments for:
> - Program performance
> - Program size
>
> And against:
> - Programmer habit
> - Cases where program termination doesn't free memory allocated by the
> program
>
> I think it's a worth-while habit to 'free' them, in general.


Uh, I meant "for not bothering" and "against not bothering."
 
Reply With Quote
 
Ben Pfaff
Guest
Posts: n/a
 
      06-24-2011
Billy Mays <(E-Mail Removed)> writes:

> In the case where you know that your program will be terminating after
> it is done using the pointers, should you even bother freeing the
> space up?


One reason that I sometimes bother is that it's very clear that a
program that frees all of its memory does not have a memory leak
(when I use a tool that reports memory statistics at program
exit).
--
Ben Pfaff
http://benpfaff.org
 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      06-24-2011
On 06/25/11 08:41 AM, Billy Mays wrote:
> In the case where you know that your program will be terminating after
> it is done using the pointers, should you even bother freeing the space up?


Code fragments that will never be reused often are, so get into good
habits early!

--
Ian Collins
 
Reply With Quote
 
Martin Ambuhl
Guest
Posts: n/a
 
      06-24-2011
On 6/24/2011 4:41 PM, Billy Mays wrote:
> In the case where you know that your program will be terminating after
> it is done using the pointers, should you even bother freeing the space up?


You should always free any space you have allocated. There is no
guarantee that the OS will do it for you. Even if the OS you are
currently using claims it will do so, relying on such behavior makes
your code non-portable and there is no warrant in the C standard for
expecting otherwise (since the C standard can hardly decree such this
for the OS).

> Example:


No example is needed.

 
Reply With Quote
 
Martin Ambuhl
Guest
Posts: n/a
 
      06-24-2011
On 6/24/2011 4:53 PM, Nobody wrote:
> On Fri, 24 Jun 2011 16:41:00 -0400, Billy Mays wrote:
>
>> In the case where you know that your program will be terminating after
>> it is done using the pointers, should you even bother freeing the space up?

>
> No.
>
> In complex programs where freeing data may be mixed with other clean-up,
> I may set __free_hook to an empty function when termination is imminent.
>


It is appropriate that this incorrect answer came from "Nobody".

 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      06-25-2011
On 6/24/2011 4:41 PM, Billy Mays wrote:
> In the case where you know that your program will be terminating after
> it is done using the pointers, should you even bother freeing the space up?


This is Question 7.24 on the comp.lang.c Frequently Asked
Questions (FAQ) page at <http://www.c-faq.com/>.

--
Eric Sosman
http://www.velocityreviews.com/forums/(E-Mail Removed)d
 
Reply With Quote
 
James Kuyper
Guest
Posts: n/a
 
      06-25-2011
On 06/24/2011 04:41 PM, Billy Mays wrote:
> In the case where you know that your program will be terminating after
> it is done using the pointers, should you even bother freeing the space up?
>
> Example:
> int main(void)
> {
> int i, * data;
> data = malloc(sizeof(int) * 100);
> if(data) {
> for(i = 0; i < 100; i++) {
> data[i] = i'
> }
> }
> /* free(data) ? */
> return 0;
> }


It's not necessary, but I think it's easier to not make a special case
of main(); the rules for creating well-written code are easier to
remember if they don't have more special cases than they have to.

Also, there's always the possibility that code from main() may someday
get moved off to another function; if that code already contains
properly connected malloc() and free() calls, that's one less thing to
worry about during the move.
--
James Kuyper
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
don't like, so don't waste your time thinking and buy them. You know,if you don't like one thing, it means nothing for you.</p> fashion t shirts seller Cisco 0 06-13-2011 02:01 AM
Free SEO -Free dictionary list -Add a link to your website for free george HTML 0 08-29-2008 04:55 PM



Advertisments