Raised This Month: $12 Target: $400
 3% 

[TUT] Packed vs Unpacked Strings


Post New Thread Reply   
 
Thread Tools Display Modes
Exolent[jNr]
Veteran Member
Join Date: Feb 2007
Location: Tennessee
Old 04-23-2009 , 11:25   Re: [TUT] Packed vs Unpacked Strings
Reply With Quote #11

In all the cases I could use packed strings for my large arrays such as MOTDs or menus, it wouldn't work because the functions require unpacked. So my question is this: why use packed strings if its only use is for smaller variables that wouldn't improve memory usage as much as the large arrays?
__________________
No private work or selling mods.
Quote:
Originally Posted by xPaw View Post
I love you exolent!
Exolent[jNr] is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 04-23-2009 , 11:36   Re: [TUT] Packed vs Unpacked Strings
Reply With Quote #12

Edit: Not working. See post http://forums.alliedmods.net/showpos...7&postcount=16

I just found that format is working with packed strings. Can someone please confirm this because I just woke up and didn't have my coffee yet, maybe I'm doing something wrong.

Code:
Result:
Unpacked: This is a test string . Size=1024
Packed: This is a test string . Size=256
PHP Code:
public plugin_init() 
{
    
register_plugin"pack test" "0.1" "bugsy" )
    
    new 
szTest[1024 char];
    
//new szTest[1024];
    
    
formatszTest 1023 ,"This is a test string" )
    
    
server_print("%s . Size=%d" szTest sizeof szTest );

__________________

Last edited by Bugsy; 04-23-2009 at 21:45.
Bugsy is offline
Spunky
Senior Member
Join Date: May 2008
Location: Orlando, Fl.
Old 04-23-2009 , 15:37   Re: [TUT] Packed vs Unpacked Strings
Reply With Quote #13

I get the same results.

Quote:
Packed: Spunky (8)
Unpacked: Spunky (32)
Code:
#include <amxmodx> public plugin_init() {     register_plugin("Packed Strings", "1.0", "Spunky")     register_clcmd("say /stringtest", "cmd_stringtest") } public cmd_stringtest(id, level, cid) {     new szPackedName[32 char]     get_user_name(id, szPackedName, 31)     console_print(id, "Packed: %s (%d)", szPackedName, sizeof szPackedName)     new szUnpackedName[32]     get_user_name(id, szUnpackedName, 31)     console_print(id, "Unpacked: %s (%d)", szUnpackedName, sizeof szUnpackedName)     return PLUGIN_HANDLED }
Spunky is offline
Send a message via AIM to Spunky
Exolent[jNr]
Veteran Member
Join Date: Feb 2007
Location: Tennessee
Old 04-23-2009 , 16:01   Re: [TUT] Packed vs Unpacked Strings
Reply With Quote #14

I did a test myself:

Code:
static motd[2500 char], len; // format MOTD console_print(client, "Sizeof packed MOTD = %i", sizeof(motd)); show_motd(client, motd, title);

Compiled fine and returned 625.
__________________
No private work or selling mods.
Quote:
Originally Posted by xPaw View Post
I love you exolent!
Exolent[jNr] is offline
joaquimandrade
Veteran Member
Join Date: Dec 2008
Location: Portugal
Old 04-23-2009 , 17:19   Re: [TUT] Packed vs Unpacked Strings
Reply With Quote #15

Quote:
Originally Posted by Bugsy View Post
I just found that format is working with packed strings. Can someone please confirm this because I just woke up and didn't have my coffee yet, maybe I'm doing something wrong.

Code:
Result:
Unpacked: This is a test string . Size=1024
Packed: This is a test string . Size=256
PHP Code:
public plugin_init() 
{
    
register_plugin"pack test" "0.1" "bugsy" )
    
    new 
szTest[1024 char];
    
//new szTest[1024];
    
    
formatszTest 1023 ,"This is a test string" )
    
    
server_print("%s . Size=%d" szTest sizeof szTest );


That is not "working with packed string". You are declaring a variable with the "char operator" but you are using it as a normal string. Basically, what you are doing is:

PHP Code:
public plugin_init() 
 {
     
register_plugin"pack test" "0.1" "bugsy" )
     
     new 
szTest[256];
     
//new szTest[1024];
     
     
formatszTest 1023 ,"This is a test string" )
     
     
server_print("%s . Size=%d" szTest sizeof szTest );
 } 


Quote:
Originally Posted by Exolent[jNr] View Post
I did a test myself:

Code:
static motd[2500 char], len;

// format MOTD console_print(client, "Sizeof packed MOTD = %i", sizeof(motd));

show_motd(client, motd, title);



Compiled fine and returned 625.
But it will not work. It will show just the 4th character from each cell.
__________________

Last edited by joaquimandrade; 04-23-2009 at 17:26.
joaquimandrade is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 04-23-2009 , 21:40   Re: [TUT] Packed vs Unpacked Strings
Reply With Quote #16

Quote:
Originally Posted by joaquimandrade View Post
That is not "working with packed string". You are declaring a variable with the "char operator" but you are using it as a normal string. Basically, what you are doing is:

But it will not work. It will show just the 4th character from each cell.
Yes, I realized this literally as I pulled out of my driveway on my way to work shortly after posting; I did not have internet access until now to edit my post. The above code only appeared to work since my code example had such a large buffer so there were enough cells to properly display the string.

What is happening is the array is getting declared with char so that will trim the cell count down to [1024 / 4 = 256]. When passed to format, it is treated as a normal 256 sized buffer [unpacked] and is able to be printed\manipulated. If you trim the buffer down to the actual size and try to format, your server will error.

PHP Code:
public plugin_init() 
 {
     
register_plugin"pack test" "0.1" "bugsy" )
     
     
//Will error the server
     /// new szTest[22 char];
     
     //Will work as normal
     // new szTest[22];
     
     //Will cause the 'g' to be cut off of the word "string"
     
new szTest[84 char];

     
formatszTest sizeof szTest ,"This is a test string" )
     
     
server_print("%s . Size=%d" szTest sizeof szTest );
 } 
__________________

Last edited by Bugsy; 04-23-2009 at 21:46.
Bugsy is offline
Spunky
Senior Member
Join Date: May 2008
Location: Orlando, Fl.
Old 04-23-2009 , 22:16   Re: [TUT] Packed vs Unpacked Strings
Reply With Quote #17

Aww lame. I thought I'd learned an amazing memory saving technique.

Any chance of AMXX natives using packed strings in the future?
Spunky is offline
Send a message via AIM to Spunky
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 04-23-2009 , 22:33   Re: [TUT] Packed vs Unpacked Strings
Reply With Quote #18

Quote:
Originally Posted by Spunky View Post
Aww lame. I thought I'd learned an amazing memory saving technique.

Any chance of AMXX natives using packed strings in the future?
Why can't you use packed strings to save memory? Did you read the entire tutorial?
__________________
Bugsy is offline
Spunky
Senior Member
Join Date: May 2008
Location: Orlando, Fl.
Old 04-24-2009 , 11:53   Re: [TUT] Packed vs Unpacked Strings
Reply With Quote #19

If I understood everything up to this point correctly, it's only practical if you know the exact value you want to assign to the string. For example:

Code:
new szExample[8 char] = !"Spunky"

But if you want to find the value to assign, it won't work the same way:

Code:
new szExample[32 char] get_user_name(id, szExample, 31)

Then it's not really a packed string. So is there a way around that?
Spunky is offline
Send a message via AIM to Spunky
danielkza
AMX Mod X Plugin Approver
Join Date: May 2007
Location: São Paulo - Brasil
Old 04-24-2009 , 12:49   Re: [TUT] Packed vs Unpacked Strings
Reply With Quote #20

Quote:
Originally Posted by Spunky View Post
If I understood everything up to this point correctly, it's only practical if you know the exact value you want to assign to the string. For example:

Code:
new szExample[8 char] = !"Spunky"


But if you want to find the value to assign, it won't work the same way:

Code:
new szExample[32 char] get_user_name(id, szExample, 31)


Then it's not really a packed string. So is there a way around that?
Recoding all AMXX's string natives, but that won't happen any time soon (if at all)
__________________

Community / No support through PM
danielkza 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 05:41.


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