The variable may not have been initialized. Can I enable this warning for a string?

When I compile this code

{$WARNINGS ON} function Test(s: string): string; var t: string; d: double; begin if s = '' then begin t := 'abc'; d := 1; end; Result := t + FloatToStr(d); end; 

I get the warning "Variable" d "may not have been initialized", but I do not get the same warning for the variable "t". This seems inconsistent. This code is just a simple example for displaying compiler warnings, but I just found an error in my live code that would be caught by a compilation warning for uninitialized string variables. Can I somehow switch this warning in Delphi 6? Or in a newer version of Delphi?

+7
source share
2 answers

No, there is no switch for this. A warning does not occur because the string is a compiler-driven type and is always initialized by the compiler.

+12
source

Yes: -)

Use shortstrings or pChars

 {$WARNINGS ON} function Test: String; var p: pChar; d: double; begin Result := p + FloatToStr(d); end; //This code will give a warning. 

Seriously

No, normal lines and short Delphi lines are automatically initialized to '' (empty line). Short circuits live on the stack and do not need to be cleared. Other lines are called so-called β€œmanaged” types and are automatically deleted when they are no longer used using reference counting.

PChars, good news
pChars are just pointers. Delphi does not manage them.
However, Delphi does automatically convert them to strings and vice versa.

p issues bad news
If you convert pChar to a string , Delphi copies the contents of pChar to a string, and you are still responsible for destroying pChar.
Also note that this copying takes time, and if you do, it will slow down your code.

If you convert the string to pChar , Delphi will give you a pointer to the address where the string lives. AND!! Delphi will cease to control the line. You can still assign values ​​to a row, but it will no longer automatically grow.

From: http://www.marcocantu.com/epascal/English/ch07str.htm

The following code will not work as expected:

 procedure TForm1.Button2Click(Sender: TObject); var S1: String; begin SetLength (S1, 100); GetWindowText (Handle, PChar (S1), Length (S1)); S1 := S1 + ' is the title'; // this won't work Button1.Caption := S1; end; 

This program compiles, but when you start it, you are surprised: the button label will have the source text of the window name without the text of the constant line that you added to it. The problem is that when Windows writes to a string (as part of the GetWindowText API GetWindowText ), it does not set the length of the long Pascal string properly. Delphi can still use this line for output and can figure out when it will end looking for a null terminator, but if you add extra characters after the null terminator, they will be completely skipped.

How can we fix this problem? The solution is to tell the system to convert the string returned by the GetWindowText API GetWindowText back to the Pascal string. However, if you write the following code:

 S1 := String (S1); 

the system will ignore it, since converting the data type back to itself is a futile operation. To get the correct long Pascal string, you need to rewrite the string to PChar and let Delphi correctly convert it back to string:

 S1 := String (PChar (S1)); 

In fact, you can skip string conversion because PChar-to-string conversions are automatically performed in Delphi. Here is the final code:

 procedure TForm1.Button3Click(Sender: TObject); var S1: String; begin SetLength (S1, 100); GetWindowText (Handle, PChar (S1), Length (S1)); S1 := String (PChar (S1)); S1 := S1 + ' is the title'; Button3.Caption := S1; end; 

An alternative is to reset the Delphi string length using the PChar string length by writing:

 SetLength (S1, StrLen (PChar (S1))); 
+3
source

All Articles