Raised This Month: $32 Target: $400
 8% 

Dynamic Arrays in SourcePawn


Post New Thread Reply   
 
Thread Tools Display Modes
-Absolute-
Member
Join Date: Mar 2010
Old 08-07-2012 , 19:19   Re: Dynamic Arrays in SourcePawn
Reply With Quote #11

Imo proposal #2 is the best, because it's more logical to me that when I assign an array to another one it should be a different copy, there is no need to have another reference to the same array. (I could just use the array again..)
-Absolute- is offline
RedSword
SourceMod Plugin Approver
Join Date: Mar 2006
Location: Quebec, Canada
Old 08-07-2012 , 19:42   Re: Dynamic Arrays in SourcePawn
Reply With Quote #12

Quote:
Originally Posted by -Absolute- View Post
Imo proposal #2 is the best, because it's more logical to me that when I assign an array to another one it should be a different copy, there is no need to have another reference to the same array. (I could just use the array again..)
Imagine you make a function counting different type of objects and you need a collection with the type that is present the most. You'll need many different dynamic arrays if you don't want to loop through all of them twice. Then you can simply affect an array to the returned (parameter ref) one. You can't modify directly that dynamic array without knowing the count of the different things.

There are scenarios where one's want to not copy an array and want to pass it directly (with only one affectation).

#1 allow to return a reference to a newly created array in a function. In his proposal, he could return (via a reference parameter) the variable "players", WITHOUT having to copy it. It's still possible to manually copy, while the opposite (to pass a reference) is not true (with #2 only) AFAIU.
__________________
My plugins :
Red Maze
Afk Bomb
RAWR (per player/rounds Awp Restrict.)
Kill Assist
Be Medic

You can also Donate if you appreciate my work
RedSword is offline
BAILOPAN
Join Date: Jan 2004
Old 08-07-2012 , 19:43   Re: Dynamic Arrays in SourcePawn
Reply With Quote #13

RedSword: Right, & is already taken. The syntax x[x.length + 1] - i.e. implicit array extension - is slower, and a weaker guarantee because you must define semantics for when x[x.length + 2] is accessed. Thanks for the feedback re: #3 being exotic.
__________________
egg

Last edited by BAILOPAN; 08-07-2012 at 19:44.
BAILOPAN is offline
BAILOPAN
Join Date: Jan 2004
Old 08-07-2012 , 19:49   Re: Dynamic Arrays in SourcePawn
Reply With Quote #14

-Absolute-: Thanks for the feedback. Indeed, I think most copying of arrays actually intends to transfer ownership to something else, or clone it completely. (Proposal #4, which builds on #2, captures this best.)

RedSword: Keep in mind that passing dynamic arrays to functions is still by-reference. The difference is that the reference cannot "escape" the stack. References to dynamic arrays may escape, by being stored in other arrays.
__________________
egg

Last edited by BAILOPAN; 08-07-2012 at 19:49.
BAILOPAN is offline
RedSword
SourceMod Plugin Approver
Join Date: Mar 2006
Location: Quebec, Canada
Old 08-07-2012 , 20:13   Re: Dynamic Arrays in SourcePawn
Reply With Quote #15

Quote:
Originally Posted by BAILOPAN View Post
RedSword: Keep in mind that passing dynamic arrays to functions is still by-reference. The difference is that the reference cannot "escape" the stack. References to dynamic arrays may escape, by being stored in other arrays.
By "cannot 'escape' the stack", do you mean that if a dynamic array with some information can't grow further in the function ? Or do you mean you can't access myArray[1424124] ? (and if so; is it an out-of-bounds exception ? =crash)

And by "References to dynamic arrays may escape, by being stored in other arrays.", do you mean that I can "cut" an array into subarrays... ? (I feel like i'm going off the rails :$) Or that you can do myArray[141241] and not crash ?

i.e. does "escape" = reach out-of-array's-bounds memory ?
__________________
My plugins :
Red Maze
Afk Bomb
RAWR (per player/rounds Awp Restrict.)
Kill Assist
Be Medic

You can also Donate if you appreciate my work
RedSword is offline
11530
Veteran Member
Join Date: Sep 2011
Location: Underworld
Old 08-07-2012 , 20:41   Re: Dynamic Arrays in SourcePawn
Reply With Quote #16

I'd personally prefer passing arrays by value, but the implementation of an address-of operator to make Proposal #1 still possible.
__________________
11530 is offline
BAILOPAN
Join Date: Jan 2004
Old 08-07-2012 , 20:55   Re: Dynamic Arrays in SourcePawn
Reply With Quote #17

RedSword: If you envision memory in two regions: the long-lived region (globals), and the short-lived region (local variables in a function), "escaping" is when a global has a reference into the short-lived region. When something short-lived has escaped, it's very dangerous, because if you read it later it could be invalid.

Allocating global memory is necessary for dynamic arrays, but the existing model of putting fixed arrays on the stack is much, much more efficient. So either we need to guarantee: (1) all references escape, (2) no references escape, or (3) distinguish references which can escape. That's the the heart of the three proposals Note that in (2), even though arrays are passed by reference, the references themselves cannot be stored in globals. Assignment copies the contents instead.
__________________
egg

Last edited by BAILOPAN; 08-07-2012 at 21:00.
BAILOPAN is offline
BAILOPAN
Join Date: Jan 2004
Old 08-07-2012 , 20:58   Re: Dynamic Arrays in SourcePawn
Reply With Quote #18

11530: What do you mean? That wasn't any proposal - we cannot pass arrays by value because that would break every native which takes a string buffer. Arrays must be passed by reference - so the proposals only talk about assignment semantics.
__________________
egg

Last edited by BAILOPAN; 08-07-2012 at 20:59.
BAILOPAN is offline
KyleS
SourceMod Plugin Approver
Join Date: Jul 2009
Location: Segmentation Fault.
Old 08-07-2012 , 22:48   Re: Dynamic Arrays in SourcePawn
Reply With Quote #19

3 seems like the right way forward, but it is a bit different. Don't slay me down for suggesting another format ;)

PHP Code:
public OnMapStart() {
    new 
bool:iCell[*];
    new * 
bool:pCell iCell;
    
    for (new 
16i++) {
        
iCell += [i];
    }
    
    new 
iLen pCell->length;
    
    
pCell[iLen - (iLen 2)] = 7;
    
    for (new 
= (iLen 1); >= 0i--) {
        
PrintNum(pCell->pop());
    }

This seems a bit clearer to me what's going on (Combination of what has been said already), but it's highly possible I'm the only one who feels this way. It's also probably a lot more difficult to implement, with the extra operators / complexity.

Overall though, how would you grab values from the dynamic array? Can you do something like this (Index elements directly)?

PHP Code:
public OnMapStart() {
    new 
iPlayers[*];
    
IgniteArray(iPlayers);
}

stock IgniteArray(iPlayers[*]) {
    for (new 
= (iPlayers.length 1); >= 0i--) {
        
IgniteEntity(iPlayers[i]);
    }

Or can you only pop them off and readd them later?
KyleS is offline
konichiwa
New Member
Join Date: Sep 2010
Location: For those who want to re
Old 08-08-2012 , 00:09   Re: Dynamic Arrays in SourcePawn
Reply With Quote #20

I would like to suggest something similar to Proposal #3 but in a different approach, by introducing a generic reference type instead of limiting them to array only.

Code:
// & as the reference operator
new &g_Players[];
MakePlayerList() {
    new players[];
    for (new i = 1; i <= MaxClients; i++) {
        if (IsClientInGame(i))
            players += [i];
    }
    // players (dynamic array) had to be allocated in heap and garbage collected
    // g_players (references) can be locally scoped
g_Players = players;
}
This would have few interesting implications.

Code:
// foreach loop with in-place update possible
foreach (new i:&p in players) {
    if (IsClientTrolling(i)) {
        KickClient(i)
        p = 0
    }
}
dv would most likely kill me for giving him more questions than answers though.
__________________
For anyone who tries to reach me, I can be found lurking at #sourcemod always.

Dont bother PM me.
konichiwa is offline
Reply



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 Jump


All times are GMT -4. The time now is 04:46.


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