Forum > General

local variables initialization

(1/1)

440bx:
Hello,

I know there is a switch that causes FPC to "trash" local variables and, I was wondering if there was some option to inform FPC to zero out (instead of "trashing") all the local variables in a function.

Is there such a compiler option ?

Thank you for your help.


Martin_fr:
Well yes.... But not explicit, so not sure if future proof


--- Code: Text  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ----gt- -gtttt
-gt can have between 1 and 4 "t". And chooses one of 4 values for the trashing.
One of those values is zero. So trashing with 0 will have the effect you want...

-gt can build up. I.e.  "-gtt" or "-gt -gt" both means 2 "t". You could just give -gtttt, but if somehow there is another -gt before then you don't get zero (because you effectively have 5 "t" mod 4 means 1 "t")

-gt-  seems to clear the "t" completely.

Of course, if after your "-gt -gtttt" is any other -gt, then it breaks again.

And also, whilst I do recall having seen the 4 trash values somewhere, I do not know if that was in any official doc, and if they will forever be the same as they are now.

Trashing with 0 makes sense, if you check for uninitialized booleans. That way you trash them to false.
Testing your up with -gt and then -gtttt means you did trash the boolean once to true, and once to false. Improving chances to catch the uninitialized.

Mind, that I have also seen managed vars of type ansistring being affected by trashing.
I.e. an uninitialized ansistring (at least result) is no longer empty.

I do not know, if that happens with -gtttt
Nor which versions of fpc do that.

440bx:
Thank you Martin, I appreciate the thorough reply.

I stay as far away from -gt as I can.  That thing also trashes out parameters.

It would be nice if FPC offered a compiler option to zero-out local variables (and only those!).  Just dreaming... :)

Thank you again.

Jonas Maebe:

--- Quote from: Martin_fr on June 22, 2022, 12:30:39 am ---Mind, that I have also seen managed vars of type ansistring being affected by trashing.
I.e. an uninitialized ansistring (at least result) is no longer empty.

--- End quote ---
An unitialized ansistring result is never guaranteed to be empty. That's why the compiler prints a warning (rather than a hint) if you read it before writing it.

Thaddy:

--- Quote from: Jonas Maebe on June 22, 2022, 09:28:03 pm ---
--- Quote from: Martin_fr on June 22, 2022, 12:30:39 am ---Mind, that I have also seen managed vars of type ansistring being affected by trashing.
I.e. an uninitialized ansistring (at least result) is no longer empty.

--- End quote ---
An unitialized ansistring result is never guaranteed to be empty. That's why the compiler prints a warning (rather than a hint) if you read it before writing it.

--- End quote ---
Then again, string vars - and not only those -  can be initialized at declaration.
--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---procedure someprocedure;var  s:string = '';// initialized, also for stack.beginend;But even if you correctly use the above for all local vars, stack thrashing can be useful to catch your unexpected errors.
Maybe note initialization at declaration can slow down your code just a bit in the case of managed types.

Navigation

[0] Message Index

Go to full version