Thread: Class + Memory issues

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Jan 2007
    Posts
    16

    Class + Memory issues

    I have this class, with 3 char-arrays that are initialized like this:

    Code:
    this->name = (char*) malloc(64);
    this->type = (char*) malloc(64);
    this->item_class = (char*) malloc(64);
    After that, I have a constructor, that copies values to these from a file (read until \n), which ALWAYS work perfectly. I am 100% sure this function is not wrong, since it has worked before, and I haven't changed it. This function copies everything to its correct place.

    Now. My problem is:

    I have a function that enchants a weapon (warhammer in this case), and because of that, appends the enchantments name to the weapons name (this->name).

    warhammer <enchantments name>

    These enchantment names are less than 20 characters long. And the weapon names are less than 20 characters long aswell.

    Example:

    I have a warhammer that I enchant, and when it gets some enchants (not all, some enchants work fine) like " of minor striking" or " of minor intellect", somehow my this->item_class gets overwritten and now says ing or lect.
    This shouldnt happen, since I have already allocated memory for up to 64 chars.
    The only thing the enchant function does, is appends the enchantments name to this->name.
    this->item_class remains untouched.

    Why does this happen?

    I make it a rule to allocate ATLEAST 64 bytes of memory for all strings, and I have nothing in my program that takes up more than 15-20 bytes on its own.
    Add enchant and weapon name together and you still only get at most a 40-byte string.

    P.S. this->item_class contains a small string, such as exotic, two-handed or ranged.
    I use strcat to append the enchantments name.
    Last edited by Siphon; 03-29-2007 at 03:12 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Mutex and Shared Memory Segment Questions.
    By MadDog in forum Linux Programming
    Replies: 14
    Last Post: 06-20-2010, 04:04 AM
  2. Creating a database
    By Shamino in forum Game Programming
    Replies: 19
    Last Post: 06-10-2007, 01:09 PM
  3. Need help to build network class
    By weeb0 in forum C++ Programming
    Replies: 0
    Last Post: 02-01-2006, 11:33 AM
  4. Replies: 10
    Last Post: 06-05-2004, 07:40 PM
  5. Memory Issues
    By Padawan in forum C Programming
    Replies: 12
    Last Post: 04-03-2004, 03:10 AM