Array limits

Just starting out? Need help? Post your questions and find answers here.
User avatar
Lord
Addict
Addict
Posts: 847
Joined: Tue May 26, 2009 2:11 pm

Array limits

Post by Lord »

Hi @all!

What are the maximum numbers for a three dimensional array?
Are there any? Or are the limits set by available memory?

The reason for my question:

Code: Select all

a1.i=999
a2.i=999
a3.i=268
q.i=8

Debug (a1+1)*(a2+1)*(a3+1)*q

Dim A.q(a1, a2, a3)

MessageRequester("", Str((a1+1)*(a2+1)*(a3+1)*q))
If I run this little piece of code I get this error with debugger on:
Debugger wrote: [15:26:34] Warte auf den Start des Executable...
[15:26:34] Executable-Typ: Windows - x64 (64bit)
[15:26:34] Executable gestartet.
[15:26:34] [ERROR] Zeile: 8
[15:26:34] [ERROR] Ungültiger Speicherzugriff. (Schreibfehler an der Adresse 18446744073709551568)
[15:26:37] Das Programm wurde beendet.
The snippet crashes with debugger of.

I have 16 GB of memory installed. There are approx. 14GB free, if I run
the code above with "a3.i=267".

So what is the reason for the crash?
Image
User avatar
STARGÅTE
Addict
Addict
Posts: 2067
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: Array limits

Post by STARGÅTE »

a windows process only can allocate ~2GB

use ArraySize() to check the correct dim of the array
Last edited by STARGÅTE on Wed Mar 07, 2012 3:55 pm, edited 1 time in total.
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
User avatar
Lord
Addict
Addict
Posts: 847
Joined: Tue May 26, 2009 2:11 pm

Re: Array limits

Post by Lord »

Thanks for your fast response!

Or

Danke für deine schnelle Antwort!
Image
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Re: Array limits

Post by ts-soft »

I think, the limit of elements in a array is the highest value of a sign long.
If you remove one element, it will work on x64 (but not on x32 about the
limitation of 2 GB Memory Allocation)
PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
Image
User avatar
luis
Addict
Addict
Posts: 3876
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

Re: Array limits

Post by luis »

Uhm... not so fast !

As ts-soft said there must be some limit imposed by PB, I don't know exactly what it is.

Under 32 bit the address space for a process is normally 2GB (it can be extended but let's forget about it), and this include the code data of the program and its static data (global vars and such) PLUS the stack space and the dynamic data (malloc and such). So space for allocated data is less than 2GB anyway.

On 64 bit on the other hand it should be 8 TB. So under 64 bit there must be something due to the way PB arrays are implemented that limit the space you can allocate through the arrays (at least with multiple dimensions).

Fred or Freak should tell.
"Have you tried turning it off and on again ?"
A little PureBasic review
buddymatkona
Enthusiast
Enthusiast
Posts: 252
Joined: Mon Aug 16, 2010 4:29 am

Re: Array limits

Post by buddymatkona »

I do not know if PB imposes a limit but in this case, you are short of physical memory.
Change

Code: Select all

Dim A.q(a1, a2, a3)
to

Code: Select all

Dim A.b(a1, a2, a3)
and it should work. The quad type would require over 17 GB. Good luck. :)
User avatar
luis
Addict
Addict
Posts: 3876
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

Re: Array limits

Post by luis »

buddymatkona wrote: The quad type would require over 17 GB. Good luck. :)
:?: A little over 2GB if I'm not mistaken. Why 17 ?


Anyway, tried under 64 bit:

A multidimensional array (a,b,c) over 2GB gives the IMA reported in the first post (it shouldn't crash).

The equivalent array, reduced to a single dimension (a*b*c) , does not crash, it's not dimensioned and you can test it has failed using ArraySize() which returns -1.

In both cases, even under 64 bit, it seem you cannot break the 2GB barrier (why, I don't know).
"Have you tried turning it off and on again ?"
A little PureBasic review
User avatar
Danilo
Addict
Addict
Posts: 3037
Joined: Sat Apr 26, 2003 8:26 am
Location: Planet Earth

Re: Array limits

Post by Danilo »

luis wrote:Anyway, tried under 64 bit:

A multidimensional array (a,b,c) over 2GB gives the IMA reported in the first post (it shouldn't crash).

The equivalent array, reduced to a single dimension (a*b*c) , does not crash, it's not dimensioned and you can test it has failed using ArraySize() which returns -1.

In both cases, even under 64 bit, it seem you cannot break the 2GB barrier (why, I don't know).
Bug report: Windows PB 4.60 x64 - Dim strangeness
Danilo wrote:1GB, 4GB, 8GB = OK. 2GB and 16GB = -1
There seems to be a problem around 2GB, whereas 4GB and 8GB are OK.

The IMA at address 18446744073709551568 is at -48:

Code: Select all

Debug 18446744073709551568
User avatar
luis
Addict
Addict
Posts: 3876
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

Re: Array limits

Post by luis »

@Danilo, thanks I missed that :)
"Have you tried turning it off and on again ?"
A little PureBasic review
User avatar
Lord
Addict
Addict
Posts: 847
Joined: Tue May 26, 2009 2:11 pm

Re: Array limits

Post by Lord »

Thanks @all for your response.

So it seems to be a case for a bug fix.
Image
buddymatkona
Enthusiast
Enthusiast
Posts: 252
Joined: Mon Aug 16, 2010 4:29 am

Re: Array limits

Post by buddymatkona »

Whoops! My 17 GB should have been 17 Gb (bits). :oops: I agree the original code should work.
User avatar
bbanelli
Enthusiast
Enthusiast
Posts: 543
Joined: Tue May 28, 2013 10:51 pm
Location: Europe
Contact:

Re: Array limits

Post by bbanelli »

Sorry to bump old thread, but am I missing something? x64 W7, PB 5.6 x64, 32GB RAM + 32GB swap; here's the deal:

Code: Select all

EnableExplicit

Define Dim a.i(50000*50000)

CallDebugger
[00:27:53] [ERROR] Line: 3
[00:27:53] [ERROR] Array size can't be negative.

Code: Select all

EnableExplicit

Define Dim a.i(100000*100000)

Debug ArraySize(a())

CallDebugger
1410065408

Code: Select all

EnableExplicit

Define Dim MultiArray(50000,50000)

CallDebugger
Works fine.

Code: Select all

EnableExplicit

Define Dim MultiArray(100000,100000)

CallDebugger
[00:31:47] [ERROR] Line: 3
[00:31:47] [ERROR] Invalid memory access. (write error at address 18446744073709551568)
"If you lie to the compiler, it will get its revenge."
Henry Spencer
https://www.pci-z.com/
normeus
Enthusiast
Enthusiast
Posts: 414
Joined: Fri Apr 20, 2012 8:09 pm
Contact:

Re: Array limits

Post by normeus »

It is all on the number you are using to define the array. PB forces it to be an integer:

Code: Select all

EnableExplicit
Define x.l=50000*50000
;Define x =50000*50000
Debug x
Define Dim a.i(x)

Debug ArraySize(a())

CallDebugger

comment out define x.l and uncomment define x

Norm.
google Translate;Makes my jokes fall flat- Fait mes blagues tombent à plat- Machte meine Witze verpuffen- Eh cumpari ci vo sunari
Post Reply