0 registered members (),
1,089
guests, and 10
spiders. |
Key:
Admin,
Global Mod,
Mod
|
|
|
Re: ptr_remove and array -> memory poblem
[Re: Reconnoiter]
#443885
07/27/14 13:44
07/27/14 13:44
|
Joined: Apr 2007
Posts: 3,751 Canada
WretchedSid
Expert
|
Expert
Joined: Apr 2007
Posts: 3,751
Canada
|
As a general rule, if you think "wouldn't it be great if XYZ would work and I could make my code cleaner", the answer is: "It does work!" Here is some code snippet that probably does what you want:
PANEL *pointer_pan[4][31];
void initialize()
{
memset(pointer_pan, 0, 4 * 31 * sizeof(PANEL *));
}
void cleanup()
{
int i, j;
for(i = 0; i < 4; i ++)
{
for(j = 0; j < 31; j ++)
{
if(pointer_pan[i][j])
{
ptr_remove(pointer_pan[i][j]);
pointer_pan[i][j] = NULL;
}
}
}
}
You may want to learn/read up a bit on basic programming and/or read some code written by others with experience. Note that the memset() trick doesn't work with dynamically allocated multidimensional arrays, because in reality this is still a flat, contiguous block of memory. The compiler will translate the 2D access into a single offset into the memory block.
Last edited by JustSid; 07/27/14 13:46.
Shitlord by trade and passion. Graphics programmer at Laminar Research. I write blog posts at feresignum.com
|
|
|
Re: ptr_remove and array -> memory poblem
[Re: WretchedSid]
#443983
07/28/14 15:20
07/28/14 15:20
|
Joined: Dec 2011
Posts: 1,823 Netherlands
Reconnoiter
OP
Serious User
|
OP
Serious User
Joined: Dec 2011
Posts: 1,823
Netherlands
|
Hi WretchedSid , As a general rule, if you think "wouldn't it be great if XYZ would work and I could make my code cleaner", the answer is: "It does work!" , not a bad rule, but you first have to think it / come with the idea . memset(pointer_pan, 0, 4 * 31 * sizeof(PANEL *)); , what does memset do? Allocate memory? I can't find it as a function in the manual. Concerning the code I am almost there, not much longer till the bug is squished. I get a crash when trying to show this text though (last line does it). It is probably something stupid but I am getting a bit dizzy of all those arrays (even when they look clearer now thanks to you and Superku).
TEXT* pointer_text_dir[4];
function init_arraysandpointers_startup()
{
var i, j;
//create array text dir
for(i = 0; i < 4; i++)
{
pointer_text_dir[i] = txt_create(30,21);
pointer_text_dir[i].pos_x = 10;
pointer_text_dir[i].pos_y = 135;
pointer_text_dir[i].layer = 21;
pointer_text_dir[i].font = "Calibri#20";
vec_set(pointer_text_dir[i].blue,vector(155, 255, 155));
}
...
}
...
...
//clean up earlier created txt strings
var i, j;
for(i = 0; i < 4; i++)
{
for(j = 0; j < 30; j++) str_cpy((pointer_text_dir[i].pstring)[j],"");
}
...
...
filesfound_var = txt_for_dir(pointer_text_dir[0],"Folder 1\\*.mdl");
set (pointer_text_dir[0],SHOW);
You may want to learn/read up a bit on basic programming and/or read some code written by others with experience. , fair enough, I do already try alot through manual and sometimes AUM. And those weekly tips from e.g. Superku. I will look around some more though.
|
|
|
Re: ptr_remove and array -> memory poblem
[Re: Reconnoiter]
#444037
07/29/14 14:12
07/29/14 14:12
|
Joined: Apr 2007
Posts: 3,751 Canada
WretchedSid
Expert
|
Expert
Joined: Apr 2007
Posts: 3,751
Canada
|
I'm afraid I can't help you with txt_for_dir(). Someone else want to pitch in?
Anyways, I can help you with memset(). It's a function from the C standard library and it sets the content of a given memory block to the given constant. In the example case it was set to 0, so the block of memory was zeroed out. It's short than the for loop, and potentially faster: Usually the library shipped with the system has an optimized version for your CPU, which takes advantage of the big guns in the instruction set (in the case of modern x86 CPUs this means it uses AVX to store 512bits at once)
Shitlord by trade and passion. Graphics programmer at Laminar Research. I write blog posts at feresignum.com
|
|
|
Re: ptr_remove and array -> memory poblem
[Re: Emre]
#444041
07/29/14 15:00
07/29/14 15:00
|
Joined: Jun 2009
Posts: 2,210 Bavaria, Germany
Kartoffel
Expert
|
Expert
Joined: Jun 2009
Posts: 2,210
Bavaria, Germany
|
This line can cause crash pointer_text_dir[i].font = "Calibri#20"; This line must cause a crash because "Calibri#20" is not a font but just some characters. You'r 2nd example should work, although it's better to define the font globally.
POTATO-MAN saves the day! - Random
|
|
|
Moderated by mk_1, Perro, rayp, Realspawn, Rei_Ayanami, rvL_eXile, Spirit, Superku, Tobias, TSG_Torsten, VeT
|