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

[TF2] 2D char array limits for SourceMod


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Haytil
Junior Member
Join Date: Oct 2018
Old 10-22-2018 , 01:03   [TF2] 2D char array limits for SourceMod
Reply With Quote #1

Hello,

What is the limit on 2D char arrays (arrays of strings) in sourcemod? How can this limit be changed?

I found that declaring a static array of 2000 16-char strings (~32 KB) causes run-time failure, but 1000 16-char strings works (~16 KB). This seems odd to me - 32 KB is really not very much.

Example code:

Plugin 1 (compiles and works):

PHP Code:

#include <sourcemod>

public Plugin myinfo =
{
    
name "Array_Test_1",
    
author "Haytil",
    
description "Demonstrates success/failure of large 2D arrays..",
    
version "1.0",
    
url "http://www.sourcemod.net"
};

public 
void OnPluginStart()
{
    
PrintToServer("Starting Array Test 1 plugin...");

    
RegAdminCmd("test_command"Test_CommandADMFLAG_KICK"Tests calling a sub-function which declares a 2D array.");
}

public 
void Update_Database(int i_Clientint i_Output_Level)
{
    
char temp_ids[1000][16];
}

public 
Action Test_Command(int clientint args)
{
    
Update_Database(client1);

    return 
Plugin_Handled;


Plugin 2 (compiles but does NOT work):

PHP Code:

#include <sourcemod>

public Plugin myinfo =
{
    
name "Array_Test_2",
    
author "Haytil",
    
description "Demonstrates success/failure of large 2D arrays..",
    
version "1.0",
    
url "http://www.sourcemod.net"
};

public 
void OnPluginStart()
{
    
PrintToServer("Starting Array Test 2 plugin...");

    
RegAdminCmd("test_command"Test_CommandADMFLAG_KICK"Tests calling a sub-function which declares a 2D array.");
}

public 
void Update_Database(int i_Clientint i_Output_Level)
{
    
char temp_ids[2000][16];
}

public 
Action Test_Command(int clientint args)
{
    
Update_Database(client1);

    return 
Plugin_Handled;

When I run the above plugin, I get the following error in console:

[SM] Exception reported: Instruction contained invalid parameter



How can I avoid this, if I want to handle a large number of strings?
Haytil is offline
Fyren
FyrenFyrenFyrenFyrenFyren FyrenFyrenFyrenFyrenFyren FyrenFyrenFyrenFyrenFyren FyrenFyrenFyrenFyrenFyren
Join Date: Feb 2106
Old 10-22-2018 , 04:32   Re: [TF2] 2D char array limits for SourceMod
Reply With Quote #2

The error reporting is really poor here (and also late). The problem is actually that plugins have a stack/heap size of 16k by default; the compiler output says what it is. The code that generates the local array trips a check involving it. We can probably have this error on compile with a better message.

I think you can either make the array global or use #pragma dynamic <size> to set a higher size.
Fyren is offline
klippy
AlliedModders Donor
Join Date: May 2013
Location: Serbia
Old 10-22-2018 , 05:01   Re: [TF2] 2D char array limits for SourceMod
Reply With Quote #3

Won't making it static work just as fine?
__________________
klippy is offline
Fyren
FyrenFyrenFyrenFyrenFyren FyrenFyrenFyrenFyrenFyren FyrenFyrenFyrenFyrenFyren FyrenFyrenFyrenFyrenFyren
Join Date: Feb 2106
Old 10-22-2018 , 06:19   Re: [TF2] 2D char array limits for SourceMod
Reply With Quote #4

Probably. The only difference with global would be scoping.
Fyren is offline
Haytil
Junior Member
Join Date: Oct 2018
Old 10-22-2018 , 13:45   Re: [TF2] 2D char array limits for SourceMod
Reply With Quote #5

Quote:
Originally Posted by Fyren View Post
The error reporting is really poor here (and also late). The problem is actually that plugins have a stack/heap size of 16k by default; the compiler output says what it is. The code that generates the local array trips a check involving it. We can probably have this error on compile with a better message.
Yes, there was literally no error or warning on compile (I used the web compiler), so no reason at all to believe there was a problem until I ran it. Definitely needs a fix.

Quote:
I think you can either make the array global or use #pragma dynamic <size> to set a higher size.
-Would making the array global within the plugin really make a difference? If the heap is limited to 16k per plugin, by default, then the plugin would still be using too much memory, just globally...

-Do you have a link to any documentation about that pragma? I assume <size> is in bytes...
Haytil is offline
Fyren
FyrenFyrenFyrenFyrenFyren FyrenFyrenFyrenFyrenFyren FyrenFyrenFyrenFyrenFyren FyrenFyrenFyrenFyrenFyren
Join Date: Feb 2106
Old 10-22-2018 , 15:08   Re: [TF2] 2D char array limits for SourceMod
Reply With Quote #6

Global arrays are static and don't live in what we call the stack/heap.

There might be some tiny bit of documentation in the upstream Pawn stuff, but I don't know.

The size you give for the pragma is in cells, though I think the compiler output reports it in bytes. There's four bytes to a cell.
Fyren 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 13:23.


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