Gamestudio Links
Zorro Links
Newest Posts
lookback setting performance issue
by 7th_zorro. 04/16/24 03:08
folder management functions
by 7th_zorro. 04/15/24 10:10
zorro 64bit command line support
by 7th_zorro. 04/15/24 09:36
Zorro FIX plugin - Experimental
by flink. 04/14/24 07:48
Zorro FIX plugin - Experimental
by flink. 04/14/24 07:46
LPDIRECT3DCUBETEXTUR
E9

by Ayumi. 04/12/24 11:00
Sam Foster Sound | Experienced Game Composer for Hire
by titanicpiano14. 04/11/24 14:56
SGT_FW
by Aku_Aku. 04/10/24 16:09
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
2 registered members (7th_zorro, Quad), 373 guests, and 3 spiders.
Key: Admin, Global Mod, Mod
Newest Members
11honza11, ccorrea, sakolin, rajesh7827, juergen_wue
19045 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
String variable loses it's value #483279
05/15/21 20:16
05/15/21 20:16
Joined: Apr 2021
Posts: 41
Slovakia
T
tomna1993 Offline OP
Newbie
tomna1993  Offline OP
Newbie
T

Joined: Apr 2021
Posts: 41
Slovakia
Hi,

Why a string variable loses it's value during a long test?
I would like to save information into a file and for that I have to create a file and then save the data into the file. I save the data at the end of the test and in the INITRUN I create the file. I save the destination of the file into a string variable:
Code
string LogDestination = strf("Log\\%s_%s.txt", LogDate, Asset);

Zorro creates the file in the Log folder as supposed then run the test. At the end of the test I would like to save the data but I get error (ab.22). I figured it out that the LogDestination doesn't hold the string, it is overwritten during the test. I created one string variable and allocated memory to it where I save the data during the test. And somehow the LogDestination is overwritten by that data.
Should I allocate memory for LogDestination variable to keep it's value? Is this a normal behaviour?
I read that character array should solve this kind of problem. I have more string variables and I don't want to end up with incorrect data in my log file.

Re: String variable loses it's value [Re: tomna1993] #483280
05/15/21 20:31
05/15/21 20:31
Joined: Apr 2021
Posts: 41
Slovakia
T
tomna1993 Offline OP
Newbie
tomna1993  Offline OP
Newbie
T

Joined: Apr 2021
Posts: 41
Slovakia
Character array solved the problem.

Re: String variable loses it's value [Re: tomna1993] #483282
05/15/21 21:12
05/15/21 21:12
Joined: Feb 2017
Posts: 1,724
Chicago
AndrewAMD Offline
Serious User
AndrewAMD  Offline
Serious User

Joined: Feb 2017
Posts: 1,724
Chicago
string is typedef'd to char*.

strf returns a temporary pointer to a buffer that gets used over and over again. So of course, using your own fixed buffer will solve the problem.

Re: String variable loses it's value [Re: tomna1993] #485985
05/22/22 13:45
05/22/22 13:45
Joined: Apr 2022
Posts: 21
H
HamzaAhmed Offline
Newbie
HamzaAhmed  Offline
Newbie
H

Joined: Apr 2022
Posts: 21
Also solves lots of confusion in my tests too... Thanks

Re: String variable loses it's value [Re: tomna1993] #486799
10/12/22 01:52
10/12/22 01:52
Joined: Oct 2017
Posts: 52
Brazil
J
jmlocatelli Offline
Junior Member
jmlocatelli  Offline
Junior Member
J

Joined: Oct 2017
Posts: 52
Brazil
Hi,
I'm trying to assign different values to each element in a string array with no success.
The values come from parsing a .ini file using the strtext() function.
As strtext() output is a temporary array, I copy the temporary array to a char array with the intention to make the value permanent.
And then assign its value to the element array.
But at the end, all elements in the array end up with the same value. The last temporary string.

My question is how to permanently assign a value to a string array element that comes from a temporary array.
It seems simple, but I'm messing up my head.

This is the code:

#define MAXGROUPS 5
string NamesGroups[MAXGROUPS];
char s1[100];
char s2[100];

function main()
{
string Ini_File = "...file.ini";
string ini_content = file_content(Ini_File);

for(i=0; i<MAXGROUPS; i++)
{
strcpy(s1,strf("NameGroup%02i",i));
strcpy(s2,strtext(ini_content,s1,""));
NamesGroups[i] = s2;
printf("\n************** %s = %s",s1,NamesGroups[i]);
}

// check the result
for(i=0; i<MAXGROUPS; i++)
printf("\n- NameGroup%02i = %s",i,NamesGroups[i]);
}

This is the output:

s1 = NameGroup00
s2 = StocksLong
************** NameGroup00 = StocksLong
s1 = NameGroup01
s2 = StocksShort
************** NameGroup01 = StocksShort
s1 = NameGroup02
s2 = BDRs
************** NameGroup02 = BDRs
s1 = NameGroup03
s2 = RV_ETFs
************** NameGroup03 = RV_ETFs
s1 = NameGroup04
s2 = FIIs
************** NameGroup04 = FIIs
- NameGroup00 = FIIs
- NameGroup01 = FIIs
- NameGroup02 = FIIs
- NameGroup03 = FIIs
- NameGroup04 = FIIs

Re: String variable loses it's value [Re: tomna1993] #486804
10/12/22 13:06
10/12/22 13:06
Joined: Feb 2017
Posts: 1,724
Chicago
AndrewAMD Offline
Serious User
AndrewAMD  Offline
Serious User

Joined: Feb 2017
Posts: 1,724
Chicago
NamesGroups is an array of pointers.

Your pointers are pointing to the s2 buffer, which has "FIIs" in its contents.

If you want to permanently retain more strings, make more buffers.

Re: String variable loses it's value [Re: tomna1993] #486809
10/12/22 18:30
10/12/22 18:30
Joined: Oct 2017
Posts: 52
Brazil
J
jmlocatelli Offline
Junior Member
jmlocatelli  Offline
Junior Member
J

Joined: Oct 2017
Posts: 52
Brazil
Hi,
Thank you for the answer.

So, is string array always an array of pointers?

Creating a buffer for each element of the array makes it impossible to use the loop to read the parameters.
And the idea here is to have a large and indetermined number of parameters, hence the loop.
MAXGROUPS = 5 for test purpose.

tks
jm


Last edited by jmlocatelli; 10/12/22 18:30.
Re: String variable loses it's value [Re: tomna1993] #486810
10/12/22 20:24
10/12/22 20:24
Joined: Feb 2017
Posts: 1,724
Chicago
AndrewAMD Offline
Serious User
AndrewAMD  Offline
Serious User

Joined: Feb 2017
Posts: 1,724
Chicago
You need to learn C. Read this now:
https://zorro-project.com/manual/en/aarray.htm
https://zorro-project.com/manual/en/apointer.htm
https://zorro-project.com/manual/en/str_.htm

A string is identical to char*. That's a pointer (an address).

A char buffer is the "home" of the data, which has an address.

Did you know you can make an array of char arrays? Like this:
Code
char foo[5][100];

Re: String variable loses it's value [Re: tomna1993] #486811
10/12/22 23:12
10/12/22 23:12
Joined: Oct 2017
Posts: 52
Brazil
J
jmlocatelli Offline
Junior Member
jmlocatelli  Offline
Junior Member
J

Joined: Oct 2017
Posts: 52
Brazil
Thank you for the advise.

In fact I've read these articles several times and couldn't get a good understanding,
After many languages I must be too old to understand well.

Anyway, I came up with a solution. I don't know if the best or correct one, but it worked.

Thank you again.

The code:

#define MAXGROUPS 5
char s1[50];
char* NamesGroups[MAXGROUPS][50];

function main()
{
string Ini_File = "...file.ini";
string ini_content = file_content(Ini_File);

printf("\nAssigning values");
for(i=0; i<MAXGROUPS; i++)
{
strcpy(s1,strf("NameGroup%02i",i));
strcpy(NamesGroups+50*i, strtext(ini_content,strf("NameGroup%02i",i), ""));
printf("\n************** %s = %s",s1,NamesGroups+50*i);
}

printf("\nChecking values");
for(i=0; i<MAXGROUPS; i++)
printf("\n- NameGroup%02i = %s",i,NamesGroups+50*i);
}

The result:

Assigning values
************** NameGroup00 = StocksLong
************** NameGroup01 = StocksShort
************** NameGroup02 = BDRs
************** NameGroup03 = RV_ETFs
************** NameGroup04 = FIIs
Checking values
- NameGroup00 = StocksLong
- NameGroup01 = StocksShort
- NameGroup02 = BDRs
- NameGroup03 = RV_ETFs
- NameGroup04 = FIIs


Moderated by  Petra 

Powered by UBB.threads™ PHP Forum Software 7.7.1