Re: [Circle] [code][boggle][oops..wrong button]

From: Sammy (
Date: 11/15/96

On Fri, 15 Nov 1996, invincibill wrote:

- dammit...sorry about that.
-     if (GET_LEVEL(tch) >= LVL_IMMORT)
-       switch (GET_LEVEL(tch))
-          {
-            case 100: lvl_cl="Immort";break;
-            case 101: lvl_cl="Demi";break;
-            case 102: lvl_cl="Diety";break;
-            case 103: lvl_cl="LGod";break;
-            case 104: lvl_cl="Hlpr";break;
-            case 105: lvl_cl="MGod";break;
-            case 106: lvl_cl="HCdr";break;
-            case 107: lvl_cl="HBld";break;
-            case 108: lvl_cl="Ambass";break;
-            case 109: lvl_cl="HGod";break;
-            case 110: lvl_cl="HMFIC";break; 
-          }
- ** the above works great....**
-     else
-     {
-         send_to_char("made it to the sprintf",ch);
-         sprintf(lvl_cl, "%2d %s",GET_LEVEL(tch), CLASS_ABBR(tch));
- **        ^ this is causing a crash...
- **  when i comment it out, i get the "made it..." message.
-     }
- any help here...?  or slaps in the face(either would work equally well)

*slap* ;)

The problem is lvl_cl is declared as a pointer to a character, not an
array of characters (a "string").  I think it's ok to define it as a
pointer and also to set it equal to "HMFIC" (though that may have to be
&("HMFIC)), but the problem is that "HMFIC" is compiled as a 6-byte
constant character array (1 byte for the null terminator).  So when you
use the sprintf to write to lvl_cl, you're putting a long string where
you've only allocated memory for a short string.

I should mention here that send_to_char is normally useless for debugging
segfaults, because it doesn't immediately send the text to the character
as it seems to imply.  All it does is write the string to your output
buffer, so it can be written to your socket later.  So you'll only see
your debug message if the mud doesn't crash.  A printf will send text to
the terminal you started the mud on, and I think it works immediately
(can't remember).  GDB is always the best way to debug these tho :)

To fix the problem, you can declare lvl_cl with a size (char lvl_cl[2048],
and use strcpy(lvl_cl, "HMFCI") to put text in it, or use your sprintf's.


| Ensure that you have read the CircleMUD Mailing List FAQ: |
|   |

This archive was generated by hypermail 2b30 : 12/18/00 PST