How do I malloc an array of panels?

Posted By: Caermundh

How do I malloc an array of panels? - 10/22/09 22:51

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?
Posted By: Quad

Re: How do I malloc an array of panels? - 10/22/09 23:07

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.)
Posted By: Caermundh

Re: How do I malloc an array of panels? - 10/22/09 23:17

Ok,

I tried that. I still get the cannot convert error messages.
Posted By: Quad

Re: How do I malloc an array of panels? - 10/22/09 23:23

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));
}


Posted By: Caermundh

Re: How do I malloc an array of panels? - 10/22/09 23:36

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];
Posted By: Quad

Re: How do I malloc an array of panels? - 10/22/09 23:48

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.
Posted By: Enduriel

Re: How do I malloc an array of panels? - 10/22/09 23:49

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
Posted By: Caermundh

Re: How do I malloc an array of panels? - 10/22/09 23:52

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.
Posted By: Quad

Re: How do I malloc an array of panels? - 10/22/09 23:59

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.
Posted By: Caermundh

Re: How do I malloc an array of panels? - 10/23/09 00:19

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)
© 2023 lite-C Forums