swarm-support
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Return pointer to array: not possible?


From: Paul E. Johnson
Subject: Return pointer to array: not possible?
Date: Thu, 01 Apr 1999 10:35:00 -0600

I'm just fiddling around, really, learning ways to torture C and
Objective-C. But I figured some of you with actual computer training
might save me a bit of trouble.  

Suppose you have a C array of objects, declared like so

id attribute[10];

How can I return that when another agent wants the attribute vector?

The idea here is that 10 SwarmObjects that are of class attribute are
used to keep track of an agent's position.  If I want another agent to
be able to say "tell me your attribute array", I realize C will not
return the array. 

I also understand the pass-by-reference work around in which you design
a method like this:
-getArray: (id[]) attributeArray
{
        for(s=0;s<10;s++)
        attributeArray[s]=attribute[s];
        return self;
}
As long as the agent that is asking has a target array that can hold the
values, that agent can call this with 
        [otherAgent getArray: targetArray];

This does copy the array out of the original object, but as I see it, it
is not great. It requires the computational expense of a loop inside the
getArray method and it also requires the memory expense of keeping a
targetArray inside the agent that is asking for the information.

I figure there has to be a better way, such creating a pointer to the
array, and returning that.  But I can't get it to work, can't find a
clear example in any of the C books I have.

Why do I think its possible?  Mainly, because Swarm has the collection
type Array, which I could use  (have used) to store the attributes, and
then I'm allowed to return Array type objects.  If an Array is just a
pointer to an object, and I can return it, why can't I figure a way to
return a pointer to a C array.  This saves me the overhead of using the
Swarm type Array, you see, and when there are millions of agents, I
think it has significant speed implications.

-- 
Paul E. Johnson                       email: address@hidden
Dept. of Political Science            http://lark.cc.ukans.edu/~pauljohn
University of Kansas                  Office: (785) 864-9086
Lawrence, Kansas 66045                FAX: (785) 864-5700

                  ==================================
   Swarm-Support is for discussion of the technical details of the day
   to day usage of Swarm.  For list administration needs (esp.
   [un]subscribing), please send a message to <address@hidden>
   with "help" in the body of the message.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]