Problem with Dynamically allocated structures

[email protected]
Newsgroups gmane.games.devel.mud.rom
Message-ID <[email protected]>

Hello, I am trying to write one of these but I am having problems. I was able to
write one in Dev C++ doing the same thing as what I can tell, in my MUD. It
works fine in Dev C++ but it doesn't seem to be working correctly in the game.

Here is what I have in Dev C++:
#include <stdio.h>
#include <stdlib.h>

void fill_table();

#define MAX_SIZE = 3

struct type
{
        char *name;
        int length;
};

struct type *table_type;

struct type *table_type =
{
};

int main(int argc, char *argv[])
{
        int i;
        fill_table();

        printf("Name: %s. Length: %d\n\r", table_type[0].name,
table_type[0].length);
        printf("Name: %s. Length: %d\n\r", table_type[1].name,
table_type[1].length);
        printf("Name: %s. Length: %d\n\r", table_type[2].name,
table_type[2].length);


        return 0;
}

void fill_table()
{
        table_type = malloc(sizeof(struct type) * 3);
        table_type[0].name = "TEST";
        table_type[0].length = 10;
        table_type[1].name = "TEST2";
        table_type[1].length = 102;
        table_type[2].name = "TEST3";
        table_type[2].length = 103;
}

This works just fine when I execute it.
Here is the output:
Name: TEST. Length: 10
Name: TEST2. Length: 102
Name: TEST3. Length: 103
This is what I have in my game:
Merc.h:
struct  spells_type
{
    char *      name;
    sh_int      length;
};
struct  spells_type     *spells_table;

Const.c:
struct          spells_type             *spells_table =
{
};

db.c:
fill_table();

act_wiz.c:
(These will be moved after I get it working and is only for testing)
void do_test(CHAR_DATA *ch, char *argument)
{


        //spells_table[0].name = "TEST";
        //spells_table[0].length = 10;



        sc(ch, "Spell 1: Name: %s. Delay: %d.\n\r", spells_table[0].name,
spells_table[0].length);
        sc(ch, "Spell 2: Name: %s. Delay: %d.\n\r", spells_table[1].name,
spells_table[1].length);
        sc(ch, "Spell 3: Name: %s. Delay: %d.\n\r", spells_table[2].name,
spells_table[2].length);
        sc(ch, "Size of the table: %d\n\r", sizeof(struct spells_type));
}

void fill_table()
{
        int num = 3;
        spells_table = malloc(sizeof(struct spells_type) * num );

        spells_table[0].name = "TEST";
        spells_table[0].length = 10;
        spells_table[1].name = "TEST2";
        spells_table[1].length = 102;
        spells_table[2].name = "TEST3";
        spells_table[2].length = 103;

}

I do a clean compile and then copyover, then, I type do_test:
>test
Spell 1: Name: (null). Delay: 0.
Spell 2: Name: (null). Delay: 0.
Spell 3: Name: (null). Delay: 0.
Size of the table: 8

However, when I uncomment the few lines of code in do_test, this happens:
>test
Spell 1: Name: TEST. Delay: 10.
Spell 2: Name: (null). Delay: 0.
Spell 3: Name: (null). Delay: 0.
Size of the table: 8


I am not entirely sure what is wrong since I am doing the same exact thing, from
what I can tell, and it doesn't work.
I have no idea what is wrong and why this isn't working.
It looks like the assigning of the data in the table isn't working correctly. Do
I have to do something else since it is spread out amongest many files?

Thank you for any help that you can provide!
James

----------------------------------------------------------------
This message was sent using IMP, the Internet Messaging Program.
-- 
ROM mailing list
[email protected]
Unsubscribe here ->>> http://www.rom.org/cgi-bin/mailman/listinfo/rom
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.