Posts: 1,031
Threads: 246
Joined: Jul 2022
06-03-2023, 09:06 AM
(This post was last modified: 06-03-2023, 09:07 AM by Davider.)
Hi,
The following code is executed multiple times and the variable values will continue to stack. How to reset it?
I need to recompile the code every time it is executed
PS: I have some difficulties in understanding C language programming and pointer variables
Thank you in advance for any suggestions and help!
david
Macro
Tcc2
dll- "" $str_add $a $b
__Tcc+ t
int compileOnce
;int compileOnce=32
__Tcc+ t
if !t.f or !compileOnce
,t.Compile("" "add" 0|compileOnce)
,&str_add=t.f
out str_add("hello" " world!")
#ret
char* add(char* a, char* b)
{
,printf("%s: a=%s, b=%s", __func__, a, b);
,strcat(a, b);
,return a;
}
Posts: 12,071
Threads: 140
Joined: Dec 2002
C is a very unsafe language and can be used only when you fully understand how the code works. Your C function does some bad things. Afterwards you'll notice some strange things happening in some unrelated places in QM and will be very difficult to find the reason.
Posts: 1,031
Threads: 246
Joined: Jul 2022
06-03-2023, 10:25 AM
(This post was last modified: 06-03-2023, 10:32 AM by Davider.)
Thanks for your reminder, it is indeed like this, sometimes QM will automatically close
PS: The C code above was written with the help of ChatGPT
In the TCC example in QM, the return values are all integers. Sometimes I find some C++functions that return strings, so I tried the above,
The above code is very short. Is there a simple solution? Or can you provide an example of a function that returns a string type?
Posts: 12,071
Threads: 140
Joined: Dec 2002
Google for how to return a string in C.
Posts: 1,031
Threads: 246
Joined: Jul 2022
06-03-2023, 10:52 AM
(This post was last modified: 06-03-2023, 12:42 PM by Davider.)
Another solution provided by ChatGPT, currently no issues have been identified
Additionally, (I don't want to generate a DLL) how to release C code resources in memory in QM, just like
UnloadDll....
Macro
Tcc2
dll- "" $str_add $a $b
int compileOnce
;int compileOnce=32
__Tcc+ t
if !t.f or !compileOnce
,t.Compile("" "add" 0|compileOnce)
,&str_add=t.f
out str_add("hello" " world!")
#ret
char* add(char* a, char* b)
{
,printf("%s: a=%s, b=%s", __func__, a, b);
,char* result = malloc(strlen(a) + strlen(b) + 1);
,strcpy(result, a);
,strcat(result, b);
,return result;
}
Posts: 12,071
Threads: 140
Joined: Dec 2002
Now only a memory leak.
Quote:how to release C code resources in memory
I don't remember, probably a __Tcc variable releases the compiled code memory when dying. Use local variable or thread variable.
Posts: 1,031
Threads: 246
Joined: Jul 2022