[Crossfire-wiki] [Crossfire DokuWiki] page changed: user:mhoram:code:bwp

no-reply_wiki at metalforge.org no-reply_wiki at metalforge.org
Thu Jan 18 02:11:54 CST 2007


A page in your DokuWiki was added or changed. Here are the details:

Date        : 2007/01/18 02:11
User        : ryo
Edit Summary: 

@@ -27,21 +27,22 @@
  </code>
  
  The way I read that, it looks like what I really want to pass to ''strcasecmp'' is a pointer to ''(char *)a'', but that doesn't work either.  (You can see that attempt commented out.)  I think for today I'm just going to comment out the ''qsort'' call so I can finish the rest of the program; in the meantime, any further hints on what I'm doing wrong would be much appreciated.
  
+   * You need to do ''qsort(array->item, array->count, sizeof(char*), sortbyname);'', not ''qsort(array->item, array->count, array->longest, sortbyname);''. The item size is a char*, not the longest element. ''array->item'' is an array of ''char*'', that is of pointers to memory zones containing ''char'', not an array of items of size ''array->longest'' :) This can certainly explain some weird things.
  
    * Also, to compare, try ''strcasecmp'' which does case-insensitive comparison.
      * That would be much easier, wouldn't it? :-)
  
    * ''append_with_comma'' (not used, but just in case) has a side effect: old will be destroyed if not NULL - the ''realloc'' can make the original pointer (which still exists probably) point to a freed memory.
      * That function got replaced by join_with_comma and will be deleted anyway, but I should understand this.  Isn't the whole point of realloc that it leaves the data pointed to unchanged?
+       * ''realloc'' will leave the **contents** of the zone unchanged. But if you ask for a bigger memory zone, it will allocate a new block somewhere else, and copy the initial contents. Thus the original pointer can become invalid.
  
    * and some day don't forget to ''free()'' allocated memory (for the char* and such) ^_-
      * Yes, I figured I'd need to do that, but I'm still learning just what I have to free.  Anything I allocated manually, I assume, but I get a little hazy on it when something was allocated in a subroutine.
  
  Thanks for all your help!
- 
- 
+   * no problem, thanks for the program :)
  
  
  ===== Code =====
  <code c>


IP-Address  : 196.3.55.241
Old Revision: http://wiki.metalforge.net/doku.php/user:mhoram:code:bwp?rev=1169056449
New Revision: http://wiki.metalforge.net/doku.php/user:mhoram:code:bwp

-- 
This mail was generated by DokuWiki at
http://wiki.metalforge.net/




More information about the crossfire-wiki mailing list