[Crossfire-wiki] [Crossfire DokuWiki] page changed: user:mhoram:code:bwp
no-reply_wiki at metalforge.org
no-reply_wiki at metalforge.org
Wed Jan 17 11:54:12 CST 2007
A page in your DokuWiki was added or changed. Here are the details:
Date : 2007/01/17 11:54
User : mhoram
Edit Summary: thanks for the help
@@ -1,15 +1,34 @@
====== Build Wiki Pages ======
This is a program I'm working on to automate the building of pages for the wiki from the monster archetypes (and eventually other categories, perhaps). I'm still working on it, but I thought I'd put it here in case anyone wanted to help me fix a problem I'm having, or critique my code. (I'm still very much an amateur at writing C.)
- The main problem I'm having right now is that a couple variables are getting clobbered; I assume by bad memory allocation. It happens at line 502: the variables *key[] and *val[] are fine before that call to join_with_comma(), but after it they are messed up. (Search for BEFORE and AFTER.) Since they aren't involved in that function at all, I assume that something in that function is causing an overflow. There's probably a way with gdb or some other tool to see what variable is using a particular memory address, but I don't know what it is.
+ The main problem I'm having right now is that a couple variables are getting clobbered; I assume by bad memory allocation. It happens at line 502: the variables ''*key[]'' and ''*val[]'' are fine before that call to ''join_with_comma(),'' but after it they are messed up. (Search for ''BEFORE'' and ''AFTER''.) Since they aren't involved in that function at all, I assume that something in that function is causing an overflow. There's probably a way with gdb or some other tool to see what variable is using a particular memory address, but I don't know what it is.
Also, if anyone knows of a good C primer/tutorial online, please let me know.
* In the sortbyname, a and b will be const char* *, not char*, thus you need to dereference them => ''strcpy(aa,(*(const char* *)a));'' or something like that :)
* I've stared at this for a while, and found some examples that do it, and I think I'm starting to understand it. I still don't quite see how the arguments can be char* *, when the function defines them as const void *, but I'll get there.
- * I changed it to the example you gave, and now it segfaults on that strcpy. ;-) I think I'm on the right track now, though. It makes sense that the problem was in that comparison routine, since I checked the addresses of the variables within join_with_comma, and none of them were even close to the ones that were getting mangled.
+ * I changed it to the example you gave, and now it segfaults on that ''strcpy''. ;-) I think I'm on the right track now, though. It makes sense that the problem was in that comparison routine, since I checked the addresses of the variables within ''join_with_comma'', and none of them were even close to the ones that were getting mangled.
+ * Ok, I've updated the code below to reflect those changes, but I'm still having trouble in ''sortbyname''. All the examples I can find online do it like you suggested, or something similar, but when I do that, I get strange results. Here's what gdb says about the first argument:
+
+ <code>
+ Breakpoint 1, sortbyname (a=0xbfbbffd0, b=0xbfbc09d0) at bwp.c:259
+ 259 return strcasecmp( *(char **)a, *(char **)b );
+ (gdb) p a
+ $1 = (const void *) 0xbfbbffd0
+ (gdb) p (char **)a
+ $2 = (char **) 0xbfbbffd0
+ (gdb) p (char *)a
+ $3 = 0xbfbbffd0 "Gnarg"
+ (gdb) p *(char *)a
+ $4 = 71 'G'
+ (gdb) p *(char **)a
+ $5 = 0x72616e47 <Error reading address 0x72616e47: Bad address>
+ </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.
+
* Also, to compare, try ''strcasecmp'' which does case-insensitive comparison.
* That would be much easier, wouldn't it? :-)
IP-Address : 206.71.197.56
Old Revision: http://wiki.metalforge.net/doku.php/user:mhoram:code:bwp?rev=1169055870
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