SBASIC & C++

Anything QL Software or Programming Related.
User avatar
tofro
Font of All Knowledge
Posts: 2685
Joined: Sun Feb 13, 2011 10:53 pm
Location: SW Germany

Re: SBASIC & C++

Post by tofro »

stevepoole wrote: But system memory limits array sizes to 256,
I am not aware of any C implementation on Windows that would limit array sizes so severely (not even C68 on QDOS does such things), but, there's rare beasts around, apparently.....
Maybe it would help if you consult your manual on how to increase the stack size in your C program.

I f you want (virtually) unlimited array sizes, you need to dive into the mysteries of dynamic mamory allocation in C.

Code: Select all

a = malloc (arraySize * sizeof (double));
would allocate <arraySize> doubles on the heap (rather: a lump of memory that has room for that much doubles) and return a pointer to it.
If you want the memory cleared on allocation, use

Code: Select all

a = calloc (arraySize, sizeof (double));
access to the array elements is as you are used to:

Code: Select all

a[xxx ] = value;
And if you no longer need the array, it needs to be returned to the system (that is, it's not automatically gone after you leave a function) using

Code: Select all

free (a);
You should be able to work on Megabyte-sized arrays using that method.

Tobias


ʎɐqǝ ɯoɹɟ ǝq oʇ ƃuᴉoƃ ʇou sᴉ pɹɐoqʎǝʞ ʇxǝu ʎɯ 'ɹɐǝp ɥO
stevepoole
Super Gold Card
Posts: 712
Joined: Mon Nov 24, 2014 2:03 pm

Re: SBASIC & C++

Post by stevepoole »

Hi again Tobias,
I am not quite sure that I understand this.
For an array zzz of size 256 :
Is zzz=malloc (256 * sizeof (double)); the correct statement?
Do I replace 'double' with 'int' for integer arrays?
Please elucidate, as I am very new to C++ coding...
Best Wishes,
Steve.


User avatar
tofro
Font of All Knowledge
Posts: 2685
Joined: Sun Feb 13, 2011 10:53 pm
Location: SW Germany

Re: SBASIC & C++

Post by tofro »

stevepoole wrote:Hi again Tobias,
I am not quite sure that I understand this.
For an array zzz of size 256 :
Is zzz=malloc (256 * sizeof (double)); the correct statement?
Do I replace 'double' with 'int' for integer arrays?
Please elucidate, as I am very new to C++ coding...
Best Wishes,
Steve.
Steve,
both your statements are correct. malloc and calloc are close to what RESPR or ALCHP do in SBASIC - They allocate memory "outside" the program's allocated data area. sizeof (double) evaluates to 8 on a PC (the size of a floating point variable), and you ask for 256 times that amount. If you want integer arrays, use "sizeof(int)", which would evaluate to 4 or 8 depending on your windows version.

Note the slight difference in notation between malloc and calloc - Although they basically do the same thing, the latter takes 2 arguments, the former only one - in your example malloc should be called with (256 * sizeof(double)), calloc with (256,sizeof double)).
The main difference between C and SBASIC here ist that the returned address can be comfortably treated as an array in C (like 'zzz[100] = 1.0), while this doesn't work in S*BASIC

Tobias


ʎɐqǝ ɯoɹɟ ǝq oʇ ƃuᴉoƃ ʇou sᴉ pɹɐoqʎǝʞ ʇxǝu ʎɯ 'ɹɐǝp ɥO
stevepoole
Super Gold Card
Posts: 712
Joined: Mon Nov 24, 2014 2:03 pm

Re: SBASIC & C++

Post by stevepoole »

HI again Tobias,
The TSP uses 9 arrays, and I want to get maximum size on my windows XP PC 1GoRAM.
At Present I have : int x[256], y[256],w[256],a[256],b[256],bes[256],be[256];
double bc[256],ds[256][256)]; I presume I have to use either malloc or calloc with each array.
Which of the two functions would you advise, since my program uses no pointers?
Best wishes,
Steve.


User avatar
tofro
Font of All Knowledge
Posts: 2685
Joined: Sun Feb 13, 2011 10:53 pm
Location: SW Germany

Re: SBASIC & C++

Post by tofro »

stevepoole wrote:HI again Tobias,
The TSP uses 9 arrays, and I want to get maximum size on my windows XP PC 1GoRAM.
At Present I have : int x[256], y[256],w[256],a[256],b[256],bes[256],be[256];
double bc[256],ds[256][256)]; I presume I have to use either malloc or calloc with each array.
Which of the two functions would you advise, since my program uses no pointers?
Best wishes,
Steve.
Steve,

first, your declarations have to be slightly re-written and would be changed to

Code: Select all

int *x, *y, *w, *a, *b, *bes, *be;
double *bc, *ds, 
Next you would adapt your initialization code (the above just declares the arrdesses, not the memory blocks that will live there). I guess that would happen somewhere early in main(), before any of the arrays are used - In case you are 100% sure you will never touch any array element that hasn't been initialized in your program explicitly, you can use malloc - If you aren't, use calloc, as otherwise your array elements will have random values after initialization. To make your life a little easier when changing array sizes afterwards, define an ARRAYSIZE constant that can easily be changed in one single place

Code: Select all

#define ARRAYSIZE 256

x = malloc (ARRAYSIZE * sizeof (int));
y = malloc (ARRAYSIZE * sizeof (int));
.
.
.
bc = malloc (ARRAYSIZE * sizeof (double));
dc = malloc (ARRAYSIZE * ARRAYSIZE * sizeof (double));  // Note we need ARRAYSIZE^2 bytes! 
With regards to sizes:
With ARRAYSIZE set to 256, your arrays will need around 7 * 256 * 4 + 256 * 8 + 256^2 * 8 = ~16k (assuming 32bit Windows versions)
You should be able to at least use 10240 as a size for starters (above formula evaluates to ~1GB in that case) - Actually, windows will allow you to go even beyond physical memory sizes, but will start to work with virtual memory and become slow.... I suppose you'd be starting small and gradually evaluate where you can go.

Regards,
Tobias


ʎɐqǝ ɯoɹɟ ǝq oʇ ƃuᴉoƃ ʇou sᴉ pɹɐoqʎǝʞ ʇxǝu ʎɯ 'ɹɐǝp ɥO
stevepoole
Super Gold Card
Posts: 712
Joined: Mon Nov 24, 2014 2:03 pm

Re: SBASIC & C++

Post by stevepoole »

Hi Tobias,
The QUINCY compiler does not accept malloc or free, but uses new & delete instead. Nevertheless, I was able to use your indications to allocate memory. First, at the slight expense of precision, I converted all DOUBLES to INT to free up some memory.
But I still can't get extra allocation for the array distance[256][256]. The code compiles, but just halts when executed if I try distance[65536] as you suggest. There is a syntax problem here, which I cannot see described in either of my C++ reference books.
Has anyone any suggestions?
Steve.


User avatar
tofro
Font of All Knowledge
Posts: 2685
Joined: Sun Feb 13, 2011 10:53 pm
Location: SW Germany

Re: SBASIC & C++

Post by tofro »

Steve,

I don't know the Quincy environment (which seems to be an IDE, BTW, not a compiler). The underlying compiler is apparently MingW, which is a gcc derivate - And should definitely support malloc() and free() (Anything that doesn't support those 2 calls can't seriously be considered a C or C++ compiler ;) )

Did you try #including stdlib.h at the beginning of your program? That is one of the pre-requisites for using those calls. Probably my fault I didn't mention that.

Apart from the above, I cannot see why code written allong the lines of what I pointed out shouldn't work or compile.

BTW: new() and delete() are C++ keywords and normally relate to the allocation of C++ objects. You can use them to allocate scalar arrays - albeit a bit complicated - , but wouldn't do that in case you are writing an otherwise C program. If you use those constructors and destructors, your code is C++, no longer C and you will not be able to back-port to C68.

For that big array, a line using new should look like

Code: Select all

distance = new int [ARRAYSIZE * ARRAYSIZE];
Note you don't need to tell the C++ compiler the number of bytes in an int in that case - It would already know.

Deleting the same thing would be done like

Code: Select all

delete [] distance;
(Note the funny place for the brackets which is needed to tell C++ you are deleting an array).

But I would rather refer to malloc() and free(), which should work if you include stdlib.h. That is more portable and easier to understand for beginners.

Regards,
Tobias


ʎɐqǝ ɯoɹɟ ǝq oʇ ƃuᴉoƃ ʇou sᴉ pɹɐoqʎǝʞ ʇxǝu ʎɯ 'ɹɐǝp ɥO
stevepoole
Super Gold Card
Posts: 712
Joined: Mon Nov 24, 2014 2:03 pm

Re: SBASIC & C++

Post by stevepoole »

Hi Tobias,
I tried turning your suggestions in every sense, but to no avail.
Only int distance=[512][512] compiles and runs successfully.
Using the new or malloc functions fail, even with ARRAYSIZE of 256 , but new works for 1D arrays...
This must be a quirk of the QUINCY graphics front-end system, which does indeed use MINGW.
Thanks for your suggestions all the same. At least with 100 nodes, I have been able to get useful timings to compare the 'shrink' TSP to other TSP programs....
I will try your code suggestions under devc++, but that way there is no graphics output!
Regards,
Steve.

Steve.


stevepoole
Super Gold Card
Posts: 712
Joined: Mon Nov 24, 2014 2:03 pm

Re: SBASIC & C++

Post by stevepoole »

Hi again,
Whenever I try to use malloc, I get - invalid conversion frm 'void*' to 'int*' -
Yet I have included <stdlib.h> and <new> headers.
Apparently there is a missing element in the initialisation code?
Steve.


User avatar
tofro
Font of All Knowledge
Posts: 2685
Joined: Sun Feb 13, 2011 10:53 pm
Location: SW Germany

Re: SBASIC & C++

Post by tofro »

Steve,

you have apparently run into some of the more subtile differences between C and C++ (and, you seem to be compiling using the C++ compiler).

C would accept

Code: Select all

distance = malloc (ARRAYSIZE * ARRAYSIZE * sizeof (int))
while C++ doesn't. You need to tell the compiler that you are actually expecting to get an int array by

Code: Select all

distance = (int*) malloc (ARRAYSIZE * ARRAYSIZE * sizeof (int));
C, as the "older" language is not so very picky on assigning "pointers to things" to "pointers to other things", while C++ pretty much is. You need to tell the compiler that you intend to use the allocated chunk of bytes as an integer, double, or whatever array. The (int*) above is what they call a "type cast" that exactly does that.

Regards,
Tobias


ʎɐqǝ ɯoɹɟ ǝq oʇ ƃuᴉoƃ ʇou sᴉ pɹɐoqʎǝʞ ʇxǝu ʎɯ 'ɹɐǝp ɥO
Post Reply