Gamestudio Links
Zorro Links
Newest Posts
AlpacaZorroPlugin v1.3.0 Released
by kzhao. 05/22/24 13:41
Free Live Data for Zorro with Paper Trading?
by AbrahamR. 05/18/24 13:28
Change chart colours
by 7th_zorro. 05/11/24 09:25
Data from CSV not parsed correctly
by dr_panther. 05/06/24 18:50
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
1 registered members (Miska), 755 guests, and 2 spiders.
Key: Admin, Global Mod, Mod
Newest Members
lorikob361, LucasJoshua, Baklazhan, Hanky27, firatv
19054 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
Page 1 of 2 1 2
Allocate pointer in a struct #407422
09/12/12 18:12
09/12/12 18:12
Joined: Sep 2012
Posts: 74
Niedersachsen, Germany
P
PriNova Offline OP
Junior Member
PriNova  Offline OP
Junior Member
P

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:

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: PriNova] #407424
09/12/12 18:30
09/12/12 18:30
Joined: Nov 2007
Posts: 2,568
Germany, BW, Stuttgart
MasterQ32 Offline
Expert
MasterQ32  Offline
Expert

Joined: Nov 2007
Posts: 2,568
Germany, BW, Stuttgart
(Agent.dna)[3] = 12


Visit my site: www.masterq32.de
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
P
PriNova Offline OP
Junior Member
PriNova  Offline OP
Junior Member
P

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 Offline
Member
frankjiang  Offline
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.
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 Offline
Expert
MasterQ32  Offline
Expert

Joined: Nov 2007
Posts: 2,568
Germany, BW, Stuttgart
Code:
//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!


Visit my site: www.masterq32.de
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 Offline
Expert
Uhrwerk  Offline
Expert

Joined: Jan 2002
Posts: 4,225
Germany / Essen
Originally Posted By: frankjiang
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
P
PriNova Offline OP
Junior Member
PriNova  Offline OP
Junior Member
P

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 Offline
Expert
Uhrwerk  Offline
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...
Re: Allocate pointer in a struct [Re: Uhrwerk] #407511
09/14/12 07:16
09/14/12 07:16
Joined: Mar 2011
Posts: 3,150
Budapest
sivan Offline
Expert
sivan  Offline
Expert

Joined: Mar 2011
Posts: 3,150
Budapest
I cannot really know what could be your problem, but due to my experience after allocation by malloc/sys_malloc/nx_alloc it is necessary immediately to fill variables with values (memset), and set pointers (at least to NULL). Only when this is done, you can assign individual values e.g. myarray[3] = 123; , as frankjiang suggested.
moreover I always use it like:
mystructarray = (*mystruct)sys_malloc( myarraylength * sizeof(mystruct) );
where myarraylength must be int
malloc is needed when you need realloc only. sys_malloc is more stupid safe, and features auto memory freeing on engine close.


Free world editor for 3D Gamestudio: MapBuilder Editor
Re: Allocate pointer in a struct [Re: sivan] #407512
09/14/12 07:29
09/14/12 07:29
Joined: Mar 2011
Posts: 3,150
Budapest
sivan Offline
Expert
sivan  Offline
Expert

Joined: Mar 2011
Posts: 3,150
Budapest
and one more thing that could help:
Code:
// 2D array [dim1][dim2] :

	short** myarray = NULL;
...
	myarray = (short**)sys_malloc( (int)dim1length * sizeof(short*) );
	for(i=0;i<dim1length ;i++)
		{
			myarray[i]	= (short*)sys_malloc( (int)dim2length  * sizeof(short) );
			for(j=0;j<dim2length;j++)
				{
					(myarray[i])[j] = 0;
				}
		}



Free world editor for 3D Gamestudio: MapBuilder Editor
Page 1 of 2 1 2

Moderated by  HeelX, Lukas, rayp, Rei_Ayanami, Superku, Tobias, TWO, VeT 

Gamestudio download | chip programmers | Zorro platform | shop | Data Protection Policy

oP group Germany GmbH | Birkenstr. 25-27 | 63549 Ronneburg / Germany | info (at) opgroup.de

Powered by UBB.threads™ PHP Forum Software 7.7.1