Gamestudio Links
Zorro Links
Newest Posts
Newbie Questions
by fairtrader. 12/06/23 11:29
Zorro Trader GPT
by TipmyPip. 12/04/23 11:34
Square root rule
by Smallz. 12/02/23 09:15
RTest not found error
by TipmyPip. 12/01/23 21:43
neural function for Python to [Train]
by TipmyPip. 12/01/23 14:47
Xor Memory Problem.
by TipmyPip. 11/28/23 14:23
Training with command line parameters
by TipmyPip. 11/26/23 08:42
Combine USD & BTC Pairs In Asset Loop
by TipmyPip. 11/26/23 08:30
AUM Magazine
Latest Screens
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Tactics of World War I
Who's Online Now
7 registered members (fairtrader, Quad, miwok, Martin_HH, AndrewAMD, alibaba, dpn), 581 guests, and 0 spiders.
Key: Admin, Global Mod, Mod
Newest Members
fairtrader, hus, Vurtis, Harry5, KelvinC
19019 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
How do I malloc an array of panels? #295095
10/22/09 22:51
10/22/09 22:51
Joined: Aug 2005
Posts: 238
Caermundh Offline OP
Member
Caermundh  Offline OP
Member

Joined: Aug 2005
Posts: 238
So if i say:

PANEL* my_panels[50];

this will give me a 50 element array that I can store panel pointers in. Therefore, I inferred from that that I could dynamically allocate a panel array by:

PANEL* my_panel;
my_panel = (PANEL*)malloc(sizeof(PANEL*)*50);

Alas, this does not appear to work. When I try to run the script I get an error message about not beg able to convert pointer to long.

What am I doing wrong above?

Re: How do I malloc an array of panels? [Re: Caermundh] #295099
10/22/09 23:07
10/22/09 23:07
Joined: Oct 2007
Posts: 5,209
İstanbul, Turkey
Quad Online
Senior Expert
Quad  Online
Senior Expert

Joined: Oct 2007
Posts: 5,209
İstanbul, Turkey
PANEL* my_panels[50];

Code:
void init_panels_startup(){
  var i = 0;
  for(i=0;i<50;i++)my_panels[i] = malloc(sizeof(PANEL));
}



that's all just paste it to your code somewhere, function will run automatically at startup without calling it. (functions ending with _startup does that.)


3333333333
Re: How do I malloc an array of panels? [Re: Quad] #295102
10/22/09 23:17
10/22/09 23:17
Joined: Aug 2005
Posts: 238
Caermundh Offline OP
Member
Caermundh  Offline OP
Member

Joined: Aug 2005
Posts: 238
Ok,

I tried that. I still get the cannot convert error messages.

Re: How do I malloc an array of panels? [Re: Caermundh] #295103
10/22/09 23:23
10/22/09 23:23
Joined: Oct 2007
Posts: 5,209
İstanbul, Turkey
Quad Online
Senior Expert
Quad  Online
Senior Expert

Joined: Oct 2007
Posts: 5,209
İstanbul, Turkey
hmm strange, are you sure you are writing PANEL* my_panels[50]; and not PANEL my_panels[50]; or PANEL my_panel; ??

this code alone compiles and runs fine for me:
Code:
PANEL* my_panels[50];
void init_panels_startup(){
  var i = 0;
  for(i=0;i<50;i++)my_panels[i] = malloc(sizeof(PANEL));
}




3333333333
Re: How do I malloc an array of panels? [Re: Quad] #295104
10/22/09 23:36
10/22/09 23:36
Joined: Aug 2005
Posts: 238
Caermundh Offline OP
Member
Caermundh  Offline OP
Member

Joined: Aug 2005
Posts: 238
Im doing this:

PANEL* my_panels;

count = 0;
while(count<50)
{ my_panels[count] = (PANEL*)malloc(sizeof(PANEL*));
count += 1;
}

Im not declaring PANEL* my_panels[50]; because if i did there would be no need to malloc. I need to be able allocate the array at runtime. the array size needed is dependant on various things. suffice to say sometimes ill need my_panels[5] other times ill need my_panels[100];

Re: How do I malloc an array of panels? [Re: Caermundh] #295105
10/22/09 23:48
10/22/09 23:48
Joined: Oct 2007
Posts: 5,209
İstanbul, Turkey
Quad Online
Senior Expert
Quad  Online
Senior Expert

Joined: Oct 2007
Posts: 5,209
İstanbul, Turkey
PANEL* my_panels[50]; because if i did there would be no need to malloc.

wrong. you only get space for PANEL pointers this way, not the actual panels, you still need to alloc space for them.


3333333333
Re: How do I malloc an array of panels? [Re: Caermundh] #295106
10/22/09 23:49
10/22/09 23:49
Joined: Aug 2008
Posts: 133
Sweden, Stockholm
E
Enduriel Offline
Member
Enduriel  Offline
Member
E

Joined: Aug 2008
Posts: 133
Sweden, Stockholm
PANEL* my_panels[50]; is an array of pointers, basicly 32 bit memory adresses with no content.

EDIT: lolz quadraxas 49 seconds before me frown

Last edited by Enduriel; 10/22/09 23:49.
Re: How do I malloc an array of panels? [Re: Enduriel] #295108
10/22/09 23:52
10/22/09 23:52
Joined: Aug 2005
Posts: 238
Caermundh Offline OP
Member
Caermundh  Offline OP
Member

Joined: Aug 2005
Posts: 238
right, but PANEL* my_panels[50] is a fixed size array. what happens when I need 100 panel pointers, for example?

I need to dynamically allocte the whole array, so PANEL* my_panels[50] defeats the purpose.

Sorry if i being contrary, but i'd really love to be able to do a dynmically allocated array of panels at run time.

Re: How do I malloc an array of panels? [Re: Caermundh] #295110
10/22/09 23:59
10/22/09 23:59
Joined: Oct 2007
Posts: 5,209
İstanbul, Turkey
Quad Online
Senior Expert
Quad  Online
Senior Expert

Joined: Oct 2007
Posts: 5,209
İstanbul, Turkey
in that case, you need a PANEL** which wil point to your PANEL*s

like

Code:
PANEL** my_panels;

void panels_startup(){
	my_panels = malloc(sizeof(PANEL*)*120); 
	//at this point you basically have space for 120 panel pointers, 120 can be a variable
	var count = 0;
	for(count=0;count<120;count++) my_panels[count] = malloc(sizeof(PANEL));
	//making my panel pointers to point to something.
}



ps you could also use
for(count=0;count<120;count++) my_panels[count] = pan_create("panel definition goes here"); instead of mallocs.
check pan_create from manual for that.

AND if you need your panel number to decrease/increase after creating the initial array, use linked-lists.


3333333333
Re: How do I malloc an array of panels? [Re: Quad] #295113
10/23/09 00:19
10/23/09 00:19
Joined: Aug 2005
Posts: 238
Caermundh Offline OP
Member
Caermundh  Offline OP
Member

Joined: Aug 2005
Posts: 238
BEAUTIFUL!!!! that works great! thank you so much! I can now put the finishing touch on this algorithm and move on with developing this game ;D (I've been stuck on this thing for like a month - For other reasons than the malloc(), but this was kina the last straw after being stuck for a month)


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