Array Size
I've always been wondering if something like this is possible, and if it is, is it allowed to use?
PHP Code:
|
Re: Array Size
Yea, it's possible, because:
First you create an array which let say will hold 33 cells ( new szCommands[33] ) Then, you create a new array which will have the value sizeof(szCommands) which will be 33 cells lenght, so practically this is same as new szTemp[33], but less hardcoded, if it's value depends on szCommands lenght. |
Re: Array Size
Quote:
PHP Code:
PHP Code:
|
Re: Array Size
Quote:
Make a little plugin with only the code you are testing and try it. |
Re: Array Size
I might have gotten a bit excited here... Sorry.
It works because sizeof is not a runtime function but a compiler function. So when the plugin is compiled it will be calculated and replaced with a hard value while the core code remains flexible. However you cannot use it to create another variable that will dependant on the second dimension lenght because it is not defined, or more exactly the compiler won't be bothered to actually calculate it for you because it is not the same for all the array entries. Code:
Code:
0: tArrays are illusions to increase the syntax readability and making it easier to work with data. It's basically memory in a row with pointers on where to start reading. And since pointers are of a defined lenght they could be kept in an array in exactly the same way but they won't need pointers to be accessed. They will always be kept in the same place (pointer position = cell lenght * index number). If I would to mess up the data you can see that it will keep on reading the next entry as well since the function using it is searching the memory for NULL to stop reading it. This is how all string functions work in PAWN. All other forms of data have a defined lenght in some form. Code:
Code:
0: test1In the end, this is why PAWN is such an easy language to learn and use. EVERYTHING is either 1 cell (32 bits) or an array of multipe cells. All the tags (Float:, bool:, et.c.) doesn't actually change the contents. They are just there to help the compiler point out when memory is used in an incorrect way because the memory is read differently depending on what it contains or how you decide to read it. Code:
Code:
1Your code will also not work because it's missing a dimension. Either way it's completely irrelevant in this case. There's no need to create several variables for a temporary usage. Code:
|
Re: Array Size
Quote:
@Black Rose; Thanks for the info, do you have any examples where it could be used to be efficient? |
Re: Array Size
Quote:
Or if you want to declutter the plugin_init() a bit and also include say_team: Code:
|
Re: Array Size
Thanks for clearing things up @Black Rose, makes a lot more sense now. This will defenitly help me and others in the future.
|
| All times are GMT -4. The time now is 14:13. |
Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.