swarm-modeling
[Top][All Lists]
Advanced

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

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


From: Paul E. Johnson
Subject: a C usage I can't understand & "class variables"
Date: Thu, 25 May 2000 08:57:22 -0500

OK, I learned yesterday that these things I was calling class variables
are really static global variables, and I don't see much difference
between a class variable and something declared as static at the top of
the ".m" file.

Oh, well. 

Here is the question.  This is not about code I wrote, its about code
someone else wrote that I'm trying to understand.

Suppose in the ".h" file one declares a struct, lets call it "Params".

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?  They seem redundant. Could
there be any good reason to declare p, pp, and params when they all end
up pointing to the same thing? It does no good whatsoever to create an
IVAR p that points to the global variable, because then p->x and
params->x have the same effect.  They change something in the global
variable params.  And the variable pp points to the same thing, so any
change caused by p->x or params->x is going to affect what is found by
pp->x.

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