Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 222
  • Last Modified:

Problem with System.MOVE

I have a problem with function MOVE from System.pas :o(

Here sample of my project for Delphi4.

If you will press BitBtn1, you will see
a result of execution function MOVE().

Memo1 not equal Memo2 :o(
Where is the problem?

---------------cut---------------
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, Buttons;

type
  TForm1 = class(TForm)
    Memo1: TMemo;
    Memo2: TMemo;
    BitBtn1: TBitBtn;
    procedure BitBtn1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  R,D   : PChar;
  Length: integer;

implementation

{$R *.DFM}

procedure TForm1.BitBtn1Click(Sender: TObject);

begin
  Length := System.Length(Memo1.Lines.Text);
  GetMem(D, Length);
  R := Memo1.Lines.GetText;
  System.Move( R^, D^, Length);
  Memo2.Lines.Text := string(D);
  FreeMem(D);
end;

end.
---------------cut---------------
object Form1: TForm1
  Left = 106
  Top = 115
  Width = 696
  Height = 480
  Caption = 'Form1'
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = False
  PixelsPerInch = 96
  TextHeight = 13
  object Memo1: TMemo
    Left = 144
    Top = 32
    Width = 425
    Height = 105
    Lines.Strings = (
      '123456')
    TabOrder = 0
  end
  object Memo2: TMemo
    Left = 144
    Top = 152
    Width = 425
    Height = 145
    TabOrder = 1
  end
  object BitBtn1: TBitBtn
    Left = 48
    Top = 40
    Width = 75
    Height = 25
    Caption = 'BitBtn1'
    TabOrder = 2
    OnClick = BitBtn1Click
  end
end

---------------cut---------------
0
DValery
Asked:
DValery
  • 3
  • 3
  • 2
  • +1
1 Solution
 
JaymolCommented:
Why are you doing it like that?

What's wrong with...

    Memo2.Lines:=Memo1.Lines;

John.
0
 
EpsylonCommented:
Use this:

var length: Cardinal;
  R,D   : PChar;
begin
  R := Memo1.Lines.GetText;
  Length := strlen(R) + 1;
  GetMem(D, Length);
  System.Move(R^, D^, Length);
//  StrCopy(D, R);  can be used too
  Memo2.Lines.Text := string(D);
  FreeMem(D);
end;


I tested it and it seems to work  :o)

Regards,

Epsylon.
0
 
EpsylonCommented:
John, I think it is just for testing the move function...
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
DValeryAuthor Commented:
It's only example that is sowing problem.
I'm using MOVE() for copy blocks of data (not string). Block data source not equal block data destination :o(

If you will make break point on string: "Memo2.Lines.Text := string(D);"
then you will can see the result execution MOVE() in R & D pointers.
0
 
shenqwCommented:
Your code works fine on d5+pack1
0
 
EpsylonCommented:
??????

What's wrong with it????
0
 
DValeryAuthor Commented:
I try it on d5+pack1 and here result:
R:
"123456
"
D:
"'123456
,>ΒΌ'"
0
 
shenqwCommented:
yes ,i got it.

i think you must do like follow:

  Length := System.Length(Memo1.Lines.Text);
  GetMem(D, Length+1);
  R := Memo1.Lines.GetText;
  ZeroMemory(D,Length+1);
  System.Move( R^, D^, Length);
  Memo2.Lines.Text := string(D);
  FreeMem(D);

//string(D); Convert PChar to string,It only end when meet #0.

0
 
DValeryAuthor Commented:
Thanks shenqw!
I saw my error ;o)
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now