We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

[Rant] Funny limitations of Delphi 6...

Wim ten Brink
on
Medium Priority
293 Views
Last Modified: 2012-05-05
Well, I happen to be working on a big project and one function tends to be a bit complex. And I get this weird error:

[Warning] ImportExportDataModule.pas(1013): Return value of function 'Limitation' might be undefined

Well, I simplified the whole code to the following:

function Limitation: string;
var
  V00, V01, V02, V03, V04, V05, V06, V07, V08, V09: string;
  V10, V11, V12, V13, V14, V15, V16, V17, V18, V19: string;
  V20, V21, V22, V23, V24, V25, V26, V27, V28, V29: string;
  V30, V31: string;
begin
  Result := '';
end;

And yes, I set the return value of this function correctly. And I get about 30 hints about unused variables. But ignore the hints. Focus on the warning! Why does it generate this warning?

Well, a simple count of the number of variables within this function just reveals that I have 32 variables. Plus one for the result, making a total of 33. If I would remove any one of them then my problem is gone! No more warning by just removing one of them...

It makes me feel real sad though to realise that Delphi has such a strange limitation. Makes me wonder why it gives an error and if it has any effects on how the generated code will run. It's one of those things that make me feel real uncomfortable inside...

Oh, well. Done with the ranting. Time for the question... Where does this limit come from, where is it documented and what will happen if I just ignore it? And what happens with procedures that have over 32 variables? Do they have similar limitations too?

But most important question: Can this warning be ignored without ANY risks?
Comment
Watch Question

Lukasz Zielinskisoftware developer
CERTIFIED EXPERT
Top Expert 2008

Commented:
first of all why use 32 string variables?? isn't it better to use TStringList???

ziolko
Lukasz Zielinskisoftware developer
CERTIFIED EXPERT
Top Expert 2008

Commented:
...or array[0..31] of string
ziolko.
Lukasz Zielinskisoftware developer
CERTIFIED EXPERT
Top Expert 2008

Commented:
You can go to Project -> Options -> Complier -> Syntax Options and uncheck Strict var-strings and warning should go away :)
ziolko.
Wim ten BrinkSelf-employed developer
CERTIFIED EXPERT

Author

Commented:
@ziolko, I already have a solution! (Reduce number of variables) but I'm asking about the "Why?"... :-)

The datatype used for the local variables matters little. What matters is the datatype of the function itself. If I return an integer, pointer, object then I don't get this warning. If I return a string, a variant or an IUnknown interface then I will get this warning. It has to do with the way Delphi puts them on the stack or whatever. The following code will generate the same result:

function Limitation: IUnknown;
var
  V00, V01, V02, V03, V04, V05, V06, V07, V08, V09,
  V10, V11, V12, V13, V14, V15, V16, V17, V18, V19,
  V20, V21, V22, V23, V24, V25, V26, V27, V28, V29,
  V30, V31: boolean;
begin
  Result := nil;
end;

The original code used about 35 different variables. It needed 5 parameters (3 of them optional) and the other 30 variables all had their purpose. I did reduce them by using an array but had to alter some code for it to use an array instead. Basically, this function was one that had grown over time. It's purpose was to transform an XML file to another format using MSXML/XSLT and the XSLT file wanted several parameters. As things developed, more and more parameters were added to the XSLT file (most of them easily calculated) but every parameter resulted in one additional parameter in the local variables. Of course, the easy fix was putting all those parameters in a single array and then passing that array to the XSLT.

It is not that I need to have a solution here. I need reassurance that this warning has no effects on my code. I have become a bit suspicious now because Delphi didn't give me a more appropiate warning about this.

So, basically -half-answering my own question now- what happens is that the compiler will add space on it's stack for 32 local variables. Now, if the function result can be returned in the AH/AX/EAX registers of the processor (like with integers, booleans and pointers) or in ST(0) of the Floating point processor then Delphi won't generate this error. However, for other datatypes the compiler is acting a bit different. In those cases the compiler adds a temporary location (on the stack, I think) to contain this value and my fear is that this location is overwritten by one of the local variables, thus this would be a compiler bug. Or whatever...
Lukasz Zielinskisoftware developer
CERTIFIED EXPERT
Top Expert 2008

Commented:
yeah I think I've "jumped the gun" a bit, I haven't look who aksed this question:)
anyway, I strated palying with such a function and yup I've noticed that it's function result that matters:
- changed result to shortstring - no warning
- changed variables type to boolean (function result left as string) - warning is still there


ziolko.
Commented:
The astounding part is the last comment on QC Entry 735 (http://qc.borland.com/wc/qcmain.aspx?d=735)

"Still exists in Delphi 2006"

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Wim ten BrinkSelf-employed developer
CERTIFIED EXPERT

Author

Commented:
@ziolko: Yep, when I ask a Delphi question, it generally tends to be a lot more difficult than the average Q's here at EE. :-)

@Kyanar, thanks for finding that link! At least now I know that upgrading to D2006 would NOT solve this bug. Still leaves only one question, though. Does this warning mean that my data (or possibly the stack) might get corrupted if I just ignore it? In other words, is it save to ignore?
Wim ten BrinkSelf-employed developer
CERTIFIED EXPERT

Author

Commented:
Oh, well. Time to close this Q. We'll just have to wait for a response from Borland about this. Kyanar, thanks for the link!
Which version of Delphi are you using? I tried it on 2005 and I don't get the problem.
Wim ten BrinkSelf-employed developer
CERTIFIED EXPERT

Author

Commented:
It failed in Delphi 6 and apparantly it still goes wrong in Delphi 2006 although I haven't had a chance to test this...
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.