Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
error on function returning _mem
#1
Hello,
I don't understand why the following code crashes during compilation.
I understood that a function should not return a type defined by TYPE, but that native types were permitted ?
Code: (Select All)
print test.size

declare function test as _mem

function test ()
    test = _memnew(1753)
end function
compilelog.txt says that :
g++  -no-pie -std=gnu++17 -fno-strict-aliasing -Wno-conversion-null -DFREEGLUT_STATIC -I./internal/c/libqb/include -I./internal/c/parts/core/freeglut/include -I./internal/c/parts/core/glew/include -DDEPENDENCY_NO_SOCKETS -DDEPENDENCY_NO_PRINTER -DDEPENDENCY_NO_ICON -DDEPENDENCY_NO_SCREENIMAGE internal/c/qbx.cpp -c -o internal/c/qbx.o
In file included from internal/c/qbx.cpp:1742:
internal/c/../temp/main.txt: In function ‘float FUNC_TEST()’:
internal/c/../temp/main.txt:30:31: error: cannot convert ‘mem_block’ to ‘float’ in assignment
  30 | *_FUNC_TEST_SINGLE_TEST=(func2=func__memnew( 1753 ));
      |                        ~~~~~~^~~~~~~~~~~~~~~~~~~~~~
      |                              |
      |                              mem_block
make: *** [Makefile:413 : internal/c/qbx.o] Erreur 1
Reply
#2
_MEM is more like a built-in UDT rather than a native type, it can't be returned from functions (except for from some special built-in functions like `_MEMNEW()`).

QB64 also doesn't help you here because the `declare function` lines are ignored and aren't actually checked for validity.
Reply
#3
internal/c/../temp/main.txt:30:31: error: cannot convert ‘mem_block’ to ‘float’ in assignment
Reply
#4
(07-04-2025, 08:04 PM)DSMan195276 Wrote: _MEM is more like a built-in UDT rather than a native type, it can't be returned from functions (except for from some special built-in functions like `_MEMNEW()`).

QB64 also doesn't help you here because the `declare function` lines are ignored and aren't actually checked for validity.
Okay. I am a bit confused; I had hoped to bypass the limit imposed on function typing by working around the issue with a _mem.
Thank you for the explanation!
Reply
#5
Hi Herve
this is what you were trying to do

Code: (Select All)
Dim t As _MEM
Print test(t)

declare function test as _mem ' it is ignored like a REM or ' comment
End
Function test (tMem As _MEM)
    tMem = _MemNew(1753)
    test = tMem.SIZE
End Function

you declare a variable of native type desired
you use your custom function for assigning a piece of ram to variable and for getting back its dimension
the declare row of code is unuseful because QB64pe compiler ignore it as for REM lines.
It is good code practice to close the main with End or System keyword.
Reply
#6
take out line #4

"declare" is unnecessary in qb64.

that method works in freebasic.  for a function that takes no parameters.

at the moment cannot create a function.  which returns _mem.  must be a subprogram.  with parameter change side-effect.

also in above program.  "test" should be "test&&" in 64-bit.  in declaration line #6.


the program should be so:

Code: (Select All)
Dim t As _MEM
Print test(t)
'the following line is not acceptable in qb64:
'declare function test as _mem
End
'this assumes 64-bit cpu:
Function test&& (tMem As _MEM)
    tMem = _MemNew(1753)
    test&& = tMem.SIZE
    'free memory blocks after they are used:
    _MemFree tMem
End Function
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Mac debugger not connecting, a user error! BlameTroi 0 98 02-07-2026, 06:18 PM
Last Post: BlameTroi
Star Suggestion for new REPLACE$() function zaadstra 3 235 01-26-2026, 05:11 PM
Last Post: grymmjack
  ERROR MESSAGES COLORS ? aurel 5 376 01-02-2026, 11:26 AM
Last Post: aurel
  Is there a menu function? Mad Axeman 17 1,018 12-17-2025, 04:43 AM
Last Post: SMcNeill
  Using CONST & _RGB used together seem to error... Dav 12 667 12-12-2025, 12:29 AM
Last Post: Dav

Forum Jump:


Users browsing this thread: 1 Guest(s)