ptr_remove and array -> memory poblem

Posted By: Reconnoiter

ptr_remove and array -> memory poblem - 07/24/14 20:20

Hi,

The beneath code causes a memory error for me, I don't know why crazy. Doing the panel ptr_remove outside an array doesn't cause an error (tried several values for the array number):

Code:
PANEL* pointer_pan_1[31];
PANEL* pointer_pan_2[31];
PANEL* pointer_pan_3[31];
PANEL* pointer_pan_4[31];

...

//clean up earlier created panels
 for(i=1; i<31; i++) 
 {
  if (pointer_pan_1[i]) ptr_remove(pointer_pan_1[i]);
  if (pointer_pan_2[i]) ptr_remove(pointer_pan_2[i]);
  if (pointer_pan_3[i]) ptr_remove(pointer_pan_3[i]);
  if (pointer_pan_4[i]) ptr_remove(pointer_pan_4[i]);
 }



tia
Posted By: WretchedSid

Re: ptr_remove and array -> memory poblem - 07/24/14 20:55

Well, two things...
1) Are you sure you initialized the array with NULL?
2) Why do you skip over the first element in the arrays?
Bonus question: Why not use two-dimensional arrays?
Posted By: DLively

Re: ptr_remove and array -> memory poblem - 07/25/14 02:54

I am sorry I am no help for this issue but JustSid, you gave me a chuckle grin
Quote:
Bonus question:
Posted By: Reconnoiter

Re: ptr_remove and array -> memory poblem - 07/25/14 18:40

Hi,
Quote:

Well, two things...
1) Are you sure you initialized the array with NULL?
, no thanks for pointing that out. It still crashes though (but maybe it is conflicting with other code, I have to better check that). Here is the code where I make the array set to NULL on startup:

Code:
//make array start with null values
 j = 1;
 for(i=1; j<5; i++) 
 {
  pointer_pan[j][i] = NULL;
  if (i >= 30) { j += 1; i = 1; }
 }



Quote:
2) Why do you skip over the first element in the arrays?
, for me it sounds more logical beginning with 1 than 0 tongue .

Quote:
Bonus question: Why not use two-dimensional arrays?
, because I had never heard about those blush. I am now implementing them in my code, tyty.

I am also trying putting in a text array, but I can't seem to get pstring with it working right. Anyone an idea why the beneath str_cpy results in a crash?

Code:
TEXT* pointer_text[5];

....

var i;
 //create array text mdl found
 for(i = 1; i < 5; i++)
 {
  pointer_text[i] = txt_create(1,21);
  pointer_text[i].pos_x = 10;
  pointer_text[i].pos_y = 135;
  pointer_text[i].layer = 21; 
  pointer_text[i].font = "Calibri#20";
  vec_set(pointer_text[i].blue,vector(155, 255, 155));
 }

....
//reset text string
str_cpy((pointer_text[1].pstring)[1],"");

Posted By: WretchedSid

Re: ptr_remove and array -> memory poblem - 07/25/14 19:59

Originally Posted By: Reconnoiter
Code:
//make array start with null values
 j = 1;
 for(i=1; j<5; i++) 
 {
  pointer_pan[j][i] = NULL;
  if (i >= 30) { j += 1; i = 1; }
 }



You are only doing five iterations here. Also, make sure to follow a ptr_remove() up by also setting the pointer to NULL, or else it will continue to hold garbage.


Originally Posted By: Reconnoiter
for me it sounds more logical beginning with 1 than 0 tongue .

It does not!
The reason it starts with zero as opposed to 1 is because it's an offset into the memory. The first element is has no offset, therefore its index is zero. For the sake of readability and consistency, I'd highly suggest you to use zero based indices. It's also, as a matter of fact, more logical.


Originally Posted By: Reconnoiter
I am also trying putting in a text array, but I can't seem to get pstring with it working right. Anyone an idea why the beneath str_cpy results in a crash?

Only because you think it's logical to start with index 1, it doesn't mean that the engine does. You create a text with one string, and try to copy values into the second one.

Check your pointers and indices! Especially if you are inexperienced, you really ought to stick with the convention, ie zero based indices, to avoid errors like this. That your program crashes is good, technically it's undefined behaviour to access unallocated memory and the compiler is allowed to destroy your hard drive, destroy the universe or fuck your sister. In many cases though, corrupt pointers will not crash but it seems to just work, but in reality they will alter a memory area of something else, leading to weird behaviour and strange bugs.
Posted By: Reconnoiter

Re: ptr_remove and array -> memory poblem - 07/26/14 14:05

I think it will choose to destroy the universe, cause that would be a blast, and well, I don't have a sister.

To a more serious note; I have changed every array and relevant for() so they start with 0. I got the text array working now, so thanks alot!

Quote:

Originally Posted By: Reconnoiter
Code:

//make array start with null values
j = 1;
for(i=1; j<5; i++)
{
pointer_pan[j][i] = NULL;
if (i >= 30) { j += 1; i = 1; }
}



You are only doing five iterations here.
, how come this does only does four iterations instead of 4 x 30? I think I am resetting i. Maybe I am getting blind here grin.
Posted By: Superku

Re: ptr_remove and array -> memory poblem - 07/26/14 14:17

Why don't you just use two for loops?

for(i = 0; i < 5; i++)
{
for(j = 0; j < 5; j++) pointer_pan[i][j] = NULL;
}
Posted By: Redeemer

Re: ptr_remove and array -> memory poblem - 07/26/14 16:55

Originally Posted By: Reconnoiter
how come this does only does four iterations instead of 4 x 30? I think I am resetting i. Maybe I am getting blind here

Step through it with a debugger and you'll see very clearly what's wrong. The if block is only executed 4 times because the for loop it's inside of only iterates 4 times. You should write your code the way superku demonstrated, with two for loops.
Posted By: WretchedSid

Re: ptr_remove and array -> memory poblem - 07/26/14 19:16

Originally Posted By: Redeemer
The if block is only executed 4 times because the for loop it's inside of only iterates 4 times.

I thought so too, but we are both wrong. Look again, at that horrible for loop (especially the second expression).

@Reconnoiter: I don't want to shit all over your code, but Superku is right, use two for loops. This is horrible! It's hard to read and grok, and code really ought to be self explanatory. Don't be too smart for your own and other goods!
Posted By: Reconnoiter

Re: ptr_remove and array -> memory poblem - 07/27/14 11:48

Ty Superku going to try that.

Quote:
@Reconnoiter: I don't want to shit all over your code, but Superku is right, use two for loops. This is horrible! It's hard to read and grok, and code really ought to be self explanatory. Don't be too smart for your own and other goods!
, hey it is not that I do this on purpose! I just not thought of the idea of using 2 for loops.
Posted By: WretchedSid

Re: ptr_remove and array -> memory poblem - 07/27/14 13:44

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

Re: ptr_remove and array -> memory poblem - 07/28/14 15:20

Hi WretchedSid grin ,

Quote:
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 tongue.

Quote:
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).

Code:
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);



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

Re: ptr_remove and array -> memory poblem - 07/29/14 14:12

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

Re: ptr_remove and array -> memory poblem - 07/29/14 14:47

This line can cause crash pointer_text_dir[i].font = "Calibri#20";
You should use font_create. pointer_text_dir[i].font =font_create("Calibri#20");
Posted By: Kartoffel

Re: ptr_remove and array -> memory poblem - 07/29/14 15:00

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

Re: ptr_remove and array -> memory poblem - 07/29/14 15:27

yesyesyes I works perfect now, luv you all grin
Posted By: Emre

Re: ptr_remove and array -> memory poblem - 07/29/14 15:37

Originally Posted By: Reconnoiter
yesyesyes I works perfect now, luv you all grin

it's good to know! And Kartoffel is right. It is better to declare globally, than create the same font again for each text. wink
Posted By: Reconnoiter

Re: ptr_remove and array -> memory poblem - 07/29/14 15:43

Yes I did like this globally now:

FONT* calibri20_font = "Calibri#20";
...
...
pointer_text_dir[i].font = calibri20_font;
© 2024 lite-C Forums