### Bookstore

 Computer Math and Games in Pascal (preview) Lazarus Handbook

### Author Topic: [RESOLVED] Add integers to an array  (Read 2047 times)

#### Pixy

• New Member
• Posts: 49
##### [RESOLVED] Add integers to an array
« on: May 20, 2019, 04:58:39 am »
I am using this:
Code: Pascal  [Select][+][-]
1. var
2.     IntAr: array of integer;
3.
4. begin
5.     IntAr[0] := 115;
6.     IntAr[1] := 160;
7.    ...
8. end.
9.
but I get an access violation error. Any idea how I can add individual integers to the array?
« Last Edit: May 20, 2019, 06:42:06 am by Pixy »

#### RAW

• Hero Member
• Posts: 867
##### Re: Add integers to an array
« Reply #1 on: May 20, 2019, 05:25:06 am »
SetLength
Windows 7 Pro (x64 Sp1) And Windows XP Pro (x86 Sp3) - LAZARUS 2.0.8 FPC 3.0.4 - TRUNK 2.1.0 FPC 3.3.1
// This is polarity (hell) and hell is reigned by lies, nothing but lies. Be careful what you believe ...
// There are 10 types of people in this world, those who understand mind control and ...

#### 440bx

• Hero Member
• Posts: 2090
##### Re: Add integers to an array
« Reply #2 on: May 20, 2019, 05:30:09 am »
RAW above is right, a little succinct but definitely right.   Here are some more details.

I am using this:
Code: Pascal  [Select][+][-]
1. var
2.     IntAr: array of integer;
3.
4. begin
5.     IntAr[0] := 115;
6.     IntAr[1] := 160;
7.    ...
8. end.
9.
but I get an access violation error. Any idea how I can add individual integers to the array?
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.)

if you do something along the lines of:
Code: Pascal  [Select][+][-]
1. SetLength(IntAr, 10);
that will give you an array of 10 elements where the first element has index 0 and the last element has index 9.  To iterate through the array you can use
Code: Pascal  [Select][+][-]
1. for I := low(IntAr) to high(IntAr) do ....
that way you don't have to hard code or remember the lower and upper bounds of the array (which will always be zero to count - 1.)

HTH.
FPC v3.0.4 and Lazarus 1.8.2 on Windows 7 64bit.

#### valdir.marcos

• Hero Member
• Posts: 1018
##### Re: Add integers to an array
« Reply #3 on: May 20, 2019, 05:39:49 am »
I am using this:
Code: Pascal  [Select][+][-]
1. var
2.     IntAr: array of integer;
3. begin
4.     IntAr[0] := 115;
5.     IntAr[1] := 160;
6.    ...
7. end.
but I get an access violation error. Any idea how I can add individual integers to the array?
http://wiki.freepascal.org/Array
http://wiki.freepascal.org/Dynamic_array
Code: Pascal  [Select][+][-]
1. procedure TForm1.Button1Click(Sender: TObject);
2. var
3.   ArraySize: Integer;
4.   IntAr: array of Integer;
5. begin
6.   ArraySize := 0;
7.
8.   ArraySize := ArraySize + 1;
9.   setLength(IntAr, ArraySize);
10.   IntAr[0] := 115;
11.
12.   ArraySize := ArraySize + 1;
13.   setLength(IntAr, ArraySize);
14.   IntAr[1] := 160;
15.
16.   ShowMessage('IntAr[0]: ' + IntToStr(IntAr[0]) + LineEnding +
17.               'IntAr[1]: ' + IntToStr(IntAr[1]));
18. end;

#### Pixy

• New Member
• Posts: 49
##### Re: Add integers to an array
« Reply #4 on: May 20, 2019, 05:55:10 am »
Thanks for the information guys RAW, 440bx, valdir.marcos.

I am using this:
Code: Pascal  [Select][+][-]
1. var
2.     IntAr: array of integer;
3. begin
4.     IntAr[0] := 115;
5.     IntAr[1] := 160;
6.    ...
7. end.
but I get an access violation error. Any idea how I can add individual integers to the array?
http://wiki.freepascal.org/Array
http://wiki.freepascal.org/Dynamic_array
Code: Pascal  [Select][+][-]
1. procedure TForm1.Button1Click(Sender: TObject);
2. var
3.   ArraySize: Integer;
4.   IntAr: array of Integer;
5. begin
6.   ArraySize := 0;
7.
8.   ArraySize := ArraySize + 1;
9.   setLength(IntAr, ArraySize);
10.   IntAr[0] := 115;
11.
12.   ArraySize := ArraySize + 1;
13.   setLength(IntAr, ArraySize);
14.   IntAr[1] := 160;
15.
16.   ShowMessage('IntAr[0]: ' + IntToStr(IntAr[0]) + LineEnding +
17.               'IntAr[1]: ' + IntToStr(IntAr[1]));
18. end;

I think that this is going to work. I'll try that.
« Last Edit: May 20, 2019, 06:18:20 am by Pixy »

#### lucamar

• Hero Member
• Posts: 3428
##### Re: Add integers to an array
« Reply #5 on: May 20, 2019, 06:11:26 am »
Thanks for the information guys RAW, 440bx, valdir.marcos.

Code: Pascal  [Select][+][-]
1. procedure TForm1.Button1Click(Sender: TObject);
2. var
3.   ArraySize: Integer;
4.   IntAr: array of Integer;
5. begin
6.   ArraySize := 0;
7.
8.   ArraySize := ArraySize + 1;
9.   setLength(IntAr, ArraySize);
10.   IntAr[0] := 115;
11.
12.   ArraySize := ArraySize + 1;
13.   setLength(IntAr, ArraySize);
14.   IntAr[1] := 160;
15.
16.   ShowMessage('IntAr[0]: ' + IntToStr(IntAr[0]) + LineEnding +
17.               'IntAr[1]: ' + IntToStr(IntAr[1]));
18. 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:

Code: Pascal  [Select][+][-]
1. procedure TForm1.Button1Click(Sender: TObject);
2. const
3.   ArraySize = 2;
4. var
5.   IntAr: array of Integer;
6. begin
7.   setLength(IntAr, ArraySize);
8.   IntAr[0] := 115;
9.   IntAr[1] := 160;
10.   ShowMessageFmt('IntAr[0]: %d' + LineEnding + 'IntAr[1]: %d', ,[IntAr[0], IntAr[1]);
11. 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.
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!)
Lazarus/FPC 2.0.8/3.0.4 & 2.0.10/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

#### Pixy

• New Member
• Posts: 49
##### Re: Add integers to an array
« Reply #6 on: May 20, 2019, 06:19:09 am »
Thanks for the information guys RAW, 440bx, valdir.marcos.

Code: Pascal  [Select][+][-]
1. procedure TForm1.Button1Click(Sender: TObject);
2. var
3.   ArraySize: Integer;
4.   IntAr: array of Integer;
5. begin
6.   ArraySize := 0;
7.
8.   ArraySize := ArraySize + 1;
9.   setLength(IntAr, ArraySize);
10.   IntAr[0] := 115;
11.
12.   ArraySize := ArraySize + 1;
13.   setLength(IntAr, ArraySize);
14.   IntAr[1] := 160;
15.
16.   ShowMessage('IntAr[0]: ' + IntToStr(IntAr[0]) + LineEnding +
17.               'IntAr[1]: ' + IntToStr(IntAr[1]));
18. 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:

Code: Pascal  [Select][+][-]
1. procedure TForm1.Button1Click(Sender: TObject);
2. const
3.   ArraySize = 2;
4. var
5.   IntAr: array of Integer;
6. begin
7.   setLength(IntAr, ArraySize);
8.   IntAr[0] := 115;
9.   IntAr[1] := 160;
10.   ShowMessageFmt('IntAr[0]: %d' + LineEnding + 'IntAr[1]: %d', ,[IntAr[0], IntAr[1]);
11. 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.

The array size depends on how many times you use it. Now I get another violation error. I am using:
Code: Pascal  [Select][+][-]
1. var
2.     ars: integer;
3.     IntAr: array of integer;
4.
5. begin
6.     for ars := 0 to 2 do
7.     begin
8.         SetLength(IntAr, ars);
9.         IntAr[ars] := 115;
10.     end;
11. end;
12.

If I use SetLength(IntAr, 3); it works but if I use ars instead of 3 I get the violation error. How can I get around that?

EDIT: Nevermind, I used another variable and it worked.
« Last Edit: May 20, 2019, 06:40:41 am by Pixy »

#### valdir.marcos

• Hero Member
• Posts: 1018
##### Re: Add integers to an array
« Reply #7 on: May 20, 2019, 08:34:23 am »
Code: Pascal  [Select][+][-]
1. procedure TForm1.Button1Click(Sender: TObject);
2. var
3.   ArraySize: Integer;
4.   IntAr: array of Integer;
5. begin
6.   ArraySize := 0;
7.
8.   ArraySize := ArraySize + 1;
9.   setLength(IntAr, ArraySize);
10.   IntAr[0] := 115;
11.
12.   ArraySize := ArraySize + 1;
13.   setLength(IntAr, ArraySize);
14.   IntAr[1] := 160;
15.
16.   ShowMessage('IntAr[0]: ' + IntToStr(IntAr[0]) + LineEnding +
17.               'IntAr[1]: ' + IntToStr(IntAr[1]));
18. end;
That's ... wasteful, to be mild. If you know the exact number of items in the array do it like this:
You could be more polite and less arrogant.
How could you know if Pixy needs a limited or dynamic quantity of elements by the array on his question?
I am using this:
Code: Pascal  [Select][+][-]
1. var
2.     IntAr: array of integer;
3.
4. begin
5.     IntAr[0] := 115;
6.     IntAr[1] := 160;
7.    ...
8. end.
but I get an access violation error. Any idea how I can add individual integers to the array?
I had provided some wiki information about limited and dynamic arrays and then a very simple solution that could address both cases.
I was not trying to be comprehensive, but simply educative. I still don't know how much Pixy knows about Pascal or Arrays.

Quote
Code: Pascal  [Select][+][-]
1. procedure TForm1.Button1Click(Sender: TObject);
2. const
3.   ArraySize = 2;
4. var
5.   IntAr: array of Integer;
6. begin
7.   setLength(IntAr, ArraySize);
8.   IntAr[0] := 115;
9.   IntAr[1] := 160;
10.   ShowMessageFmt('IntAr[0]: %d' + LineEnding + 'IntAr[1]: %d', ,[IntAr[0], IntAr[1]);
11. 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 are correct, but again I believe Pixy is searching for a basic solution, so far. As times goes by, he will learn better solutions.

#### mangakissa

• Hero Member
• Posts: 1105
##### Re: [RESOLVED] Add integers to an array
« Reply #8 on: May 20, 2019, 08:37:00 am »
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;`
Lazarus 2.06 (64b) / FPC 3.0.4 / Windows 10
stucked on Delphi 10.3.1

#### lucamar

• Hero Member
• Posts: 3428
##### Re: Add integers to an array
« Reply #9 on: May 20, 2019, 08:57:24 am »
You could be more polite and less arrogant.

Youre right. It wasn't my intention to sound arrogant but my phrasing was unfortunate. Sorry.
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!)
Lazarus/FPC 2.0.8/3.0.4 & 2.0.10/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

#### PascalDragon

• Hero Member
• Posts: 2586
• Compiler Developer
##### Re: [RESOLVED] Add integers to an array
« Reply #10 on: May 20, 2019, 04:08:01 pm »
The upcoming FPC 3.2.0 also allows this
Code: Pascal  [Select][+][-]
1. IntAr := [115, 160];

#### valdir.marcos

• Hero Member
• Posts: 1018
##### Re: Add integers to an array
« Reply #11 on: May 20, 2019, 09:09:53 pm »
You could be more polite and less arrogant.
Youre right. It wasn't my intention to sound arrogant but my phrasing was unfortunate. Sorry.
You're welcome.

#### Pixy

• New Member
• Posts: 49
##### Re: [RESOLVED] Add integers to an array
« Reply #12 on: May 21, 2019, 01:40:28 am »
@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.

The upcoming FPC 3.2.0 also allows this
Code: Pascal  [Select][+][-]
1. IntAr := [115, 160];

Interesting.

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;`

That makes sense. I will try that, very useful.

#### valdir.marcos

• Hero Member
• Posts: 1018
##### Re: [RESOLVED] Add integers to an array
« Reply #13 on: May 21, 2019, 01:58:15 am »
@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.
Just curious.
Coming from web programming languages and Microsoft .Net, why are you interested in Free Pascal and Lazarus?

#### lucamar

• Hero Member
• Posts: 3428
##### Re: [RESOLVED] Add integers to an array
« Reply #14 on: May 21, 2019, 02:02:35 am »
@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.
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!)
Lazarus/FPC 2.0.8/3.0.4 & 2.0.10/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.