Delphi Pages Forums  

Go Back   Delphi Pages Forums > Delphi Forum > General

Lost Password?

Reply
 
Thread Tools Display Modes
  #1  
Old 08-18-2018, 09:36 AM
oxygen oxygen is offline
Junior Member
 
Join Date: May 2018
Posts: 8
Question Problem to allocate memory

Hello, I have this code

Code:
type TBytes = array of byte;
    type PTBytes = ^TBytes;
    procedure appendConstantData(pData: PTBytes; sid: Byte; grp: Byte =     1; dataLength: Byte = 1; spec: Byte = 0);
    
    var pData: PTBytes;
    begin
    setLength(pData^, 1);
      for n := 3 to 8 do
        begin
          ss := 'b:\prefixes_' + inttostr(n) + '.txt';
          Needles.LoadFromFile(ss);
          setLength(pData^, needles.count * n);
          for i:= 0 to needles.count-1 do
            move(Needles[i][1], pData^[n*i], n );
          WMD.appendConstantData( pData, n-3, 0, n, n);
        end;
      WMD.fileWriteClose;
On the line with setLength(pData^, 1) is a bug. It skips the loop and continues after the code. I suppose it fails to allocate memory. How to fix it?

Another try:
Code:
  setLength(Data, 1);
//  PData := Data^;
  for n := 3 to 8 do
    begin
      ss := 'b:\prefixes_' + inttostr(n) + '.txt';
      Needles.LoadFromFile(ss);
      setLength(Data, needles.count * n);
      for i:= 0 to needles.count-1 do
        move(Needles[i][1], Data[n*i], n );
      WMD.appendConstantData( data, n-3, 0, n, n);
    end;
Error near WMD.appendConstantData( data,
Incompatible types: 'Array' and 'TBytes'

Last edited by oxygen; 08-18-2018 at 10:01 AM.
Reply With Quote
  #2  
Old 08-31-2018, 03:22 AM
Glenn1234 Glenn1234 is offline
Senior Member
 
Join Date: Aug 2009
Posts: 130
Default

SetLength doesn't allocate memory. It's a procedure that sets the length of a string.


You need to look into using New/GetMem and Dispose/Freemem.
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On

Forum Jump


All times are GMT. The time now is 01:45 AM.


Powered by vBulletin® Version 3.8.8
Copyright ©2000 - 2018, vBulletin Solutions, Inc.