Forum > General

Quickly set unit variables to their default value

<< < (2/2)

speter:

--- Quote from: dseligo on January 13, 2022, 12:54:25 am ---If he won't initialize anint and adouble in code then there is no initialization when InitDec is called multiple times.
And if there is no need for multiple initialization, you don't have to initialize variables to 0: global variables are initialized by FPC.

--- End quote ---
All true. :) You are correct, I should have included initialiation for anint and adouble.

cheers
S.

PS: Can anyone comment on whether "defaulting" a dynamic array (that hasn't had its length set) does anything useful?

--- 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";}};} ---adynamic := default(tdynamic);

dadycool:
I like special function as jamie and speter suggest but using record with default as per winni and Kays seems easier to maintain. I will evaluate both and see how it goes.

p/s: Wish this forum had a HUG button so I can HUG everyone with their excellent answers. All of your comments make sense and shed me light  :D

Thaddy:
@winni, a.o.:
FYI
AFAIK An important difference between Default() and fillChar etc is that Default() is an intrinsic, That means the compiler can generate less code or sometimes no code at all! :o
It will be resolved by the compiler at compile time and less or in some cases no code is executed by the program,
The fillchar is in effect delegated to the compiler.
So always use default() if it is available for the type. (I believe only file types are excluded)

This is documented for Delphi - hard to find, but it is - and I expect this is indeed the same in FPC and it looks like it is: compile with -al to see the difference in generated assembler. Default() generates considerably less code than FillChar.
See also wikipedia on intrinsics

PascalDragon:

--- Quote from: Kays on January 12, 2022, 11:30:48 pm ---
--- Quote from: winni on January 12, 2022, 10:59:42 pm ---[…]
--- 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";}};} ---[…]Fillchar (myrec, sizeof(myrec),0)[…]
--- End quote ---
Please use default. I’m not even sure if it will properly treat a record containing for instance an ANSIString (i.e. decrement reference count), but it’s definitely better than a blunt “erase everything with #0!”.

--- End quote ---

One needs to use Finalize(myrec) before doing the FillChar so that this works correctly. But yes, I agree that Default is the way to go.

Navigation

[0] Message Index

[*] Previous page

Go to full version