(Re)Dim with a negative size just works. Bug or Feature?
Posted: Fri Apr 25, 2025 11:14 am
Hi guys.
In this post I found out that it seems to be no problem using a size of -1 for Dim or ReDim.
Example with Dim:
Example with ReDim:
An issue occurs if you try to increase the size again afterwards.
In the original code setting the array size to -1 is very helpful. Because only with an array of size -1 you can create an empty JSON array.
So I think at first look it is a bug that you can set the size to -1 and even ArraySize() reports the size as -1. And of course it is a bug that increasing the size results in an invalid memory access. But on the other hand it is very helpful when working with JSON and you don't want to program in special cases for empty arrays.
Let's discuss together if this should be moved to the Bug forum.
I tested it with PureBasic 6.20 (x64) on Ubuntu 24.04.
In this post I found out that it seems to be no problem using a size of -1 for Dim or ReDim.
Example with Dim:
Code: Select all
Define size.i = -1
Dim arr.i(size)
Debug ArraySize(arr())
Code: Select all
Dim arr.i(0)
Debug ArraySize(arr())
Define size.i = -1
ReDim arr(size)
Debug ArraySize(arr())
Code: Select all
Define size.i = -1
Dim arr.i(size)
Debug ArraySize(arr())
ReDim arr(0) ; --> Invalid memory access
Debug ArraySize(arr())
So I think at first look it is a bug that you can set the size to -1 and even ArraySize() reports the size as -1. And of course it is a bug that increasing the size results in an invalid memory access. But on the other hand it is very helpful when working with JSON and you don't want to program in special cases for empty arrays.
Let's discuss together if this should be moved to the Bug forum.
I tested it with PureBasic 6.20 (x64) on Ubuntu 24.04.