I am using this:An "array of integer" is just a pointer to an array. The array itself doesn't exist until you use the function "SetLength" to tell the compiler how many elements you want in the array. (until you use SetLength there is no memory allocated for the array.)but I get an access violation error. Any idea how I can add individual integers to the array?
var IntAr: array of integer; begin IntAr[0] := 115; IntAr[1] := 160; ... end.
I am using this:http://wiki.freepascal.org/Arraybut I get an access violation error. Any idea how I can add individual integers to the array?
var IntAr: array of integer; begin IntAr[0] := 115; IntAr[1] := 160; ... end.
I am using this:http://wiki.freepascal.org/Arraybut I get an access violation error. Any idea how I can add individual integers to the array?
var IntAr: array of integer; begin IntAr[0] := 115; IntAr[1] := 160; ... end.
http://wiki.freepascal.org/Dynamic_array
procedure TForm1.Button1Click(Sender: TObject); var ArraySize: Integer; IntAr: array of Integer; begin ArraySize := 0; ArraySize := ArraySize + 1; setLength(IntAr, ArraySize); IntAr[0] := 115; ArraySize := ArraySize + 1; setLength(IntAr, ArraySize); IntAr[1] := 160; ShowMessage('IntAr[0]: ' + IntToStr(IntAr[0]) + LineEnding + 'IntAr[1]: ' + IntToStr(IntAr[1])); end;
Thanks for the information guys RAW, 440bx, valdir.marcos.
procedure TForm1.Button1Click(Sender: TObject); var ArraySize: Integer; IntAr: array of Integer; begin ArraySize := 0; ArraySize := ArraySize + 1; setLength(IntAr, ArraySize); IntAr[0] := 115; ArraySize := ArraySize + 1; setLength(IntAr, ArraySize); IntAr[1] := 160; ShowMessage('IntAr[0]: ' + IntToStr(IntAr[0]) + LineEnding + 'IntAr[1]: ' + IntToStr(IntAr[1])); end;
I think that this is going to work. I'll try that.
Thanks for the information guys RAW, 440bx, valdir.marcos.
procedure TForm1.Button1Click(Sender: TObject); var ArraySize: Integer; IntAr: array of Integer; begin ArraySize := 0; ArraySize := ArraySize + 1; setLength(IntAr, ArraySize); IntAr[0] := 115; ArraySize := ArraySize + 1; setLength(IntAr, ArraySize); IntAr[1] := 160; ShowMessage('IntAr[0]: ' + IntToStr(IntAr[0]) + LineEnding + 'IntAr[1]: ' + IntToStr(IntAr[1])); end;
I think that this is going to work. I'll try that.
That's ... wasteful, to be mild. If you know the exact number of items in the array do it like this:
procedure TForm1.Button1Click(Sender: TObject); const ArraySize = 2; var IntAr: array of Integer; begin setLength(IntAr, ArraySize); IntAr[0] := 115; IntAr[1] := 160; ShowMessageFmt('IntAr[0]: %d' + LineEnding + 'IntAr[1]: %d', ,[IntAr[0], IntAr[1]); end;
Each time you use SetLength() a new block of memory has to be allocated, the existing array copied over and the old memory block disposed. Call SetLength() as few times as you possible can.
You could be more polite and less arrogant.That's ... wasteful, to be mild. If you know the exact number of items in the array do it like this:
procedure TForm1.Button1Click(Sender: TObject); var ArraySize: Integer; IntAr: array of Integer; begin ArraySize := 0; ArraySize := ArraySize + 1; setLength(IntAr, ArraySize); IntAr[0] := 115; ArraySize := ArraySize + 1; setLength(IntAr, ArraySize); IntAr[1] := 160; ShowMessage('IntAr[0]: ' + IntToStr(IntAr[0]) + LineEnding + 'IntAr[1]: ' + IntToStr(IntAr[1])); end;
I am using this:I had provided some wiki information about limited and dynamic arrays and then a very simple solution that could address both cases.but I get an access violation error. Any idea how I can add individual integers to the array?
var IntAr: array of integer; begin IntAr[0] := 115; IntAr[1] := 160; ... end.
You are correct, but again I believe Pixy is searching for a basic solution, so far. As times goes by, he will learn better solutions.Each time you use SetLength() a new block of memory has to be allocated, the existing array copied over and the old memory block disposed. Call SetLength() as few times as you possible can.
procedure TForm1.Button1Click(Sender: TObject); const ArraySize = 2; var IntAr: array of Integer; begin setLength(IntAr, ArraySize); IntAr[0] := 115; IntAr[1] := 160; ShowMessageFmt('IntAr[0]: %d' + LineEnding + 'IntAr[1]: %d', ,[IntAr[0], IntAr[1]); end;
var
ars: integer;
IntAr: array of integer;
begin
for ars := 1 to 3 do
begin
SetLength(IntAr, ars);
IntAr[ars - 1] := 115;
end;
end;
You could be more polite and less arrogant.
You're welcome.You could be more polite and less arrogant.Youre right. It wasn't my intention to sound arrogant but my phrasing was unfortunate. Sorry.
The upcoming FPC 3.2.0 also allows this :-[
IntAr := [115, 160];
You can't create a setlength of 0. That's freeing the array in the memory.Code: [Select]var
ars: integer;
IntAr: array of integer;
begin
for ars := 1 to 3 do
begin
SetLength(IntAr, ars);
IntAr[ars - 1] := 115;
end;
end;
@valdir.marcos, @lucamar, Ideally I would like to use a dynamic array that adds integers, however, I will have to test whether using setlength multiple times within a period of milliseconds works without trouble.Just curious.
I started Pascal/Delphi 1 month ago and I think that I've learnt all the basics. Mostly, my areas are web dev languages and .net.
@valdir.marcos, @lucamar, Ideally I would like to use a dynamic array that adds integers, however, I will have to test whether using setlength multiple times within a period of milliseconds works without trouble. I started Pascal/Delphi 1 month ago and I think that I've learnt all the basics. Mostly, my areas are web dev languages and .net.
There shouldn't be (much) trouble if the arrays are reasonably short, say up to about a hundred integers. It depends also on how many times is "multiple times".
There are solutions even for high update rates and huge arrays but without knowing more of what you are doing they may be a little overkill for you.
Less than 1 second here for 300,000 ten-digit elements.There shouldn't be (much) trouble if the arrays are reasonably short, say up to about a hundred integers. It depends also on how many times is "multiple times".I think that more than 210 indexes wont be needed. So, anywhere between 3 to 210 (each index with integer of up to 6-7 digits) within let's say a second or as fast as it can be done.
There are solutions even for high update rates and huge arrays but without knowing more of what you are doing they may be a little overkill for you.
I think that more than 210 indexes wont be needed. So, anywhere between 3 to 210 (each index with integer of up to 6-7 digits) within let's say a second or as fast as it can be done.
I think that more than 210 indexes wont be needed. So, anywhere between 3 to 210 (each index with integer of up to 6-7 digits) within let's say a second or as fast as it can be done.
Test it and see how much time it takes using SetLength(Length+1) each time you want to add one item in your target platform. A maximum of 210 item/second doesn't sound as much for current OS/hardware.
Of course, depends on what else the application must do :)
Less than 1 second here for 300,000 ten-digit elements.There shouldn't be (much) trouble if the arrays are reasonably short, say up to about a hundred integers. It depends also on how many times is "multiple times".I think that more than 210 indexes wont be needed. So, anywhere between 3 to 210 (each index with integer of up to 6-7 digits) within let's say a second or as fast as it can be done.
There are solutions even for high update rates and huge arrays but without knowing more of what you are doing they may be a little overkill for you.
procedure TForm1.Button1Click(Sender: TObject); var TimeLapsed: TTime; Number: Integer; i, Elements, ArraySize: Integer; IntAr: array of Integer; begin TimeLapsed := Time; Elements := 300000; Number := 1234567890; for i := 1 to Elements do begin ArraySize := i; setLength(IntAr, ArraySize); IntAr[ArraySize - 1] := Number + i; end; TimeLapsed := Time - TimeLapsed; ShowMessage('Time-lapsed: ' + TimeToStr(TimeLapsed) + LineEnding + 'IntAr[' + IntToStr(Elements - 1) + '] = ' + IntToStr(IntAr[Elements - 1]) + LineEnding + 'Array Size: ' + IntToStr(Length(IntAr))); end;[code]
If an upper limit is known in advance, simply SetLength(arry, 210); at the outset.
Keep a count of the number of indexes allocated, and when all indexing is complete, call SetLength(arry, count).
Of course, the difference in speed compared to multiple calls to SetLength() may be negligible. Timing the two alternative implementations is the only way to know if there is any significant speed gain. It is surprising how often optimisations which in theory would be expected to make a difference in practice make little difference, or even have a negative effect with certain data. Compilers are getting ever more sophisticated, and low level caching/pipelining etc. over which the high level programmer has little or no control may ultimately prove to be the determinative factor.
[…] but I get an access violation error. […]
[…] An "array of integer" is just a pointer to an array. […]Just to explain the error message: Your IntAr (as a “managed” type (https://www.freepascal.org/docs-html/ref/refse20.html)) is initialized as a nil pointer, i. e. it points to the address 0. The “access violation” occurs, because your program accesses a block that isn’t in its purview. If you coincidentally happen to access IntAr[1234567] and that block belongs to your program, you could alter its content without an error.