swarm-modeling
[Top][All Lists]
Advanced

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

Re: a C usage I can't understand & "class variables"


From: Ralf Stephan
Subject: Re: a C usage I can't understand & "class variables"
Date: Thu, 25 May 2000 16:43:41 +0200

Paul: 
> In Agent.h, there is also a instance variable which is a pointer to a
> Params struct.
>       struct Params *p;
> 
> Then in Agent.m, there are two of these global variables declared at the
> top.
> static struct Params * params;
> static struct Params * pp;
> 
> 
> When the Agent is initialized, there is a calloc for params, as in
> 
>    params = (struct Params *) malloc(sizeof(struct Params));
> 
> Then in later instance methods, the other global pointer is aimed at
> that same thing:
>  pp = params;
> 
> And later the instance variable pointer is aimed at the same thing:
>  p = params;
> 
> 
> Here is what I don't understand. Can't I just delete p and pp and make
> all references of them use params instead? 

Yes, but I smell rats... when is the block freed?  Hopefully not by
more than one Agent!  But you say 'the Agent' so there seems to be but
one.  In this case, yes, two of the three handles aren't needed (not
even for optimization reasons).

> Could
> there be any good reason to declare p, pp, and params when they all end
> up pointing to the same thing?

E.g. if you use one of them as a running pointer to an array of structs
in a loop --- but the malloc above shows no such intention --- or the
p and pp are changed at some time in another way.


ralf
-- 
http://ME.IN-berlin.de/~rws/


                  ==================================
   Swarm-Modelling is for discussion of Simulation and Modelling techniques
   esp. using 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]