swarm-support
[Top][All Lists]
Advanced

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

Re: Vector passing


From: Paul E Johnson
Subject: Re: Vector passing
Date: Fri, 18 Aug 2000 09:32:37 -0500

"Marcus G. Daniels" wrote:
> 
> >>>>> "PJ" == Paul E Johnson <address@hidden> writes:
> 
> PJ> - (int *) vectorFunction
> PJ> {
> PJ>   int avector[128];
> PJ>   blah blah;
> PJ>   avector;
> PJ>  //same as &avector[0] , the address of the first argument }
> PJ> }
> 
> If you mean "return avector;", not "avector;", that won't work.  It
> won't work since the stack space for avector will disappear as soon as
> the method returns, and the pointer will be invalid.
  
Sorry, I don't know what I was thinking. I am humbled.

Just to clean up my mess:

- (int * ) vectorFunction
{
        int * avector;
        int NOFSPACES= 128;
        avector = [[self getZone] allocBlock: (NOFSPACES * sizeof(int)) ];
        blah blah;
        return avector; //same as return &avector[0]
}

This allocates memory and so when it is called, keep track of that
memory and drop it when you don't need it, as in

     int * thevector;
     thevector = [self vectorFunction];
     blah doing what it wants to the vector;
     [thevector drop];

(if you use a regular C "malloc" to allocate the memory, use free() to
drop it).

If you already have an instance variable "thevector" then you can do
- (int *) vectorFunction
{
        blah doing what you want to thevector;
        return thevector;
}

Presumably this would be used when you are passing that vector to
another object.

-- 
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]