SA-MP Forums Archive
Strings overlap eachother. - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: Strings overlap eachother. (/showthread.php?tid=596020)



Strings overlap eachother. - justinnater - 11.12.2015

Hello, I ran into a bug from a really short code but I just can't find what I did wrong.

I'm using myStrcpy to store strings in a variable.
Код:
myStrcpy(to[], const from[])
{
  new length = strlen(from);
  for (new i = 0; i <= length; i++)
  {
    to[i] = from[i];
  }
  return 1;
}
i use a stock to reset the inventory from a player like this:
Код:
stock ResetInventory(playerid)
{
	for(new b=26; b<=53; b++)
	{
		myStrcpy(InventoryInfo[playerid][invtext][b],"None");
		if(b==26) printf("test3: %s",InventoryInfo[playerid][invtext][26]);
		if(b==27) printf("test4: %s",InventoryInfo[playerid][invtext][26]);
		if(b==28) printf("test5: %s",InventoryInfo[playerid][invtext][26]);
	}
	return 1;
}
I already made some prints in that stock which might be usefull to locate the problem for you guys since I cant find the issue.
Код:
test3: None
test4: NNone
test5: NNNone
As you see the string overlap eachother.
Hopefully someone can help me!

Here is the enum for InventoryInfo
Код:
enum inventorystats
{
	invtext[64],
	itemid,
	ammo,
	sort,
}
new InventoryInfo[MAX_PLAYERS][inventorystats][54];



Re: Strings overlap eachother. - vassilis - 11.12.2015

strcpy is doing string copy i don't really think is the best way to store strings in a variable.


Re: Strings overlap eachother. - justinnater - 11.12.2015

Quote:
Originally Posted by vassilis
Посмотреть сообщение
strcpy is doing string copy i don't really think is the best way to store strings in a variable.
Im using this function in diffrent situations aswell and it just sets the string into the variable properly.
Only in this stock its just not functioning the way I want it to.


Re: Strings overlap eachother. - vassilis - 11.12.2015

pawn Код:
if(b==26) printf("test3: %s",InventoryInfo[playerid][invtext][26]);
        if(b==27) printf("test4: %s",InventoryInfo[playerid][invtext][27]);
        if(b==28) printf("test5: %s",InventoryInfo[playerid][invtext][28]);
    }
I might be wrong but try this


Re: Strings overlap eachother. - justinnater - 11.12.2015

Thats not going to fix the problem since I just placed those for debug purposes.
I had to see where it went wrong exactly and I provided this information with ya'll incase it'be usefull.

But incase you are curious of what it prints exactly

test3: None
test4: None
test5: None

But when the loop is done and I print the first one again for example... the print is:
Код:
printf("test5: %s",InventoryInfo[playerid][invtext][26]);
test5: NNNNNNNNNNNNNNNNNNNNNNNNNNNNone



Re: Strings overlap eachother. - vassilis - 11.12.2015

You want it to print None in the from b 26 to 53 right?


Re: Strings overlap eachother. - Vince - 11.12.2015

This is a really dangerous function because it has no out-of-bounds checking whatsoever. The max length to copy should be the size of the destination, not the size of the source. But yeah, if you just want to copy strings use the proven method because this still seems really crappy (sorry). You may still use your own function to copy arrays.

PHP код:
strcpy(dest[], const source[], maxlength sizeof dest)
    return 
strcat((dest[0] = EOSdest), sourcemaxlength); 



Re: Strings overlap eachother. - vassilis - 11.12.2015

Quote:
Originally Posted by Vince
Посмотреть сообщение
This is a really dangerous function because it has no out-of-bounds checking whatsoever. The max length to copy should be the size of the destination, not the size of the source. But yeah, if you just want to copy strings use the proven method because this still seems really crappy (sorry). You may still use your own function to copy arrays.

PHP код:
strcpy(dest[], const source[], maxlength sizeof dest)
    return 
strcat((dest[0] = EOSdest), sourcemaxlength); 
Just because i have curiosity. He want to store string in variable. What other method could we use?


Re: Strings overlap eachother. - SickAttack - 11.12.2015

@vassilis - This:

pawn Код:
#define strcpy(%0,%1,%2) strcat((%0[0] = '\0', %0), %1, %2)

stock ResetInventory(playerid)
{
    for(new b = 26; b <= 53; b ++)
    {
        strcpy(InventoryInfo[playerid][invtext][b], "None", 5);
    }
    return 1;
}
Is the same as:

pawn Код:
stock ResetInventory(playerid)
{
    for(new b = 26; b <= 53; b ++)
    {
        InventoryInfo[playerid][invtext][b][0] = EOS;
        strcat(InventoryInfo[playerid][invtext][b], "None", 5);
    }
    return 1;
}
You shouldn't be going around looking for other methods if you cannot even get this one to work. And the above is pretty much what you would want to go with.


Re: Strings overlap eachother. - justinnater - 11.12.2015

Quote:
Originally Posted by vassilis
Посмотреть сообщение
Just because i have curiosity. He want to store string in variable. What other method could we use?
I wonder the same.