Mega Search
23.2 Million


Sign Up

Make a donation  
Re: Static vs Dynamic arrays  
News Group: embarcadero.public.delphi.language.delphi.general

On 19/01/2015 15:02, Tom Brunberg wrote:
> Gerald Holdsworth wrote:
>

> Please show the procedure(s) where you load original file, create a new one (file or array?) and
> save it to disk.

This has been concatenated for clarity. 'data' and 'fsize' are declared 
in the public section of the Form declaration, while 'F' and 'amt' are 
declared locally in the procedure. Also, the code is on the other 
computer, which is currently turned off. If I change it to how Jeff 
suggested (which is how the code now currently looks) it still works the 
same.
The code to save the data is only temporary, and if I access the array 
'data' later on (which I do), I still get the corrupted data.

var
  data: array of Byte;
  F: File;
  fsize, amt,ptr,file_size,l: Integer;
  file_buffer: array[0..$FFF] of Byte;
begin
  AssignFile(F,filename);
  Reset(F,1);
  fsize:=FileSize(F);
  file_size:=fsize;
  SetLength(data,fsize);
  ptr:=0;
  while file_size>0 do
  begin
   BlockRead(F,file_buffer,Length(file_buffer),amt);
   for l:=0 to amt-1 do
    data[l+ptr]:=file_buffer[l];
   file_size:=file_size-amt;
   ptr:=ptr+amt;
  end;
  CloseFile(F);
  AssignFile(F,filename+'test.dat');
  ReWrite(F,1);
  BlockWrite(F,data,fsize,amt);
  CloseFile(F);
end;

Vote for best answer.
Score: 0  # Vote:  0
Date Posted: 19-Jan-2015, at 11:23 AM EST
From: Gerald Holdsworth
 
Re: Static vs Dynamic arrays  
News Group: embarcadero.public.delphi.language.delphi.general
Gerald Holdsworth wrote:

> On 18/01/2015 21:15, Jeff Overcash (TeamB) wrote:
> > Gerald Holdsworth wrote:
> >> On 18/01/2015 14:50, Jeff Overcash (TeamB) wrote:
> >>> Gerald Holdsworth wrote:
> 
> >> It is just declared untyped. If I declare it as Byte, I need to change
> >> the second line to Reset(F) (it won't take a second parameter for typed
> >> files), and it still doesn't work - same error.
> > > 
> > You need to send the start of the array, not the array.
> > 
> > BlockRead(F,data[0],size,amt);
> > 
> 
> Excellent, that worked - thank you very much.
> 
> However, new problem...now I'm using a dynamic array, the contents don't 
> seem to be, how shall I put it...very stable.
> 
> Just to illustrate, I loaded a file into this, public, dynamic array (of 
> Byte), closed the file then, as a test, created a new one and saved it 
> back out. I then compared the results. The new file was full of random 
> data, and not what had been loaded in. I put this test in after noticing 
> that the data had become corrupted - I first I thought it may be due to 
> another unit accessing it, but this file saving out was straight after 
> it was loaded (so within the same procedure).
> 
> Cheers,
> 
> Gerald.

Please show the procedure(s) where you load original file, create a new one (file or array?) and
save it to disk.

-- 
Tom Brunberg
firstname.lastname@welho.com

Vote for best answer.
Score: 0  # Vote:  0
Date Posted: 19-Jan-2015, at 7:02 AM EST
From: Tom Brunberg
 
Re: Static vs Dynamic arrays  
News Group: embarcadero.public.delphi.language.delphi.general
Gerald Holdsworth wrote:

> On 18/01/2015 21:15, Jeff Overcash (TeamB) wrote:
> > Gerald Holdsworth wrote:
> >> On 18/01/2015 14:50, Jeff Overcash (TeamB) wrote:
> >>> Gerald Holdsworth wrote:
> 
> >> It is just declared untyped. If I declare it as Byte, I need to change
> >> the second line to Reset(F) (it won't take a second parameter for typed
> >> files), and it still doesn't work - same error.
> > > 
> > You need to send the start of the array, not the array.
> > 
> > BlockRead(F,data[0],size,amt);
> > 
> 
> Excellent, that worked - thank you very much.
> 
> However, new problem...now I'm using a dynamic array, the contents don't 
> seem to be, how shall I put it...very stable.
> 
> Just to illustrate, I loaded a file into this, public, dynamic array (of 
> Byte), closed the file then, as a test, created a new one and saved it 
> back out. I then compared the results. The new file was full of random 
> data, and not what had been loaded in. I put this test in after noticing 
> that the data had become corrupted - I first I thought it may be due to 
> another unit accessing it, but this file saving out was straight after 
> it was loaded (so within the same procedure).
> 
> Cheers,
> 
> Gerald.

Please show the procedure(s) where you load original file, create a new one (file or array?) and
save it to disk.

-- 
Tom Brunberg
firstname.lastname@welho.com

Vote for best answer.
Score: 0  # Vote:  0
Date Posted: 19-Jan-2015, at 7:01 AM EST
From: Tom Brunberg
 
Re: Static vs Dynamic arrays  
News Group: embarcadero.public.delphi.language.delphi.general
On 18/01/2015 21:15, Jeff Overcash (TeamB) wrote:
> Gerald Holdsworth wrote:
>> On 18/01/2015 14:50, Jeff Overcash (TeamB) wrote:
>>> Gerald Holdsworth wrote:

>> It is just declared untyped. If I declare it as Byte, I need to change
>> the second line to Reset(F) (it won't take a second parameter for typed
>> files), and it still doesn't work - same error.
>>
> You need to send the start of the array, not the array.
>
> BlockRead(F,data[0],size,amt);
>

Excellent, that worked - thank you very much.

However, new problem...now I'm using a dynamic array, the contents don't 
seem to be, how shall I put it...very stable.

Just to illustrate, I loaded a file into this, public, dynamic array (of 
Byte), closed the file then, as a test, created a new one and saved it 
back out. I then compared the results. The new file was full of random 
data, and not what had been loaded in. I put this test in after noticing 
that the data had become corrupted - I first I thought it may be due to 
another unit accessing it, but this file saving out was straight after 
it was loaded (so within the same procedure).

Cheers,

Gerald.

Vote for best answer.
Score: 0  # Vote:  0
Date Posted: 19-Jan-2015, at 3:11 AM EST
From: Gerald Holdsworth
 
Re: Static vs Dynamic arrays  
News Group: embarcadero.public.delphi.language.delphi.general
Gerald Holdsworth wrote:
> On 18/01/2015 14:50, Jeff Overcash (TeamB) wrote:
>> Gerald Holdsworth wrote:
>>> First of all, thank you for everyone's advice on this. I have now been
>>> playing and have found that:
>>>
>>>    AssignFile(F,filename);
>>>    Reset(F,1);
>>>    size:=FileSize(F);
>>>    SetLength(data,size);
>>>    BlockRead(F,data,size,amt);
>>>    CloseFile(F);
>>>
>> How are you declaring F?  If it is File of Char, you need to double the size to
>> get the number of bytes in the file since Char = 2 bytes.  If oyu declare it as
>> a file of byte does that change things?
>>
>>
> It is just declared untyped. If I declare it as Byte, I need to change 
> the second line to Reset(F) (it won't take a second parameter for typed 
> files), and it still doesn't work - same error.
> 
> Cheers,
> 
> Gerald.

You need to send the start of the array, not the array.

BlockRead(F,data[0],size,amt);

-- 
Jeff Overcash (TeamB)
       (Please do not email me directly unless  asked. Thank You)
And so I patrol in the valley of the shadow of the tricolor
I must fear evil. For I am but mortal and mortals can only die.
Asking questions, pleading answers from the nameless
faceless watchers that stalk the carpeted  corridors of Whitehall.
              (Fish)

Vote for best answer.
Score: 0  # Vote:  0
Date Posted: 18-Jan-2015, at 1:15 PM EST
From: Jeff Overcash (TeamB)
 
Re: Static vs Dynamic arrays  
News Group: embarcadero.public.delphi.language.delphi.general
On 18/01/2015 14:50, Jeff Overcash (TeamB) wrote:
> Gerald Holdsworth wrote:
>> First of all, thank you for everyone's advice on this. I have now been
>> playing and have found that:
>>
>>    AssignFile(F,filename);
>>    Reset(F,1);
>>    size:=FileSize(F);
>>    SetLength(data,size);
>>    BlockRead(F,data,size,amt);
>>    CloseFile(F);
>>
>
> How are you declaring F?  If it is File of Char, you need to double the size to
> get the number of bytes in the file since Char = 2 bytes.  If oyu declare it as
> a file of byte does that change things?
>
>
It is just declared untyped. If I declare it as Byte, I need to change 
the second line to Reset(F) (it won't take a second parameter for typed 
files), and it still doesn't work - same error.

Cheers,

Gerald.

Vote for best answer.
Score: 0  # Vote:  0
Date Posted: 18-Jan-2015, at 8:09 AM EST
From: Gerald Holdsworth
 
Re: Static vs Dynamic arrays  
News Group: embarcadero.public.delphi.language.delphi.general
Gerald Holdsworth wrote:
> First of all, thank you for everyone's advice on this. I have now been 
> playing and have found that:
> 
>   AssignFile(F,filename);
>   Reset(F,1);
>   size:=FileSize(F);
>   SetLength(data,size);
>   BlockRead(F,data,size,amt);
>   CloseFile(F);
> 

How are you declaring F?  If it is File of Char, you need to double the size to 
get the number of bytes in the file since Char = 2 bytes.  If oyu declare it as 
a file of byte does that change things?


-- 
Jeff Overcash (TeamB)
       (Please do not email me directly unless  asked. Thank You)
And so I patrol in the valley of the shadow of the tricolor
I must fear evil. For I am but mortal and mortals can only die.
Asking questions, pleading answers from the nameless
faceless watchers that stalk the carpeted  corridors of Whitehall.
              (Fish)

Vote for best answer.
Score: 0  # Vote:  0
Date Posted: 18-Jan-2015, at 6:50 AM EST
From: Jeff Overcash (TeamB)
 
Re: Static vs Dynamic arrays  
News Group: embarcadero.public.delphi.language.delphi.general
First of all, thank you for everyone's advice on this. I have now been 
playing and have found that:

  AssignFile(F,filename);
  Reset(F,1);
  size:=FileSize(F);
  SetLength(data,size);
  BlockRead(F,data,size,amt);
  CloseFile(F);

Does not work - it reports an error "IO Error 998". And no amount of 
fiddling will get it working (aside from making 'data' a static array 
again). However, I have found:

  AssignFile(F,filename);
  Reset(F,1);
  size:=FileSize(F);
  file_size:=size;
  SetLength(data,size);
  ptr:=0;
  while file_size>0 do
  begin
   BlockRead(F,file_buffer,Length(file_buffer),amt);
   for l:=0 to amt-1 do
   begin
    data[l+ptr]:=file_buffer[l];
   end;
   file_size:=file_size-amt;
   ptr:=ptr+amt;
  end;
  CloseFile(F);

works better. This leads onto another issue...

This unit (which is 'MainUnit', the primary unit) loads this file, 
analyses it, and then decides if it is data that it can open. If it is, 
an MDI Child form/unit is then created which deals with the data.

The array 'data' in 'MainUnit' is a global one, so it can then be 
accessed by the child form by 'MainUnit.data'. Historically, the child 
form used to load the file in (and, hence, was not dealt with by 
'MainForm'). However, things change and evolve!

Now, the child form has it's own 'data' variable (this one is local to a 
procedure in the child unit). Now I'm using a dynamic array for 
'MainForm.data', the following does not work in the child form:

    for x:=0 to MainForm.size-1 do
     data[x]:=MainForm.data[x];

A large chunk of at the beginning of the array is missing. However, if I 
change 'MainForm.data' back to a static, it works (child unit 'data' is 
currently dynamic, but makes no difference if I make it static).

I therefore have three options, I think:

1 - change the child form 'data' for 'MainForm.data'...this is a large 
number of occurrences; or

2 - make child form 'data' a pointer to 'MainForm.data'...problem here 
is that I don't know if I can do this, as I don't know much about 
pointers; or

3 - make 'MainForm.data' a static array again.

(Incidentally, both 'data's are of the type Byte).

Any help and advice will be greatly appreciated.

Cheers,

Gerald.

Vote for best answer.
Score: 0  # Vote:  0
Date Posted: 18-Jan-2015, at 6:13 AM EST
From: Gerald Holdsworth
 
Re: Static vs Dynamic arrays  
News Group: embarcadero.public.delphi.language.delphi.general
Gerald Holdsworth wrote:

> That explains an error I got when I increased the size of a static
> local array to 800K.

Also if you declare very big array as global var (i.e. not on stack)
linker can reject this. AFAIR I seen this in CBuilder which uses the
same linker as Delphi.

--
Alex

Vote for best answer.
Score: 0  # Vote:  0
Date Posted: 15-Jan-2015, at 9:00 PM EST
From: Alex Belo
 
Re: Static vs Dynamic arrays  
News Group: embarcadero.public.delphi.language.delphi.general
On 14/01/2015 18:28, Remy Lebeau (TeamB) wrote:
> Gerald wrote:

>> Obviously, I should be able to change the 4th line to be
>> SetLength(data,size)
>
> That is OK for small files, but I would not recommend itfor large files.
>   Large files should be processed using smaller fixed chunks or a memory mapping.

Hmmm...food for thought.

Cheers,

Gerald.

Vote for best answer.
Score: 0  # Vote:  0
Date Posted: 15-Jan-2015, at 10:18 AM EST
From: Gerald Holdsworth