SA-MP Forums [Include] pointers.inc - Pointers for PAWN!
 User Name Remember Me? Password
 New Account Members List Search Today's Posts Mark Forums Read

 Thread Tools Display Modes
18/01/2012, 05:17 PM   #21
Zh3r0
High-roller

Join Date: Mar 2008
Posts: 4,514
Reputation: 398
Re: pointers.inc - Pointers for PAWN!

Quote:
 Originally Posted by Stewie` http://www.cplusplus.com/doc/tutorial/pointers/
Thanks for that, but I still don't understand, considering that I lost interest in learning about coding(overall).
__________________

 18/01/2012, 05:21 PM #22 steki. High-roller     Join Date: Nov 2010 Location: São Paulo, Brasil Posts: 2,557 Reputation: 315 Re: pointers.inc - Pointers for PAWN! Pointers are a type of variable that points to another variable. With that, we can save alot of memory and make our job way easier like: pawn Code: `int make_a_zero(int *var){     *var = 0;     return 0;}main(){     int hey = 1212;     make_a_zero(&hey);}` So hey's value and whatever you put as an argument (int) will become zero. __________________ Rio
18/01/2012, 05:43 PM   #23
CaHbKo
Huge Clucker

Join Date: Nov 2007
Posts: 399
Reputation: 41
Re: pointers.inc - Pointers for PAWN!

Quote:
 Originally Posted by Stewie` Pointers are a type of variable that points to another variable. With that, we can save alot of memory and make our job way easier like: pawn Code: `int make_a_zero(int *var){     *var = 0;     return 0;}main(){     int hey = 1212;     make_a_zero(&hey);}` So hey's value and whatever you put as an argument (int) will become zero.
Wow. So cool. But that example still doesn't explain how pointers are useful in SA-MP, because it can be translated to PAWN like this:
pawn Code:
`stock make_a_zero(&var){     var = 0;     return 0;}main(){     new hey = 1212;     make_a_zero(hey);}`

 18/01/2012, 05:50 PM #24 Slice High-roller   Join Date: Mar 2008 Location: Sweden Posts: 1,835 Reputation: 1607 Re: pointers.inc - Pointers for PAWN! Well the example in the first post is one.. Another thing you could do is save the addresses of variables for later use. For examples on what I mean, see stmt_bind_result_field in SQLite Improved and BindUserVar in users.inc (note that I didn't use this library for that, but it's fully doable with it).
 19/01/2012, 02:37 AM #25 Adoniiz Gangsta   Join Date: Mar 2010 Location: Wormhole Posts: 826 Reputation: 57 Respuesta: pointers.inc - Pointers for PAWN! ¿Really? nice inc Sicle . __________________
19/01/2012, 04:58 AM   #26
Slice
High-roller

Join Date: Mar 2008
Location: Sweden
Posts: 1,835
Reputation: 1607
Re: pointers.inc - Pointers for PAWN!

Quote:
 Originally Posted by ****** There's a fairly major bug in this version. Currently you're doing "idx = idx + 1 & max;" to get the next index to store in. A very common use case is to set up a load of permanent resources (in this case pointers) at the start of the mode (which would result in them being at the start of the array), and then assign and release more temporary pointers as the mode goes. With your current code this will destroy the initial pointers after a while, but I have no idea for a solution (and I suspect you knew this already so I'm not sure why I'm posting).
The only reason I put index in the first place was so it wouldn't get messed up when using multiple pointers in one function call, though I suspect that's not needed when I come to think about it.
Pointer variables hold the absolute address to another variable, not a pointer index.
This, of course, results in a function call each time a pointer is used, which isn't optimal sometimes. I'm thinking there should be a more static way to create pointers as well, but there are some problems that has to be solved before that's possible.

Edit: After some testing, I've now updated the include to a version without these indexes. Memory usage were now reduced a lot and I increased the max index limit to 1024.

PointerByAddress now consists of only 4 instructions!

Last edited by Slice; 19/01/2012 at 06:57 AM.

 01/02/2012, 08:51 PM #27 Double-O-Seven Little Clucker     Join Date: Jan 2012 Posts: 49 Reputation: 35 Re: pointers.inc - Pointers for PAWN! OK, I don't quite get how the write works... pawn Code: `new    g_Test[] = {123, 456, 789};public OnGameModeInit() {    new pointer = GetVariableAddress(g_Test);       // Change the 2nd value    @[pointer][1] = 444;       // Print out the values    printf("%d, %d, %d", @[pointer][0], @[pointer][1], @[pointer][2]);}` You write @[pointer][1] = 444. But how can this (with your definition of @) write 444 g_Test and replace 456? //EDiT: OK, figured out what PointerByAddress does. It just stores the value of the argument into g_aaiPointers, right? And then this is used: @[%1] (PointerByAddress (%1), g_aaiPointers[0][0]) But why the devil is the address stored in g_aaiPointers[0][0] assigned with the actual value when you use @[pointer] = value and not just g_aaiPointers[0][0]? What kind of sorcery is this? //EDiT 2: OK, maybe I don't understand at all what PointerByAddress does... Last edited by Double-O-Seven; 01/02/2012 at 09:27 PM.
 02/02/2012, 06:42 AM #28 Slice High-roller   Join Date: Mar 2008 Location: Sweden Posts: 1,835 Reputation: 1607 Re: pointers.inc - Pointers for PAWN! I would give you a clear explanation as to exactly how and why this works, but that what fun would that be for you!? Let me just give you this hint: pawn Code: `// Simple assignmenttest[2][5] = 123;// This does the same thing as the line above// In fact, this is exactly what the PAWN compiler generates#emit ADDR.pri  test#emit ADD.C     8#emit MOVE.alt #emit LOAD.I    #emit ADD#emit ADD.C     20#emit MOVE.alt#emit CONST.pri 123#emit STOR.I`
 02/02/2012, 07:45 AM #29 Double-O-Seven Little Clucker     Join Date: Jan 2012 Posts: 49 Reputation: 35 Re: pointers.inc - Pointers for PAWN! Tell me only one more thing: Is this natural behaviour which would be except by everyone familiar with assembly or is this about exploiting a bug? Because when I do (something(), var[0][0]) = 123; I expect that var[0][0] is assigned with 123 and not the address it points to. Same with SomeGlobalVar = (somethingelse(), anothervar[0][0]) where SomeGlobalVar should be assigned with anothervar[0][0] and not with the value which anothervar points to. //EDiT: Have you ever thought of make some malloc() and free() functions? This might be greater combined with your pointers.
 02/02/2012, 08:32 AM #30 (LifeStealeR) Big Clucker     Join Date: Jul 2010 Location: Romania Posts: 128 Reputation: 0 Re: pointers.inc - Pointers for PAWN! Awesome work Slice ofc as always __________________

 Thread Tools Display Modes Linear Mode

 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 Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post CHC Scripting Help 2 22/06/2012 05:09 AM Kasura Help Archive 4 20/11/2010 04:35 AM SAMPfan109 General 4 04/03/2010 08:14 PM Michael_Corleone Help Archive 5 21/07/2009 11:05 AM

All times are GMT. The time now is 12:50 AM.

 -- (EN) English -- (RU) Русски SA-MP - Archive - Top