SA-MP Forums

Go Back   SA-MP Forums > SA-MP Scripting and Plugins > Filterscripts > Includes

Reply
 
Thread Tools Display Modes
Old 18/01/2012, 06:17 PM   #21
Zh3r0
High-roller
 
Join Date: Mar 2008
Posts: 4,513
Reputation: 398
Default Re: pointers.inc - Pointers for PAWN!

Quote:
Originally Posted by Stewie` View Post
Thanks for that, but I still don't understand, considering that I lost interest in learning about coding(overall).
__________________
Zh3r0 is offline   Reply With Quote
Old 18/01/2012, 06:21 PM   #22
steki.
High-roller
 
steki.'s Avatar
 
Join Date: Nov 2010
Location: São Paulo, Brasil
Posts: 2,557
Reputation: 315
Default 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
steki. is offline   Reply With Quote
Old 18/01/2012, 06:43 PM   #23
CaHbKo
Huge Clucker
 
Join Date: Nov 2007
Posts: 399
Reputation: 41
Default Re: pointers.inc - Pointers for PAWN!

Quote:
Originally Posted by Stewie` View Post
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);
}
CaHbKo is offline   Reply With Quote
Old 18/01/2012, 06:50 PM   #24
Slice
High-roller
 
Join Date: Mar 2008
Location: Sweden
Posts: 1,831
Reputation: 1607
Default 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).
Slice is offline   Reply With Quote
Old 19/01/2012, 03:37 AM   #25
Adoniiz
Gangsta
 
Join Date: Mar 2010
Location: Wormhole
Posts: 812
Reputation: 57
Default Respuesta: pointers.inc - Pointers for PAWN!

¿Really? nice inc Sicle .
__________________
Adoniiz is offline   Reply With Quote
Old 19/01/2012, 05:58 AM   #26
Slice
High-roller
 
Join Date: Mar 2008
Location: Sweden
Posts: 1,831
Reputation: 1607
Default Re: pointers.inc - Pointers for PAWN!

Quote:
Originally Posted by ****** View Post
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 07:57 AM.
Slice is offline   Reply With Quote
Old 01/02/2012, 09:51 PM   #27
Double-O-Seven
Little Clucker
 
Double-O-Seven's Avatar
 
Join Date: Jan 2012
Posts: 49
Reputation: 35
Default 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 10:27 PM.
Double-O-Seven is offline   Reply With Quote
Old 02/02/2012, 07:42 AM   #28
Slice
High-roller
 
Join Date: Mar 2008
Location: Sweden
Posts: 1,831
Reputation: 1607
Default 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 assignment
test[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
Slice is offline   Reply With Quote
Old 02/02/2012, 08:45 AM   #29
Double-O-Seven
Little Clucker
 
Double-O-Seven's Avatar
 
Join Date: Jan 2012
Posts: 49
Reputation: 35
Default 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.
Double-O-Seven is offline   Reply With Quote
Old 02/02/2012, 09:32 AM   #30
(LifeStealeR)
Big Clucker
 
(LifeStealeR)'s Avatar
 
Join Date: Jul 2010
Location: Romania
Posts: 128
Reputation: 0
Default Re: pointers.inc - Pointers for PAWN!

Awesome work Slice ofc as always
__________________
(LifeStealeR) is offline   Reply With Quote
Reply

Thread Tools
Display Modes

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
New to pawn, trying to convert some C code to pawn CHC Scripting Help 2 22/06/2012 06:09 AM
[HELP] [pawn][/pawn] bbcode for phpbb3 Kasura Help Archive 4 20/11/2010 05:35 AM
getting this tommorow!!!need a few pointers!! SAMPfan109 General 4 04/03/2010 09:14 PM
Pointers to an admin IRC bot Michael_Corleone Help Archive 5 21/07/2009 12:05 PM


All times are GMT. The time now is 02:01 AM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.