Strings overlap eachother.
#1

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];
Reply
#2

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

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.
Reply
#4

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
Reply
#5

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
Reply
#6

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

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); 
Reply
#8

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?
Reply
#9

@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.
Reply
#10

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.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)