Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Print Using?
#41
Thumbs Up 
(11-03-2022, 05:59 PM)Spriggsy Wrote: Try "Declare CustomType Library" instead of "Declare Library"
Thank you for the tip! Learned something else about QB64PE.
Reply
#42
(11-03-2022, 05:58 PM)SMcNeill Wrote: You guys really do like to complicate things.  Tongue
Stick to the simple BASICs:

Why easy, if complicated is still another option?  Big Grin
Reply
#43
Lightbulb 
Wait, how do I write a string function that resides in a "dot-H" file? Memory has to be allocated for it, right, not just the "char-star"? Use "alloca()" or "malloc()" in C, or "cheat" using the "string" class in C++? Does the string buffer, only for allocation of a function's return value, have to be allocated by BASIC code (use "SPACE$()") or by C/C++ code? The "DECLARE CUSTOMTYPE LIBRARY" works really well with writing functions returning the simple types, but many people, including myself, would be interested in doing the same thing for string.

This is the main reason why I prefer coding it as sub with the last parameter being a string that is the return value. There's no need to use "_OFFSET" or some other solution I don't like. With returning "_INTEGER64" or "SINGLE" or something else then the "DECLARE CUSTOMTYPE LIBRARY" way is fine.

EDIT: I was able to figure it out with a strictly C method. Previously said "calloc()" above instead of "alloca()"; the former is like "malloc()" but is for setting up like an array, and the latter takes memory from the "stack" (function space) instead of the "heap" (global space). I hope it's all right using "alloca()" for allocating function return value because I've had an ugly experience with Psycle Lua scripting device; it was very easy to get it to run out of stack space so that music program crashed hard. Doing things in C/C++ means not having access to "qbs" system deep-rooted into QB64.

My small knowledge and patience of C language paid off. Previously I tried to do it with C++ "string" class but it didn't work. I used "c_str()" method to copy the object away from "string" class variable and into an allocated "char-star" buffer but it didn't work. Had to make many adjustments to get it to compile successfully but didn't produce the expected output.

EDIT #2: It is all right! From "man alloca" (man pages for "alloca()" C runtime library):
Quote:DESCRIPTION
The alloca() function allocates size bytes of space in the stack frame
of the caller. This temporary space is automatically freed when the
function that called alloca() returns to its caller.
Reply




Users browsing this thread: 2 Guest(s)