Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8683113
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T21:57:23+00:00 2026-06-12T21:57:23+00:00

Consider the code below which compiles and runs without error in Delphi 6. When

  • 0

Consider the code below which compiles and runs without error in Delphi 6. When I recover the dynamic string array, instead of seeing an empty array in sa, I see an array with a length of 1 with a single element containing an empty string. Why is this and how can I safely assign a NIL dynamic array to a Variant and recover it properly? Here’s the code:

TDynamicStringArray = array of string;

var
    V: Variant;
    sa: TDynamicStringArray;
begin
    sa := nil;

    V := sa;

    sa := V;

    OutputDebugString('sa has a single element now with an empty string in it when I expect it to be empty.');
end;
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T21:57:24+00:00Added an answer on June 12, 2026 at 9:57 pm

    There are two bugs here.

    First of all in Variants.DynArrayVariantBounds. When the dynamic array is nil this erroneously returns a low/high bounds pair of (0, 0). It should return (0, -1). This bug is fixed in the latest versions of Delphi. That causes V := sa to return a variant array with a single, empty, element.

    The second bug affects the other direction, sa := V. This bug is still present in the latest versions of Delphi. This bug is in Variants.DynArrayFromVariant. There is a repeat/until loop which walks over the input variant array and populates the output dynamic array. When the input variant array is empty, it should not enter that repeat/until loop. However, the code erroneously does so and attempts to read an element of the variant array with VarArrayGet. Since the array is empty, that provokes a runtime error. I have reported this: QC#109445.

    Here is a very simply bit of code that fixes the bugs. Note that I have only consider the case where the arrays are one dimensional. If you need to support higher dimensional arrays then you can extend this approach to do so.

    program Project1;
    
    {$APPTYPE CONSOLE}
    
    uses
      Variants;
    
    var
      OriginalVarFromDynArray: procedure(var V: Variant; const DynArray: Pointer; TypeInfo: Pointer);
      OriginalVarToDynArray: procedure(var DynArray: Pointer; const V: Variant; TypeInfo: Pointer);
    
    function DynArrayVarType(typeInfo: PDynArrayTypeInfo): Integer;
    const
      tkDynArray  = 17;
    begin
      Result := varNull;
      if (typeInfo<>nil) and (typeInfo.Kind=tkDynArray) then
      begin
        Inc(PChar(typeInfo), Length(typeInfo.name));
        Result := typeInfo.varType;
        if Result=$48 then
          Result := varString;
      end;
      if (Result<=varNull) or (Result=$000E) or (Result=$000F) or ((Result>varInt64) and not (Result=varString)) then
        VarCastError;
    end;
    
    procedure VarFromDynArray(var V: Variant; const DynArray: Pointer; TypeInfo: Pointer);
    var
      VarType, DynDim: Integer;
    begin
      DynDim := DynarrayDim(PDynArrayTypeInfo(TypeInfo));
      if DynDim=1 then
      begin
        //only attempt to deal with 1 dimensional arrays
        if DynArray=nil then begin
          VarClear(V);
          VarType := DynArrayVarType(PDynArrayTypeInfo(TypeInfo));
          if VarType = varString then
            VarType := varOleStr;
          V := VarArrayCreate([0, -1], VarType);
          exit;
        end;
      end;
      OriginalVarFromDynArray(V, DynArray, TypeInfo);
    end;
    
    procedure VarToDynArray(var DynArray: Pointer; const V: Variant; TypeInfo: Pointer);
    var
      DimCount: Integer;
      Len: Integer;
    begin
      DimCount:= VarArrayDimCount(V);
      if DimCount=1 then
      begin
        //only attempt to deal with 1 dimensional arrays
        Len := VarArrayHighBound(V, 1) - VarArrayLowBound(V, 1) + 1;
        if Len=0 then begin
          DynArraySetLength(DynArray, PDynArrayTypeInfo(TypeInfo), 1, @Len);
          exit;
        end;
      end;
      OriginalVarToDynArray(DynArray, V, TypeInfo);
    end;
    
    procedure FixVariants;
    var
      VarMgr: TVariantManager;
    begin
      GetVariantManager(VarMgr);
      OriginalVarFromDynArray := VarMgr.VarFromDynArray;
      VarMgr.VarFromDynArray := VarFromDynArray;
      OriginalVarToDynArray := VarMgr.VarToDynArray;
      VarMgr.VarToDynArray := VarToDynArray;
      SetVariantManager(VarMgr);
    end;
    
    type
      TDynamicStringArray = array of string;
    
    var
      V: Variant;
      sa: TDynamicStringArray;
    begin
      FixVariants;
    
      sa := nil;
      V := sa;
      sa := V;
    
      Writeln(Length(sa));
      Readln;
    end.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider the code below. I need to remove the error which is coming because
Consider the code below, private void Convert_Click(Object sender, RoutedEventArgs e) { string[] strCmdLineParams =
I want to determine which attribute to look-up dynamicaly, Consider below python code: def
Consider the code below (which has been simplified). I have a service class that
Consider the below code snippet: for(i=0;i<10;i+=2) // 1 for(i=0;i<2;i=i+2) // 2 Which one will
Consider the code below. I don't understand why my GCC compiler does not try
Consider the JavaScript code below, inspired from the YUI documentation on YAHOO.lang.extend . In
Consider the Java code below, what would happen if there were no paintComponent method
I am having trouble finding an answer to this. Consider the clipping code below:
I have a code snippet as below <CheckBox Name=cb Margin=1,2,1,0 IsChecked={Binding Path=IsManager} IsEnabled=True/> Consider

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.