Print Using?
#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


Messages In This Thread
Print Using? - by james2464 - 11-01-2022, 11:39 PM
RE: Print Using? - by bplus - 11-01-2022, 11:45 PM
RE: Print Using? - by mnrvovrfc - 11-01-2022, 11:49 PM
RE: Print Using? - by Pete - 11-01-2022, 11:51 PM
RE: Print Using? - by james2464 - 11-02-2022, 12:03 AM
RE: Print Using? - by Kernelpanic - 11-02-2022, 12:23 AM
RE: Print Using? - by PhilOfPerth - 11-02-2022, 12:26 AM
RE: Print Using? - by justsomeguy - 11-02-2022, 12:36 AM
RE: Print Using? - by james2464 - 11-02-2022, 12:54 AM
RE: Print Using? - by Kernelpanic - 11-02-2022, 01:03 AM
RE: Print Using? - by PhilOfPerth - 11-02-2022, 01:33 AM
RE: Print Using? - by justsomeguy - 11-02-2022, 01:55 AM
RE: Print Using? - by SMcNeill - 11-02-2022, 02:51 PM
RE: Print Using? - by james2464 - 11-02-2022, 03:37 PM
RE: Print Using? - by mdijkens - 11-02-2022, 06:04 PM
RE: Print Using? - by bplus - 11-02-2022, 06:16 PM
RE: Print Using? - by james2464 - 11-02-2022, 06:22 PM
RE: Print Using? - by bplus - 11-02-2022, 06:25 PM
RE: Print Using? - by james2464 - 11-02-2022, 06:33 PM
RE: Print Using? - by Pete - 11-02-2022, 06:33 PM
RE: Print Using? - by james2464 - 11-02-2022, 06:43 PM
RE: Print Using? - by mnrvovrfc - 11-02-2022, 06:48 PM
RE: Print Using? - by Pete - 11-02-2022, 06:54 PM
RE: Print Using? - by TerryRitchie - 11-02-2022, 09:22 PM
RE: Print Using? - by james2464 - 11-02-2022, 06:51 PM
RE: Print Using? - by Kernelpanic - 11-02-2022, 10:36 PM
RE: Print Using? - by SMcNeill - 11-03-2022, 12:11 AM
RE: Print Using? - by Kernelpanic - 11-03-2022, 12:47 AM
RE: Print Using? - by mnrvovrfc - 11-03-2022, 12:07 PM
RE: Print Using? - by Kernelpanic - 11-03-2022, 03:26 PM
RE: Print Using? - by SpriggsySpriggs - 11-03-2022, 03:44 PM
RE: Print Using? - by bplus - 11-03-2022, 03:53 PM
RE: Print Using? - by Kernelpanic - 11-03-2022, 03:59 PM
RE: Print Using? - by SpriggsySpriggs - 11-03-2022, 04:36 PM
RE: Print Using? - by mnrvovrfc - 11-03-2022, 05:17 PM
RE: Print Using? - by SpriggsySpriggs - 11-03-2022, 05:59 PM
RE: Print Using? - by mnrvovrfc - 11-03-2022, 09:16 PM
RE: Print Using? - by bplus - 11-03-2022, 05:22 PM
RE: Print Using? - by mnrvovrfc - 11-03-2022, 05:32 PM
RE: Print Using? - by bplus - 11-03-2022, 05:57 PM
RE: Print Using? - by SMcNeill - 11-03-2022, 05:58 PM
RE: Print Using? - by Kernelpanic - 11-04-2022, 12:57 AM
RE: Print Using? - by mnrvovrfc - 11-04-2022, 03:47 PM



Users browsing this thread: 6 Guest(s)