swarm-support
[Top][All Lists]
Advanced

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

Re: a couple of C questions about ASM-objc


From: Paul E. Johnson
Subject: Re: a couple of C questions about ASM-objc
Date: Wed, 17 May 2000 23:21:31 -0500

Thanks for the pointers.

This is just more recreational C computing talk, not actual swarm work,
but it is swarm related because I need to make the sfsm model (the
objective C version of the artificial stock market) work in order to
better understand the Swarm version.

"Marcus G. Daniels" wrote:
> 
> >>>>> "PJ" == Paul E Johnson <address@hidden> writes:
> 
> PJ> 1. The compiler absolutely hates this in the file error.m: FILE
> PJ> *msgfile = stderr;
> 
> On some systems, stderr can be an expression.  For example, Cygwin is
> like this.  With Hurd, the stdio streams variable expressions invoke
> functions.  So that kind of initialization should not be done.

I don't know why it might be valuable to have this capability that sfsm
is aimed at, but how does a person do it in a portable way?  In sfsm,
they do that FILE *msgfile=stderr thing to leave open the later choice
of outputting stuff to a file or to stderr.  After they make that FILE
declaration, then they liberally use "fprintf(msgfile, "blah")".  And if
they later change their mind, and want output into a file, all they need
to do is go into util.m and redefine "msgfile=fopen(filename,w+).

But I don't see a way to fix the code in the sfsm model without just
going through and changing all their usages of 
    
   fprintf( msgfile, " blah");
to
   fprintf( stderr, "blah");

The sfsm code is really hard to understand because it has all kinds of
global variables floating around.  msgfile is one of those global
variables.  I never understood the project-oriented beauty of object
oriented programming until I beheld this... 
 
> Right, change to using getcwd.
> 
I don't understand why sfsm declares all these functions with extern,
though.  In other projects, I have declared functions in header files
without the "extern" tag and the files that import that header are able
to use the functions.  I previously thought that extern was only
necessary when one wanted to share variables across "m" file.

Oh, well, it will probably come to me in my sleep.  That is what helped
me use the getcwd. I kept getting segfaults because I was relying on the
getcwd to malloc some space for me.  The man page says it will do it if
you pass in a null pointer.  But later I did
        pathname=calloc(MAXPATHLEN, sizeof(char));
         getcwd( (char*) pathname, MAXPATHLEN);

then it worked fine.  Eventually I found a manual that said it was
discouraged to rely on getcwd to allocate the memory, but it did not say
why.   

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