ReDim : Array not initialized

Just starting out? Need help? Post your questions and find answers here.
User avatar
Otrebor
Enthusiast
Enthusiast
Posts: 211
Joined: Mon Mar 17, 2014 1:42 pm
Location: São Paulo, Brasil
Contact:

ReDim : Array not initialized

Post by Otrebor »

Hi
I do not know if this is related with my PC or...

Code: Select all

Dim frameInValues.u (250000,0)
ReDim frameInValues.u (250000,1949)
ReDim frameInValues.u (250000,1950);----> Variable Viewer: The Array is not initialized

Code: Select all

Dim frameInValues.u (250000,0)
ReDim frameInValues.u (250000,10)
ReDim frameInValues.u (250000,1950);------>Variable Viewer: This is OK

Code: Select all

Dim frameInValues.u (250000,3000); ------>Variable Viewer: This is OK 
OS: WIN 10
PB 6.01 LTS and PB 6.1 Beta 4 (both x86) asm or c - not tested with last beta
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4991
Joined: Sun Apr 12, 2009 6:27 am

Re: ReDim : Array not initialized

Post by RASHAD »

PB 6.04 x86 or x64 Windows 11 x64
No problem
Egypt my love
User avatar
Otrebor
Enthusiast
Enthusiast
Posts: 211
Joined: Mon Mar 17, 2014 1:42 pm
Location: São Paulo, Brasil
Contact:

Re: ReDim : Array not initialized

Post by Otrebor »

Thanks, RASHAD!
User avatar
NicTheQuick
Addict
Addict
Posts: 1527
Joined: Sun Jun 22, 2003 7:43 pm
Location: Germany, Saarbrücken
Contact:

Re: ReDim : Array not initialized

Post by NicTheQuick »

An Unicode array of the dimensions (250000, 1949) has 487,501,950 entries and allocates 975,003,900 bytes (~930 MiB) of memory.
If you make it bigger your operating system may has trouble to simply enlarge the memory when it can not find enough free memory directly behind the currently allocated space in memory.
If this is the case it has to allocate a completely new memory block of the target size and copying over the data from the old to the new location in memory. During this process your program consumes more than twice the amount of memory. And if there is not enough memory left the ReDim operation simply fails.
That might be the reason why your first example does not work but the other ones do.

Here's my calculation of the worst case memory consumption of your examples:
Example1: ((250000 + 1) · (1949 + 1) + (250000 + 1) · (1950 + 1)) · 2 bytes = 1,950,507,802 bytes = 1,860.15 MiB
Example2: ((250000 + 1) · (10 + 1) + (250000 + 1) · (1950 + 1)) · 2 bytes = 981,003,924 bytes = 935.56 MiB
Example3: (250000 + 1) · (3000 + 1) · 2 bytes = 1,500,506,002 bytes = 1,430.99 MiB
The english grammar is freeware, you can use it freely - But it's not Open Source, i.e. you can not change it or publish it in altered way.
User avatar
STARGÅTE
Addict
Addict
Posts: 2260
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: ReDim : Array not initialized

Post by STARGÅTE »

No problem here.

The issue could be related to your system.
In the first case ReDim frameInValues.u (250000,1949) allocates around 1 GB.
If you then resize the array and the memory do not fit at the current position, PureBacis allocates another 1 GB for the new array and copy all data. You need 2 GB space.
In the second case, the initial array is very small, and only 1 GB is needed.
In the third case, you also need only 1.5 GB.

Edit: Nic was faster ^^
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
Otrebor
Enthusiast
Enthusiast
Posts: 211
Joined: Mon Mar 17, 2014 1:42 pm
Location: São Paulo, Brasil
Contact:

Re: ReDim : Array not initialized

Post by Otrebor »

Thanks, NicTheQuick and STARGÅTE for the explanation!
Post Reply