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

  • Home
  • SEARCH
  • 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 273043
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T00:23:49+00:00 2026-05-12T00:23:49+00:00

I wrote two methods with a void type parameter: procedure Method1(const MyVar; size: cardinal);

  • 0

I wrote two methods with a void type parameter:

procedure Method1(const MyVar; size: cardinal);
var
  Arr: array of byte;
begin
  SetLength(Arr, size);
  {now copy the data from MyVar to Arr, but how?}  
end;

procedure Method2(var MyVar; size: cardinal);
var
  Arr: array of byte;
begin
  SetLength(Arr, size);
  {return the data from the array, but how?}
end;

In the first one I would like to access the MyVar as an array of byte. In the second one, I would like to copy the data from the local array Arr to MyVar. Therefore I used the CopyMemory() function, but something is wrong with it.

If I use the following in the second method, it is fine as long as Method2 is called with an array as its parameter (Method2(Pointer(MyString)^, Length(MyString)) or Method2(Pointer(MyArray), Length(MyArray))).

CopyMemory(Pointer(MyVar), Pointer(Arr), size);

If I call Method2 with a, for instance, integer parameter (Method2(MyInteger, SizeOf(MyInteger))), it does not work properly. In this case, the CopyMemory() has to be called this way:

CopyMemory(@MyVar, Pointer(Arr), size);

How to return data from Method2 correctly not knowing whether it is a simple type (or record) or an array? The situation will be similar in Method1, but here I would have to use

CopyMemory(Pointer(Arr), Pointer(MyVar), size);

in case of arrays and

CopyMemory(Pointer(Arr), @MyVar, size);

in case of simple types.

What can I do about it when I don’t know what the MyVar parameter is?

  • 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-05-12T00:23:50+00:00Added an answer on May 12, 2026 at 12:23 am

    There’s no such thing a a void type in Delphi. What you’re referring to is called an untyped parameter.

    An untyped parameter is always the actual thing itself, not a pointer to the thing you’re supposed to use. Therefore, the correct way to use CopyMemory with such a parameter is to apply the @ operator to it, like so:

    CopyMemory(@MyVar, @Arr[0], size);
    

    Notice I’ve also changed the way I pass the second parameter. It’s better if you don’t rely on the fact that a dynamic array is really a pointer to the first element. If you need a pointer to the first element, just say so explicitly, like I did here.

    Your confusion comes from a test you made where the parameter was used as though it were a pointer, and the test appeared to work. I doubt the validity of that test, though. When you said Pointer(MyString)^, what you had was the first character of the string. When you then said Pointer(MyVar) inside the function, you were type-casting that character into a pointer, which was an invalid type-cast. If your program appeared to work, then it was only by accident; your code was wrong.

    The best advice I can give is to not type-cast stuff unless you really have to. And when you do, please be sure that the thing you’re type-casting really does have that type. In your case, you don’t need to type-cast anything before you pass it as an untyped parameter. You can call Method1 like this:

    Method1(MyString[1], Length(MyString) * Sizeof(Char));
    

    (I multiply by SizeOf(Char) since I don’t know whether you have Delphi 2009 or not.)

    Also, do avoid untyped parameters. One of the great things a compiler can do to help ensure program correctness is to enforce type safety, but when you take away the types, the compiler can’t help you anymore.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 159k
  • Answers 159k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer That's a restriction that the Blackberry API got from the… May 12, 2026 at 11:24 am
  • Editorial Team
    Editorial Team added an answer If you want to post to the server use jquery's… May 12, 2026 at 11:24 am
  • Editorial Team
    Editorial Team added an answer This CodeProject article shows how to send keystrokes to an… May 12, 2026 at 11:24 am

Related Questions

Suppose I've got a method that accepts an array and processes each element in
Background I have a container class which uses vector<std::string> internally. I have provided a
I'd like to write two distinct functions to handle a constant value and a
When writing async method implementations using the BeginInvoke/EndInvoke pattern the code might look something

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.