[Tutorial] How to use #pragma
#1

How to use #pragma

Today, I will teach you how to use #pragma. A useful directive which will fix a wide array of compiler warnings.

Usage

i lied

#pragma is a directive used to change compiler settings. It is not a warning fixer.

I always see people advising others to use #pragma to remove warnings from their script. The warnings are only silenced - they do not magically disappear.

Instead, you will learn how to avoid using it. Warnings should never be silenced - they should be fixed the correct way!

#pragma tabsize
This sets the tab size, which is set to 4 by default. When you set it to zero, you are basically allowing for unreadable code in your script.

You should never have to set the tab size. Why do you want to set it anyway? Learn to indent your code correctly, and you won't have a problem.

#pragma dynamic
This code:

pawn Код:
#pragma dynamic 1000000
Dynamically sets the size of the stack. Without it, you may receive something along the lines of:

Код:
Pawn compiler 3.2.3664	 	 	Copyright © 1997-2006, ITB CompuPhase

Header size:          19036 bytes
Code size:          1327124 bytes
Data size:         43915204 bytes
Stack/heap size:      16384 bytes; estimated max. usage: unknown, due to recursion
Total requirements:45277748 bytes
This warning is caused by sparse arrays, unnecessary string sizes, and using up a lot of data.

Ask yourself this: do you really need to use all of that space? Why are there 2,000 slots allocated for HouseInfo, when you only use 100 slots? There are many ways to reduce data usage.

pawn Код:
#define MAX_HOUSES      1000
#define MAX_FURNITURE   100 // Per house.

new FurnitureInfo[MAX_HOUSES][MAX_FURNITURE][e_FURNITURE_DATA];
That array is 400 kilobytes big, not including enumerator data. That is nothing compared to today's standards, but it still matters, and there is a way to reduce it.

Let's say we have an average of 40 items of furniture per house. We can easily reduce it to:

pawn Код:
new FurnitureInfo[MAX_HOUSES * 40][e_FURNITURE_DATA];
Of course, not every house will contain 40 items of furniture. The size of this array is now 40 kilobytes, as opposed to the 400 kilobytes from the other one.

Conclusion
TL;DR - Don't use #pragma to fix compiler warnings.

Other versions of Pawno use different compiler settings. SA-MP uses its own settings, so it's important that you adapt to it.

In case you didn't notice the underlined links in this post, you can read my tutorials about packed strings and also sparse arrays
Reply
#2

Great job!
Reply
#3

I don't know if can understand French, but if you can : https://sampforum.blast.hk/showthread.php?tid=534218

#pragma isn't only made to hide warnings. It can also be very useful to do other things (such as ctrlchar, pack, codepage or even amxram).

Finally, the debug messages aren't really warnings but, as I stated, debug messages. I'm sure I teach you nothing here in my whole post - especially here - but you can enable this kind of messages at each compilation you process by compiling in -d2 or -d3 (using pawno, it's by creating the pawn.cfg file and writing down the requested compilation options).
Reply
#4

Yea, that's why I mentioned this in my post:

"#pragma is a directive used to change compiler settings. It is not a warning fixer.".

The point of this tutorial is to teach newbies to not use #pragma to hide compiler warnings. Your tutorial looks nicely done, I might translate it and give it a read soon.
Reply
#5

Well, a real newbie can't be aware of #pragma unless a more (but not that much) experienced member tells him about it. The real solution is to tell to everyone that #pragma isn't a warning solver.

And thanks !
Reply
#6

Quote:

Without it, you may receive something along the lines of:

Код:
Pawn compiler 3.2.3664	 	 	Copyright © 1997-2006, ITB CompuPhase

Header size:          19036 bytes
Code size:          1327124 bytes
Data size:         43915204 bytes
Stack/heap size:      16384 bytes; estimated max. usage: unknown, due to recursion
Total requirements:45277748 bytes
So in other words, once compiling and getting such info means the script uses more cells than it should have?
Mine:
Код:
Pawn compiler 3.2.3664	 	 	Copyright © 1997-2006, ITB CompuPhase

Header size:          17156 bytes
Code size:          1388312 bytes
Data size:          2764968 bytes
Stack/heap size:      16384 bytes; estimated max. usage: unknown, due to recursion
Total requirements: 4186820 bytes
[Finished in 10.1s]
Reply
#7

Seems like you invoke the compiler externally. Compiling within Pawno does not yield the "finished in" message. If the compiler is invoked with the -v or -d3 options that message will always appear, regardless.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)