1 registered members (Miska),
755
guests, and 2
spiders. |
Key:
Admin,
Global Mod,
Mod
|
|
|
Allocate pointer in a struct
#407422
09/12/12 18:12
09/12/12 18:12
|
Joined: Sep 2012
Posts: 74 Niedersachsen, Germany
PriNova
OP
Junior Member
|
OP
Junior Member
Joined: Sep 2012
Posts: 74
Niedersachsen, Germany
|
Here is my problem. First i define a struct with type organism. it includes 1 char and one int pointer (later used as an array). Then i initialize the struct to a pointer with name Agent. After that i want to allocate a size of space only to the pointer(array) dna, but it doesn't work and i don't know why. i read my book about structs, pointers and arrays, but didn't give me a solution. here is the code:
struct organism
{
char fitness;
int *dna;
};
void main()
{
struct organism *Agent; //intiialize struct organism to pointer Agent
Agent.dna = malloc(5*sizeof(int)); //TRY to allocate 5times the dna integer to get an array
Agent.dna[3] = 12; //try to set a value into index 3 of the dna-array
}
then it fails and the error message is: subscript requires array or pointer type. what it do, is to allocate 5 times memory for the whole agent and not only for the dna(array).
Last edited by PriNova; 09/12/12 18:12.
|
|
|
Re: Allocate pointer in a struct
[Re: MasterQ32]
#407426
09/12/12 19:01
09/12/12 19:01
|
Joined: Sep 2012
Posts: 74 Niedersachsen, Germany
PriNova
OP
Junior Member
|
OP
Junior Member
Joined: Sep 2012
Posts: 74
Niedersachsen, Germany
|
oh, my Master032 it works. What else than the brackets. Please let the brackets in the next version away. hehe
thanks for that. i write it into my special rule book. haha
Last edited by PriNova; 09/12/12 19:01.
|
|
|
Re: Allocate pointer in a struct
[Re: PriNova]
#407442
09/13/12 10:09
09/13/12 10:09
|
Joined: Dec 2009
Posts: 128 China
frankjiang
Member
|
Member
Joined: Dec 2009
Posts: 128
China
|
don`t be use mallor ,free function,they are from c,if you use they,you memory will be lost. in lite,you can use sys_malloc,sys_free,like this code.
typedef struct
{
char fitness;
int* dna;
}organism;
void main(){
organism* Agent = sys_malloc(sizeof(organism));
Agent.dna = sys_malloc(5*sizeof(int));
//reset your memory,make all is '\0'
memset(Agent.dna,'\0',5);
//and than set your varible
(Agent.dna)[3] = 12;
printf("%d", (Agent.dna)[3]);
sys_free(Agent.dna);
sys_free(Agent);
}
development 3d game is interesting!
|
|
|
Re: Allocate pointer in a struct
[Re: frankjiang]
#407449
09/13/12 11:44
09/13/12 11:44
|
Joined: Nov 2007
Posts: 2,568 Germany, BW, Stuttgart
MasterQ32
Expert
|
Expert
Joined: Nov 2007
Posts: 2,568
Germany, BW, Stuttgart
|
//reset your memory,make all is '\0'
memset(Agent.dna,'\0',5);
// BETTER
//reset your memory,make all is '\0'
memset(Agent.dna,0,sizeof(int) * 5); // does not need to be a character, also int is 4 byte in size
but if you use sys_malloc both of it isn't necessary because sys_malloc does this for you!
|
|
|
Re: Allocate pointer in a struct
[Re: frankjiang]
#407469
09/13/12 14:03
09/13/12 14:03
|
Joined: Jan 2002
Posts: 4,225 Germany / Essen
Uhrwerk
Expert
|
Expert
Joined: Jan 2002
Posts: 4,225
Germany / Essen
|
don`t be use mallor ,free function,they are from c,if you use they,you memory will be lost. That is nonsense. malloc and free work perfectly fine. You don't loose any memory with this. sys_malloc and sys_free are the recommended way to go though.
Always learn from history, to be sure you make the same mistakes again...
|
|
|
Re: Allocate pointer in a struct
[Re: Uhrwerk]
#407489
09/13/12 18:23
09/13/12 18:23
|
Joined: Sep 2012
Posts: 74 Niedersachsen, Germany
PriNova
OP
Junior Member
|
OP
Junior Member
Joined: Sep 2012
Posts: 74
Niedersachsen, Germany
|
i used sys_malloc to intitialize my agents and their genomes. At dimensions of 30 Agents each 500 Genomes my Engine chrashes.
but if i use only malloc and free i could initialize 1000 Agents with each 1000 Genomes. And their move on the screen very fast. (like Ants)
I'm benchmarking my script at the moment.
EDIT: actual 5000 Agents each 1000 Genomes = 5.000.000 Genes processed in nearly 20 seconds.
Last edited by PriNova; 09/13/12 18:25.
|
|
|
Re: Allocate pointer in a struct
[Re: PriNova]
#407506
09/14/12 02:44
09/14/12 02:44
|
Joined: Jan 2002
Posts: 4,225 Germany / Essen
Uhrwerk
Expert
|
Expert
Joined: Jan 2002
Posts: 4,225
Germany / Essen
|
I cannot imagine a bug in sys_malloc or sys_free. Many users have used this instruction without any problems before - including me. If your game crashes when using sys_malloc and works with malloc I'd suspect the other code that crashes. Maybe you're overwriting memory? Why don't you post the code that crashes so we can have a look at it?
Always learn from history, to be sure you make the same mistakes again...
|
|
|
|