Transport Tycoon Forums

The place to talk about Transport Tycoon
It is currently Wed Jun 19, 2013 3:35 am

All times are UTC




Post new topic Reply to topic  [ 7 posts ] 
Author Message
PostPosted: Sun Nov 28, 2010 4:38 pm 
Offline
Engineer
Engineer

Joined: Sun Oct 25, 2009 9:56 am
Posts: 58
Location: Belgium
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:
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.
--------------------------------------------------


Top
 Profile  
 
PostPosted: Sun Nov 28, 2010 4:50 pm 
Offline
OpenTTD Developer
OpenTTD Developer

Joined: Thu Dec 20, 2007 12:49 pm
Posts: 3653
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;


Top
 Profile  
 
PostPosted: Sun Nov 28, 2010 4:58 pm 
Offline
Engineer
Engineer

Joined: Sun Oct 25, 2009 9:56 am
Posts: 58
Location: Belgium
k, thanks for the answer

_________________
--------------------------------------------------
MailAI, a casual postal service for openTTD.
--------------------------------------------------


Top
 Profile  
 
PostPosted: Sun Nov 28, 2010 5:58 pm 
Offline
Tycoon
Tycoon
User avatar

Joined: Thu Jun 25, 2009 4:42 pm
Posts: 2244
Location: Suffolk, UK
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

  • Unix *is* user-friendly. It is not ignorant-friendly and idiot-friendly.
  • Duct tape is like the force. It has a light side, a dark side, and it holds the universe together... -- Carl Zwanzig
  • I am always doing that which I cannot do, in order that I may learn how to do it. -- Pablo Picasso
  • Time LINE...? Ehh, time isn't made out of LINES. It is made out of circles. That is why clocks are round! -- Caboose, RvB


Top
 Profile  
 
PostPosted: Sun Nov 28, 2010 6:12 pm 
Offline
OpenTTD Developer
OpenTTD Developer

Joined: Thu Feb 09, 2006 7:15 pm
Posts: 3712
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).


Top
 Profile  
 
PostPosted: Sun Nov 28, 2010 6:41 pm 
Offline
Tycoon
Tycoon
User avatar

Joined: Thu Jun 25, 2009 4:42 pm
Posts: 2244
Location: Suffolk, UK
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

  • Unix *is* user-friendly. It is not ignorant-friendly and idiot-friendly.
  • Duct tape is like the force. It has a light side, a dark side, and it holds the universe together... -- Carl Zwanzig
  • I am always doing that which I cannot do, in order that I may learn how to do it. -- Pablo Picasso
  • Time LINE...? Ehh, time isn't made out of LINES. It is made out of circles. That is why clocks are round! -- Caboose, RvB


Top
 Profile  
 
PostPosted: Mon Nov 29, 2010 7:54 pm 
Offline
Chief Executive
Chief Executive

Joined: Sat Jul 18, 2009 5:54 pm
Posts: 647
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.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 7 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 7 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  

Powered by phpBB © 2000-2013 phpBB Group

Copyright © Owen Rudge/The Transport Tycoon Forums 2001-2013.
Hosted by Zernebok Hosting.