|
3 registered members (TipmyPip, clint000, Grant),
6,810
guests, and 4
spiders. |
|
Key:
Admin,
Global Mod,
Mod
|
|
|
Problem with creating struct object dynamically
#218897
07/31/08 09:45
07/31/08 09:45
|
Joined: Feb 2006
Posts: 1,011 Germany
pegamode
OP
Serious User
|
OP
Serious User
Joined: Feb 2006
Posts: 1,011
Germany
|
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.
|
|
|
Re: Problem with creating struct object dynamically
[Re: pegamode]
#219037
07/31/08 18:38
07/31/08 18:38
|
Joined: Feb 2006
Posts: 1,011 Germany
pegamode
OP
Serious User
|
OP
Serious User
Joined: Feb 2006
Posts: 1,011
Germany
|
|
|
|
Re: Problem with creating struct object dynamically
[Re: VeT]
#219049
07/31/08 19:38
07/31/08 19:38
|
Joined: Feb 2006
Posts: 1,011 Germany
pegamode
OP
Serious User
|
OP
Serious User
Joined: Feb 2006
Posts: 1,011
Germany
|
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.
|
|
|
Re: Problem with creating struct object dynamically
[Re: VeT]
#219200
08/01/08 13:40
08/01/08 13:40
|
Joined: Feb 2006
Posts: 1,011 Germany
pegamode
OP
Serious User
|
OP
Serious User
Joined: Feb 2006
Posts: 1,011
Germany
|
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.
|
|
|
|