String variable loses it's value

Posted By: tomna1993

String variable loses it's value - 05/15/21 20:16

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.
Posted By: tomna1993

Re: String variable loses it's value - 05/15/21 20:31

Character array solved the problem.
Posted By: AndrewAMD

Re: String variable loses it's value - 05/15/21 21:12

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.
Posted By: HamzaAhmed

Re: String variable loses it's value - 05/22/22 13:45

Also solves lots of confusion in my tests too... Thanks
Posted By: jmlocatelli

Re: String variable loses it's value - 10/12/22 01:52

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
Posted By: AndrewAMD

Re: String variable loses it's value - 10/12/22 13:06

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.
Posted By: jmlocatelli

Re: String variable loses it's value - 10/12/22 18:30

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

Posted By: AndrewAMD

Re: String variable loses it's value - 10/12/22 20:24

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];
Posted By: jmlocatelli

Re: String variable loses it's value - 10/12/22 23:12

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
© 2024 lite-C Forums