Attempt at fixing memory mess.
[progcomp10.git] / src / link / C / agents / c_wash.c
index 9ca4a41..557464e 100644 (file)
@@ -1,5 +1,5 @@
 /*
- *  c_frechie.c
+ *  c_wash.c
  *  c-link-lib
  *
  *  Created by Daniel Axtens on 22/04/10.
    gospel on the number of agents you're going to see. */
 #define NUMBEROFAGENTSGUESS 100
 
+typedef char agentName[MAXAGENTNAMELEN];
+
 /* data for each instance of my agent */
 typedef struct {
        /* The name of the n-th foe who has beaten us */
-       char (*defeatingFoes)[MAXFOENAMELEN];
+       agentName * defeatingFoes;
 
        /* The length of the array, and how far we are along it */
        size_t foesLen;
@@ -50,7 +52,7 @@ int haveLostTo( wash_data * me, char * foeName ) {
     
     /* check every foe we know to have defeated us */
     for (foe=0; foe<me->foesCount; foe++) {
-        if (strncmp( me->defeatingFoes[foe], foeName, MAXFOENAMELEN) == 0) {
+        if (strncmp( me->defeatingFoes[foe], foeName, MAXAGENTNAMELEN) == 0) {
             //debugmsg( "%d\thaveLostTo( %s ) -> Yes\n", me, foeName );
             return 1;
         }
@@ -64,7 +66,7 @@ int haveLostTo( wash_data * me, char * foeName ) {
 void * Initialise( char * myName ) {
        wash_data * me = malloc( sizeof( wash_data ) );
        
-       me->defeatingFoes = calloc( NUMBEROFAGENTSGUESS, sizeof( MAXFOENAMELEN*sizeof(char) ) );
+       me->defeatingFoes = calloc( NUMBEROFAGENTSGUESS, sizeof( agentName ) );
        me->foesLen = NUMBEROFAGENTSGUESS;
     me->foesCount = 0;
        
@@ -149,7 +151,7 @@ void Results( void * this, char * foeName, int isInstigatedByYou,
     
     /* if we've already lost the foe, don't store again */
     for (foe=0; foe<me->foesCount; foe++) {
-        if (strncmp( me->defeatingFoes[foe], foeName, MAXFOENAMELEN ) == 0) {
+        if (strncmp( me->defeatingFoes[foe], foeName, MAXAGENTNAMELEN ) == 0) {
             /* we've found it! */
             return;
         }
@@ -159,11 +161,11 @@ void Results( void * this, char * foeName, int isInstigatedByYou,
     if (me->foesCount==me->foesLen) {
         /* double the array size. This should error check, but doesn't */
         me->defeatingFoes = realloc( me->defeatingFoes, 
-                                            me->foesLen*2*sizeof( MAXFOENAMELEN*sizeof(char) ) );
+                                            me->foesLen*2*sizeof( agentName ) );
         me->foesLen *= 2;
     }
     
-    strncpy( me->defeatingFoes[me->foesCount], foeName, MAXFOENAMELEN );
+    strncpy( me->defeatingFoes[me->foesCount], foeName, MAXAGENTNAMELEN );
     me->foesCount++;
     
     return;
@@ -174,4 +176,4 @@ void Cleanup( void * this ) {
        wash_data * me = (wash_data *) this;
        free(me->defeatingFoes);
        free(me);
-}
\ No newline at end of file
+}

UCC git Repository :: git.ucc.asn.au