2
votes

I have a projectgroup in which all Win32 programs have the same ..\PatchLibs search path. This folder contains a patched System.Win.Ctrl.pas containing:

{$IFDEF WIN32}
function _malloc(size: size_t): Pointer; cdecl;
begin
  if (size > MaxInt) then 
  begin                   
     Result := Nil
  end
  else
  begin
     try
        Result := AllocMem(size);
     except
        Result := Nil;
     end;
  end;
end;

[This patch suppresses an error in midaslib (QC 104337)]

The issue:

One of the (smaller) projects gives a W1023 ("comparing signed and unsigned types") compiler warning on the 'MaxInt' line, all others build without warnings.
None of the projects have System.Win.Ctrl in their uses statements or in their project files.

Thinking there might be two typed constant definitions for Maxint I wanted to prefix Maxint with the 'correct' unit name, but can't find its definition.
I have searched through all available c:\program files (x86)\embarcadero\rad studio\9.0\source*.* files, but found no definitions.
System.MaxInt works but does not eliminate the warning.
Typecasting Cardinal(MaxInt) removes the warning, but I'd still prefer the 'fully qualified' solution.
(size_t is defined as ULONG_PTR is defined as NativeUInt)
I found Quality Central issue 102873, 69836 and 53202 but these refer to duplicate definitions C++ .h header files

Is my assumption about more than one definition correct? If so, what would/should the unit prefix be? And most important: why do I get the compiler warning for that one project build only?

1

1 Answers

4
votes

MaxInt is declared in the System unit. I'm pretty sure that's the only MaxInt that is in scope here. The warning you see is accurate. MaxInt is signed and size_t is unsigned. You should suppress the warning. For example you could cast MaxInt to size_t:

if size > size_t(MaxInt) then

That's fine because MaxInt is within the range of values of size_t.

As an aside, I'd probably deal with the underlying issue by hooking the function that needs fixing, rather than re-compiling the entire unit. I personally find that to be less invasive and easier to maintain.


Why do I get the compiler warning for that one project build only?

Some ideas:

  1. You only have one project that includes that unit.
  2. You have different compiler options in different projects. Perhaps only one of your projects has warnings enabled, or only one project has that specific warning enabled.
  3. You only have one project with WIN32 defined.
  4. This file is only compiled once, but used multiple times. Perhaps because you are making rather building.

It's pretty hard to explain that part of your question. Irrespective, when the code in your question is compiled with warnings enabled, you will get that warning. You really are comparing signed and unsigned. So you really do need to suppress the warning with the method I provide above.

Remember that a warning does not mean that your code is broken. Suppressing that warning will not change the behaviour of the code. When you see that warning you analyse the code to check whether or not there is a problem. In this case, the code works fine and you just need to suppress the warning.