AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Run time error 3 - Stack Error - is there a pragma I need? (https://forums.alliedmods.net/showthread.php?t=28025)

Geesu 05-05-2006 13:44

Run time error 3 - Stack Error - is there a pragma I need?
 
In war3ft this line is giving me a stack error:

new szEachLine[150][128];

Is there a pragma I can use so my plugin is given more memory? I'm salty...

TY,
Josh

BAILOPAN 05-05-2006 13:45

Please tell me that line isn't in a function.

I don't want to have to hurt you.

Geesu 05-05-2006 13:57

haha yea its in a function.. is that the problem?

Code:
public WC3_EnableModule( module_name[] ) {     new const szModulesINI[] = "addons/amxmodx/configs/modules.ini";         // Assuming a max of 400 lines and each line is less than 256 characters... if not... TOO BAD     new szEachLine[150][128];     new line = 0, len, iTotalLines = 0;     if ( file_exists( szModulesINI ) )     {         while ( ((line = read_file( szModulesINI, line, szEachLine[iTotalLines++], 31, len)) != 0) && iTotalLines < 150 ){}     }     // Now we have every line of modules.ini in our szEachLine variable, w00t     // Check out each line please     for ( new i = 0; i < iTotalLines; i++ )     {         // Module Name Found         if ( contain( szEachLine[i], module_name ) != -1 )         {             // Semicolon before module name found             if ( contain( szEachLine[i], ";" ) != -1 )             {                 new len = strlen( szEachLine[i] ), pos = 0;                 new szTemp[128];                 for ( new j = 1; j < len; j++ )                 {                     pos += ( szTemp[pos], 127 - pos, "%c", szEachLine[i][j] );                 }                 copy( szEachLine[i], 127, szTemp );             }         }     }     // Check out each line please     for ( new i = 0; i < iTotalLines; i++ )     {         server_print( szEachLine[i] );     } }

BAILOPAN 05-05-2006 14:03

not only is there no need to waste memory, you should use the 'static' keyword on large local variables.

Greenberet 05-05-2006 14:05

Code:
public WC3_EnableModule( module_name[] ) {     // you should make them static or global     new const szModulesINI[] = "addons/amxmodx/configs/modules.ini";     new const szTempINI[] = "addons/amxmodx/configs/modules_temp.ini";     static szLine[256];     //******************************************************************************     new rFile = fopen( szModulesINI, "r" );     if( !rFile )         return 0;             new wFile = fopen( szTempINI, "w" );     if( !wFile )         return 0;         //******************************************************************************     new pos;     while( !feof( rFile ) )     {         fgets( rFile, szLine, 255 );         pos = 0;         if( containi( szLine, module_name ) != -1 && szLine[0] == ';')             pos = 1;             fputs( wFile, szLine[pos] );     }     //******************************************************************************     fclose( rFile );     fclose( wFile );         delete_file( szModulesINI );         return rename_file( szTempINI, szModulesINI )}

compiled fine with 0 errors and 0 warnings.
not tested, but should work

edit:
i had an mistake in the code *fixed*

PM 05-05-2006 14:09

You see, that's a lot of memory. 150*128*4 = 76800 = 75 kB on the stack... Should be avoided when possible (although the compiler _should_ have computed the estimated stack size correctly in this case I think.. hmm.)

Instead, you could only hold a buffer for one line and process them on the fly ( in the body of the loop which has read_file in its condition ). This way you also wouldn't need to re-evaluate the length of each line (read_file fills it in the last parameter). I was also a bit surprised that you reserved a 128 byte buffer for each line and then told read_file to return 31 characters at max. You may want to use a constant for the line buffer length so you don't have to change it in two places (but then do constant+1 in the buffer declaration).

You may also want to try using the new file natives.
Also, the // Semicolon before module name found comment was misleading I'm afraid.

Well, my thoughts.

Urahara 05-05-2006 14:12

Ohayo Geesu-chan! :D

There are many better ways to do this! :D

first off you should start by not using a predefined path to the module.ini; using the stocks provided by amxx is much cleaner and portable!

second off this could easily be accomplished with a simple read and if statement instead of thatconvoluted mess ill post an example later from mai pluginz

Geesu 05-05-2006 14:17

OK I have it working fine now...

Suggestions? Besides using the amxx natives to get the filename :P

Code:
// Assuming a max of 150 lines and each line is less than 256 characters... if not... TOO BAD new szEachLine[150][256]; public WC3_EnableModule( module_name[] ) {     new const szModulesINI[] = "addons/amxmodx/configs/modules.ini";     new line = 0, len, iTotalLines = 0;     if ( file_exists( szModulesINI ) )     {         while ( ((line = read_file( szModulesINI, line, szEachLine[iTotalLines++], 255, len)) != 0) && iTotalLines < 150 ){}     }     // Now we have every line of modules.ini in our szEachLine variable, w00t     // Check out each line please     for ( new i = 0; i < iTotalLines; i++ )     {         // Module Name Found         if ( contain( szEachLine[i], module_name ) != -1 )         {             // Semicolon before module name found             if ( contain( szEachLine[i], ";" ) != -1 )             {                 new len = strlen( szEachLine[i] ), pos = 0;                 new szTemp[256];                 for ( new j = 1; j < len; j++ )                 {                     add( szTemp, 255, szEachLine[i][j], 1 );                     //pos += ( szTemp[pos], 255 - pos, "%c", szEachLine[i][j] );                 }                 copy( szEachLine[i], 255, szTemp );             }         }     }     unlink( "addons/amxmodx/configs/modules.ini" );     // Check out each line please     for ( new i = 0; i < iTotalLines; i++ )     {         write_file( "addons/amxmodx/configs/modules.ini", szEachLine[i] );     } }

PM 05-05-2006 14:51

lol, sometimes i shout but no one seems to hear...

My suggestions are still the same.

Specifically, don't use that much memory. Do one line at a time.
Read, process, write. This is even less code because write_file overwrites the line you tell it to overwrite.

Unless I am missing something crucial, there's really no need to waste 75 kB of memory on this. And if I am, you could at least be so kind and point it out to me.

Thank you.

Geesu 05-05-2006 15:08

PM are u referring to use like fopen/fseek/fputc?

I tried this initially, but I wasn't able to successfully do it, so I switched to this way...

O shit I think I just realized it.. since u specify the line number in read_file/write_file...

LOL yea ur right, I am retarded, thanks :)


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

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