equivalent program in FreeBasic?

Windows specific questions.
Post Reply
gfernval
Posts: 2
Joined: Jan 18, 2025 1:25

equivalent program in FreeBasic?

Post by gfernval »

I use the following C program to check the memory available in Windows:

Code: Select all

#include <stdlib.h>
#include <stdio.h>

void fail(char *s)
{
    puts(s);
    putchar(7); /* beep */
    exit(1);
}

main(int argc, char *argv[])
{
    char *p;
    unsigned long int kb;
    unsigned long int allocs;

    if (argc < 2)
        kb = 1024;      /* default 1 megabyte, equivalent to 1024 kilobytes */
    else
        kb = strtoul(argv[1], &p, 10);  /* command-line option: n kilobytes */

    printf("\nAllocating %lu kilobyte%s... ",
    kb, ((kb == 0) || (kb > 1)) ? "s" : "");

    for (allocs = 0; allocs < kb; allocs++)
       if ((p = malloc(1024)) != 0)    /* in 1 kilobyte blocks */
       {
           *p = 'x';            /* do something, anything with */
           p[1023] = 'y';       /* the allocated memory        */
       }
       else
       {
           printf("only %lu kilobyte%s available\n",
           allocs, ((allocs == 0) || (allocs > 1)) ? "s" : "");
           fail("Insufficient memory!");
       }

   puts("ok");
   return 0;
}

It is possible to do a version in FreeBasic? Don´t know if FreeBasic has a function equivalent to malloc in C
UEZ
Posts: 1028
Joined: May 05, 2017 19:59
Location: Germany

Re: equivalent program in FreeBasic?

Post by UEZ »

Try this:

Code: Select all

#include "string.bi"
#include "windows.bi"
#include "win/psapi.bi"

Dim As _PERFORMANCE_INFORMATION PERFORMANCE_INFORMATION
GetPerformanceInfo(@PERFORMANCE_INFORMATION, SizeOf(_PERFORMANCE_INFORMATION))
Dim As Double total, available, size = 1024 ^ 3
With PERFORMANCE_INFORMATION
	total = .PhysicalTotal / size * .PageSize
	available = .PhysicalAvailable / size * .PageSize
	? "Total: " & Format(total, "#,##0.00 gb") 
	? "Available: " & Format(available, "#,##0.00 gb / ") & Format(1 - .PhysicalAvailable / .PhysicalTotal, "0.00%")
End With
Sleep
fxm
Moderator
Posts: 12473
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: equivalent program in FreeBasic?

Post by fxm »

More simply, the FreeBASIC 'Fre' keyword directly returns the amount of free memory available:

Code: Select all

Print Using "Free memory available:###.## GB"; fre / 1024 / 1024 / 1024
Sleep

gfernval wrote: Jan 18, 2025 22:52 Don´t know if FreeBasic has a function equivalent to malloc in C
FreeBASIC functions for working with dynamic memory:
- 'Allocate' : Reserves a number of bytes of uninitialized memory and returns the address.
- 'CAllocate' : Reserves a number of bytes of initialized (zeroed) memory and returns the address.
- 'Reallocate' : Changes the size of reserved memory.
- 'Deallocate' : Returns reserved memory back to the system.

FreeBASIC operators for working with dynamic memory:
- 'New' Expression : Allocates memory for and constructs objects.
- 'New' Overload : Overloads memory allocation process of Operator 'New' Expression when applying to UDT.
- Placement 'New' : Constructs objects at a specified memory location.
- 'Delete' Statement : Destroys and deallocates memory for objects.
- 'Delete' Overload : Overloads memory deallocation process of Operator 'Delete' Statement when applying to UDT.
gfernval
Posts: 2
Joined: Jan 18, 2025 1:25

Re: equivalent program in FreeBasic?

Post by gfernval »

Only know C and Pascal and a little Basic (I learned Basic with a Commmodore)
The program in C, once compiled (to mem.exe for example), when running inside a dos command prompt in Windows XP 32 bit,
(run "mem 4000000") with output "only 1980000 bytes available" due Windows XP 32 bit less than 4gb of memory, but if I run
such program ("mem 40000000") inside a command prompt in a Windows 10 computer with 8gb of memory if does the memory
allocation successfully. Does FreeBasic supports command line parameters?
fxm
Moderator
Posts: 12473
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: equivalent program in FreeBasic?

Post by fxm »

gfernval wrote: Jan 19, 2025 21:17 Does FreeBasic supports command line parameters?

Yes, see the 'Command()' keyword,
or the intrinsic defines (macro value) set by the compiler: '__FB_ARGC__' and '__FB_ARGV__'.
Post Reply