Raised This Month: $32 Target: $400
 8% 

Misunderstandings with strings


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Electr000999
Senior Member
Join Date: Aug 2011
Old 01-07-2022 , 11:01   Misunderstandings with strings
Reply With Quote #1

Hi, i have a dumb question, here my easy example for explane my problem:

Code:
#include <sourcemod>

public OnPluginStart()
{
	RegConsoleCmd ( "sm_test", CmdTest);
}

public Action CmdTest(int client, int args)
{
	Test1("FOOBAR", "FOOBAR");
	
	return Plugin_Handled;
}

void Test1(char[] sTemp1, char[] sTemp2)
{
	PrintToServer("Temp1: %s, sTemp2: %s", sTemp1, sTemp2);
	
	FormatEx(sTemp1, 32, "MOOBAR");
	FormatEx(sTemp2, 32, "MOOBAR");
}
after first run sm_test:
Temp1: FOOBAR, sTemp2: FOOBAR


after second run sm_test:
Temp1: MOOBAR, sTemp2: MOOBAR

after third and other and other runs sm_test:
Temp1: MOOBAR, sTemp2: MOOBAR


in my logic if i am call function Test1 with args FOOBAR it should be they must be passed inside the function, but it only happens once, why?
Electr000999 is offline
Send a message via Skype™ to Electr000999
Electr000999
Senior Member
Join Date: Aug 2011
Old 01-07-2022 , 16:45   Re: Misunderstandings with strings
Reply With Quote #2

Quote:
Originally Posted by Electr000999 View Post
Hi, i have a dumb question, here my easy example for explane my problem:

Code:
#include <sourcemod>

public OnPluginStart()
{
	RegConsoleCmd ( "sm_test", CmdTest);
}

public Action CmdTest(int client, int args)
{
	Test1("FOOBAR", "FOOBAR");
	
	return Plugin_Handled;
}

void Test1(char[] sTemp1, char[] sTemp2)
{
	PrintToServer("Temp1: %s, sTemp2: %s", sTemp1, sTemp2);
	
	FormatEx(sTemp1, 32, "MOOBAR");
	FormatEx(sTemp2, 32, "MOOBAR");
}
after first run sm_test:
Temp1: FOOBAR, sTemp2: FOOBAR


after second run sm_test:
Temp1: MOOBAR, sTemp2: MOOBAR

after third and other and other runs sm_test:
Temp1: MOOBAR, sTemp2: MOOBAR


in my logic if i am call function Test1 with args FOOBAR it should be they must be passed inside the function, but it only happens once, why?
resolved, i am not change string args sTemp1 and sTemp2 inside function Test1, just copy values from them in other vars and wotk with them, like in example:

Code:
#include <sourcemod>

public OnPluginStart()
{
	RegConsoleCmd ( "sm_test", CmdTest);
}

public Action CmdTest(int client, int args)
{
	Test1("FOOBAR", "FOOBAR");
	
	return Plugin_Handled;
}

void Test1(char[] sTemp1, char[] sTemp2)
{
	PrintToServer("Temp1: %s, sTemp2: %s", sTemp1, sTemp2);
	
	char sTemp3[65], sTemp4[65];
	strcopy(sTemp3, 65, sTemp1);
	strcopy(sTemp4, 65, sTemp2);
	
	FormatEx(sTemp3, 32, "MOOBAR");
	FormatEx(sTemp4, 32, "MOOBAR");
}
Electr000999 is offline
Send a message via Skype™ to Electr000999
Bacardi
Veteran Member
Join Date: Jan 2010
Location: mom's basement
Old 01-07-2022 , 21:44   Re: Misunderstandings with strings
Reply With Quote #3

I have no good knowledge of coding, what happens here.

My wild guess is, in that function Test1,
you set text into parameter, after compiling and loading plugin into server it reserve that text in memory.
Then you need keep in mind, this memory location could turn to garbage in some point (I'm not 100%).

If you want string to be same in every call, you need... allocate that ? I'm not sure what I'm saying.
For example: Do char buffer and pass that into function.

PHP Code:
public void OnPluginStart()
{
    
RegConsoleCmd "sm_test"CmdTest);
}

public 
Action CmdTest(int clientint args)
{
    
char buffer[] = "FOOBAR1";

    
PrintToServer("PRE buffer %s"buffer);

    
Test1(buffer"FOOBAR2");

    
PrintToServer("POST buffer %s"buffer);

    return 
Plugin_Handled;
}

void Test1(char[] string1char[] string2)
{
    
PrintToServer("PRE Test1 %s %s"string1string2);

    
Format(string18"BARFOO1");
    
Format(string28"BARFOO2");

    
PrintToServer("POST Test1 %s %s"string1string2);


output
Code:
sm plugins reload test
[SM] Plugin test.smx reloaded successfully.






sm_test
PRE buffer FOOBAR1
PRE Test1 FOOBAR1 FOOBAR2
POST Test1 BARFOO1 BARFOO2
POST buffer BARFOO1


sm_test
PRE buffer FOOBAR1
PRE Test1 FOOBAR1 BARFOO2
POST Test1 BARFOO1 BARFOO2
POST buffer BARFOO1


sm_test
PRE buffer FOOBAR1
PRE Test1 FOOBAR1 BARFOO2
POST Test1 BARFOO1 BARFOO2
POST buffer BARFOO1
__________________
Do not Private Message @me
Bacardi is offline
azalty
AlliedModders Donor
Join Date: Feb 2020
Location: France
Old 01-15-2022 , 15:50   Re: Misunderstandings with strings
Reply With Quote #4

Quote:
Originally Posted by Bacardi View Post
My wild guess is, in that function Test1,
you set text into parameter, after compiling and loading plugin into server it reserve that text in memory.
Thanks!

This thread and this answer are very useful
__________________
GitHub | Discord: @azalty | Steam
azalty is offline
Bacardi
Veteran Member
Join Date: Jan 2010
Location: mom's basement
Old 01-15-2022 , 15:55   Re: Misunderstandings with strings
Reply With Quote #5

Hope devs can explain this right and in detail.
__________________
Do not Private Message @me
Bacardi is offline
Silvers
SourceMod Plugin Approver
Join Date: Aug 2010
Location: SpaceX
Old 01-15-2022 , 17:55   Re: Misunderstandings with strings
Reply With Quote #6

Which version of SM? This seems strange behavior and unexpected from what should happen. I haven't tested but at some point hopefully I will edit with a test case in SM 1.10.

Since it's printing before modification I would assume the string to be "FOOBAR" always, but as Bacardi said this is maybe something with buffers. Maybe fixed in latest and broken with others, or broken broken in latest but working correctly with others. SM 1.11 is still in development so that could be the reason if you use 1.11.
__________________
Silvers is offline
Bacardi
Veteran Member
Join Date: Jan 2010
Location: mom's basement
Old 01-15-2022 , 20:55   Re: Misunderstandings with strings
Reply With Quote #7

Quick one
Spoiler





*edit
here is example of function, which have default value in parameter
PHP Code:
public void OnPluginStart()
{
    
RegConsoleCmd "sm_test"CmdTest);
}

public 
Action CmdTest(int clientint args)
{
    
char buffer[] = "FOOBAR1";

    
PrintToServer("PRE buffer %s"buffer);

    
Test1(buffer"FOOBAR2");

    
PrintToServer("POST buffer %s"buffer);

    return 
Plugin_Handled;
}

void Test1(char[] string1char[] string2char[] string3 "FOOBAR3")
{
    
PrintToServer("PRE Test1 %s %s %s"string1string2string3);

    
Format(string18"BARFOO1");
    
Format(string28"BARFOO2");
    
Format(string38"BARFOO3");

    
PrintToServer("POST Test1 %s %s %s"string1string2string3);

Code:
sm_test
PRE buffer FOOBAR1
PRE Test1 FOOBAR1 FOOBAR2 FOOBAR3
POST Test1 BARFOO1 BARFOO2 BARFOO3
POST buffer BARFOO1


sm_test
PRE buffer FOOBAR1
PRE Test1 FOOBAR1 BARFOO2 FOOBAR3
POST Test1 BARFOO1 BARFOO2 BARFOO3
POST buffer BARFOO1


sm_test
PRE buffer FOOBAR1
PRE Test1 FOOBAR1 BARFOO2 FOOBAR3
POST Test1 BARFOO1 BARFOO2 BARFOO3
POST buffer BARFOO1
__________________
Do not Private Message @me

Last edited by Bacardi; 01-16-2022 at 04:29.
Bacardi is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 12:12.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode