Problem with creating struct object dynamically

Posted By: pegamode

Problem with creating struct object dynamically - 07/31/08 09:45

Hi everybody,

I'm using the following code:

---------------

typedef struct {
ENTITY* entity;
STRING* description;
int isUsable;
STRING* function_use;
int isTakeable;
STRING* function_take;
STRING* canNotBeTakenOrUsedBy;
int canBeCombined;
ENTITY* canBeCombinedWith;
int isPushable;
STRING* function_push;
int isPullable;
STRING* function_pull;
int canBeOpenedOrClosed;
int canBeTalkedTo;
int dialogueID;
STRING* canBeGivenTo;
} USEABLE_OBJECT;

var fhandle;
var tmpObj;

void filereader() {
fhandle = file_open_read("..\\database\\outside.room");
if (fhandle != 0) {
var tempPointer = 0;
str_cpy(delimit_str,";");
STRING* objName = "";
STRING* str = "";
STRING* strTemp = "";
while (file_str_read(fhandle,objName) != -1) {
tmpObj = (USEABLE_OBJECT*)malloc(sizeof(USEABLE_OBJECT));
file_str_read(fhandle,str);
str_cpy(strTemp,str);
((USEABLE_OBJECT*)tmpObj).description = strTemp;
addToGSHashmap(_chr(objName), ((USEABLE_OBJECT*)tmpObj));
file_str_read(fhandle,str);
}
}
file_close(fhandle);
}

---------------

The command addToGSHashmap comes from a DLL I wrote for using a Hashmap in GS.
The content of my file outside.room is:

---------------

trashbin;Dies ist ein Mülleimer;*
fence;Das ist einfach nur ein Zaun. Nicht mehr, nicht weniger;*
test1;Das ist einfach nur ein Zaun. Nicht mehr, nicht weniger;*
test2;Das ist einfach nur ein Zaun. Nicht mehr, nicht weniger;*

---------------

After execution of filereader() my GSHashmap has a size of 4.
So far so good.

When I call my dll function getFromGSHashmap("trashbin"); it returns a pointer to my USEABLE_OBJECT struct, but it's not the one I expect but as it seems it is the last one created in the while-loop.

Some tests showed that it doesn't seem to be a problem in my dll, but that in my while-loop always the same struct is used, overwritten and then the pointer is put into my GSHashmap so that I have always the same pointer in my map.

Can someone help me???

Best regards,
Pegamode.
Posted By: pegamode

Re: Problem with creating struct object dynamically - 07/31/08 10:00

Sorry, I put it in the wrong forum.

Should be in Lite-C Programming ... maybe one of the moderators can move it ???
Posted By: pegamode

Re: Problem with creating struct object dynamically - 07/31/08 18:38

No one any idea ???
Posted By: VeT

Re: Problem with creating struct object dynamically - 07/31/08 19:04

some time ago somebody give me this example
Code:
typedef struct
{
long body;
char* name;
}PILOT;
 

typedef struct
{
PILOT* driver;
char* name;
}ROACH;
 
PILOT* ivan = 
{
 name = "Ivan";
}
 
ROACH* ignat = 
{
 name = "Ignat";
 driver = ivan;
}
 
void test_structs_startup()
{
 ivan.body = ignat;
 
 wait(-2);
 ROACH* temp_roach;
 temp_roach = (ROACH*)ivan.body;
 error(temp_roach.name);
}


maybe it can be usefull...
Posted By: pegamode

Re: Problem with creating struct object dynamically - 07/31/08 19:38

Unfortunately that doesn't help. In this example the struct objects are just created the "normal" way.

In my case the struct objects don't have their own name, because they need to be created dynamically.

I think one of my mistake was that I declared tmpObj outside the loop, so I changed the code as follows:

while (file_str_read(fhandle,objName) != -1) {
var tmpObj = (USEABLE_OBJECT*)malloc(sizeof(USEABLE_OBJECT));
file_str_read(fhandle,str);
str_cpy(strTemp,str);
((USEABLE_OBJECT*)tmpObj).description = strTemp;
addToGSHashmap(_chr(objName), ((USEABLE_OBJECT*)tmpObj));
file_str_read(fhandle,str);
}

But it still doesn't work ... I have something in my mind that I have to declare something static, but I can't remember what it was exactly ... hmmm ... maybe the weather is too hot out there.
Posted By: pegamode

Re: Problem with creating struct object dynamically - 08/01/08 13:01

No more ideas ??? Anyone ???
Posted By: VeT

Re: Problem with creating struct object dynamically - 08/01/08 13:15

maybe you'd upload example?
Posted By: pegamode

Re: Problem with creating struct object dynamically - 08/01/08 13:40

Hmm ... it's part of a bigger project so that it's not so easy to cut it out.

But maybe it helps if I'd share my GSHashmap DLL with you, so that it might be easy to try yourself.

You can download it here:

GSHashmap

It's not final, but works fine in my tests so far.

All you have to do is create objects dynamically in a loop and add their pointers to the GSHashmap under a certain key.
(use char* as key).

Then try to get one of them (not the last one) back from the hashmap and look if it's the right one.

If I do something like this:

ENTITY* ent1 = ent_create(...);
ENTITY* ent2 = ent_create(...);

addToGSHashmap("entity1", ent1);
addToGSHashmap("entity2", ent2);

ENTITY* ent3 = (ENTITY*)getFromGSHashmap("ent1");

Everything worked fine ... but it does not if I do it dynamically in a loop.

Regards,
Pegamode.
Posted By: VeT

Re: Problem with creating struct object dynamically - 08/01/08 14:55

i'd look at it this evening smile
Posted By: pegamode

Re: Problem with creating struct object dynamically - 08/02/08 07:18

Hi VeT,

thanks for helping ...

Did you find the time to take a look?
Posted By: Fenriswolf

Re: Problem with creating struct object dynamically - 08/02/08 11:29

Hello,

Quote:
STRING* objName = "";

Perhaps the length of the string is not altered by file_str_read, so the string is always empty. You could try to initialize a longer string.
Posted By: pegamode

Re: Problem with creating struct object dynamically - 08/02/08 11:42

Hi Fenriswolf,

I tried that already ... no change at all.

If it would be always the same name or always an empty string, the size of my hashmap would rise.

I also printed out all values that come into the dll and everything seems to be ok, but it looks like I get always the same pointer to my struct and this is overwriting itself in the loop.
Posted By: Fenriswolf

Re: Problem with creating struct object dynamically - 08/02/08 12:11

There is another thing I realized just a moment ago. Dont know if this causes the problem though. : )

Quote:
((USEABLE_OBJECT*)tmpObj).description = strTemp;

This just stores a pointer to the local variable. Have you already tried to use str_cpy or str_create here?
Posted By: pegamode

Re: Problem with creating struct object dynamically - 08/02/08 12:43

Hmm ... good hint ... I'll try that.
Posted By: pegamode

Re: Problem with creating struct object dynamically - 08/02/08 20:43

Thanks a lot Fenriswolf ... now it's working fine.

The problem was that I used str_cpy, but it has to be str_create ...

file_str_read(fhandle,str);
((USEABLE_OBJECT*)tmpObj).description = str_create(str);

So it wasn't a problem with the struct object, but how the data of the struct was set. I stored a pointer to a pointer.

Now I can easily read all the additional data for my entities from my csv-file, put it into my GSHashmap and can get it back from there when I need them. A big advantage is that I also can save a lot of state data for my entities and don't lose them when changing the levels.

As I am currently working on a point&click adventure I made a big step forwards with the base implementation.

Thanks to everyone who helped ... @everyone ... feel free to use my GSHashmap.
If you have questions or tips for improvement just contact me or better write into this thread so that everyone can discuss.

Best regards,
Pegamode.
Posted By: Fenriswolf

Re: Problem with creating struct object dynamically - 08/03/08 03:44

Btw: thank you for sharing your hashmap dll. : )
I haven't tried it out yet, but it will come in handy for sure.
© 2023 lite-C Forums