Use "free(p)" instead of "if (p != NULL) free(p)"

Discuss the new AI features ("NoAI") introduced into OpenTTD 0.7, allowing you to implement custom AIs, and the new Game Scripts available in OpenTTD 1.2 and higher.

Moderator: OpenTTD Developers

Post Reply
Hephi
Engineer
Engineer
Posts: 72
Joined: 25 Oct 2009 09:56
Location: Belgium

Use "free(p)" instead of "if (p != NULL) free(p)"

Post by Hephi »

Hi, in an attempt to clean up my code as I'm getting closer to putting up a first version of my AI I was studying the style guideline.http://wiki.openttd.org/Coding_style

At a certain point it talks about: Use "free(p)" instead of "if (p != NULL) free(p)"
and I was wondering if that's some C++ left in the guideline or if it's actually squirrel.
Also, what does it do exactly? Is it the same as putting free = null; in the code?

I tried searching but only thing I can find is about free variables which is not the same.
I include it anyway because I think it's nifty and would like to try and use it once. :)

Free variables
Free variables are variables referenced by a function that are not visible in the function scope. In the following example the function foo() declares x, y and testy as free variables.

Code: Select all

local x=10,y=20
testy <- “I’m testy”

function foo(a,b):(x,y,testy)
{
    ::print(testy);
    return a+b+x+y;
}

The value of a free variable is frozen and bound to the function when the function is created; the value is passed to the function as implicit parameter every time is called.
--------------------------------------------------
MailAI, a casual postal service for openTTD.
--------------------------------------------------
Yexo
Tycoon
Tycoon
Posts: 3663
Joined: 20 Dec 2007 12:49

Re: Use "free(p)" instead of "if (p != NULL) free(p)"

Post by Yexo »

The style guide you link to is about the OpenTTD code (which is C++), it has no relation at all with any NoAI code you write. Of course you can use it also for your AI project, but whether you do so is completely up to you. If you do, ignore those parts which are not applicable (like the part about free).

In C++, free(p) is different from p=NULL;
Hephi
Engineer
Engineer
Posts: 72
Joined: 25 Oct 2009 09:56
Location: Belgium

Re: Use "free(p)" instead of "if (p != NULL) free(p)"

Post by Hephi »

k, thanks for the answer
--------------------------------------------------
MailAI, a casual postal service for openTTD.
--------------------------------------------------
User avatar
Lord Aro
Tycoon
Tycoon
Posts: 2369
Joined: 25 Jun 2009 16:42
Location: Location, Location
Contact:

Re: Use "free(p)" instead of "if (p != NULL) free(p)"

Post by Lord Aro »

WARNING: N00bish question approaching! :lol:
Yexo wrote: In C++, free(p) is different from p=NULL;
How so?
AroAI - A really feeble attempt at an AI

It is practically impossible to teach good programming to students that have had a prior exposure to BASIC: as potential programmers they are mentally mutilated beyond hope of regeneration. --Edsger Dijkstra
Rubidium
OpenTTD Developer
OpenTTD Developer
Posts: 3815
Joined: 09 Feb 2006 19:15

Re: Use "free(p)" instead of "if (p != NULL) free(p)"

Post by Rubidium »

Lord Aro wrote:How so?
free(p) is like removing wallpaper, p=NULL is like putting wallpaper over other wallpaper. With free(p) the old wallpaper is gone, with p=NULL the old wallpaper is still there you just can't access it anymore (ever).
User avatar
Lord Aro
Tycoon
Tycoon
Posts: 2369
Joined: 25 Jun 2009 16:42
Location: Location, Location
Contact:

Re: Use "free(p)" instead of "if (p != NULL) free(p)"

Post by Lord Aro »

Rubidium wrote:
Lord Aro wrote:How so?
free(p) is like removing wallpaper, p=NULL is like putting wallpaper over other wallpaper. With free(p) the old wallpaper is gone, with p=NULL the old wallpaper is still there you just can't access it anymore (ever).
so with p=NULL, the original variable can possibly be found, but with free(p), it's eradicated from existence?
AroAI - A really feeble attempt at an AI

It is practically impossible to teach good programming to students that have had a prior exposure to BASIC: as potential programmers they are mentally mutilated beyond hope of regeneration. --Edsger Dijkstra
Brumi
President
President
Posts: 921
Joined: 18 Jul 2009 17:54

Re: Use "free(p)" instead of "if (p != NULL) free(p)"

Post by Brumi »

Something like that, but most importantly, with p = NULL, your object remains in the memory, thus causing a memory leak. Practically you cannot find p anymore because you've lost all your references to it. free(p) frees the memory associated to p, and your object is eradicated from existence.
Post Reply

Return to “OpenTTD AIs and Game Scripts”

Who is online

Users browsing this forum: No registered users and 9 guests