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 9111527
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T03:31:39+00:00 2026-06-17T03:31:39+00:00

Consider the following code: procedure Test; var MyPCharArray: array of PChar; begin SetLength(MyPCharArray, 3);

  • 0

Consider the following code:

procedure Test;
var
  MyPCharArray: array of PChar;

begin
  SetLength(MyPCharArray, 3);

  GetMem(MyPCharArray[0], 5);
  GetMem(MyPCharArray[1], 5);
  GetMem(MyPCharArray[2], 5);

  StrCopy(MyPCharArray[0], 'test');
  StrCopy(MyPCharArray[1], 'abcd');
  StrCopy(MyPCharArray[2], '1234');

  // Are these necessary?
  FreeMem(MyPCharArray[0], 5);
  FreeMem(MyPCharArray[1], 5);
  FreeMem(MyPCharArray[2], 5);
end;

Should the allocated elements be freed manually, or the compiler will free the array elements automatically when MyPCharArray goes out of scope?

  • 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-17T03:31:40+00:00Added an answer on June 17, 2026 at 3:31 am

    Indeed each of your calls to GetMem must be matched with a call to FreeMem.

    I’m not sure why you have this array. It’s not a type that I’d expect to see in pure Pascal code. So my guess is that you are passing the PChar array to some external library. In which case I’d declare an array of string as well as the array of PChar. And then make each PChar element by using PChar(…) on the corresponding element of the string array. Then you can avoid the StrCopy, GetMem and FreeMem.

    procedure CallLib(const str: array of string);
    var
      i: Integer;
      parr: array of PChar;
    begin
      SetLength(parr, Length(str));
      for i := 0 to high(parr) do
        parr[i] := PChar(str[i]);
      // call library now
    end;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider the following code: procedure TForm1.PaintBox1Paint(Sender: TObject); var vRect : TRect; vFormat : TTextFormat;
Consider the following code: var AryUser = new Array(); AryUser.prototype.HasUser = function() { if(this.length
Consider the following code when a new container is defined over a std::array #include
Consider the following psuedo T-SQL code (performed by a stored procedure): CREATE TABLE #localTable
Consider following code: char str[] = Hello\0; What is the length of str array,
Please consider the following code: type TFoo1 = class public procedure DoSomething1; end; TFoo2
consider the following code: window.stage = bonsai.run(document.getElementById('stage'), { code: function() { var circle; circle
Consider the following stored procedure : CREATE OR REPLACE FUNCTION get_supported_locales() RETURNS TABLE( code
Consider following code: My problem is: 1) I can't seem to cast the errors
Consider following code: enum size = 16; double[size] arr1 = [...]; double[size] arr2 =

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.