0 registered members (),
1,397
guests, and 7
spiders. |
Key:
Admin,
Global Mod,
Mod
|
|
|
ptr_remove and array -> memory poblem
#443805
07/24/14 20:20
07/24/14 20:20
|
Joined: Dec 2011
Posts: 1,823 Netherlands
Reconnoiter
OP
Serious User
|
OP
Serious User
Joined: Dec 2011
Posts: 1,823
Netherlands
|
Hi, The beneath code causes a memory error for me, I don't know why . Doing the panel ptr_remove outside an array doesn't cause an error (tried several values for the array number):
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
|
|
|
Re: ptr_remove and array -> memory poblem
[Re: Reconnoiter]
#443807
07/24/14 20:55
07/24/14 20:55
|
Joined: Apr 2007
Posts: 3,751 Canada
WretchedSid
Expert
|
Expert
Joined: Apr 2007
Posts: 3,751
Canada
|
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?
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: DLively]
#443837
07/25/14 18:40
07/25/14 18:40
|
Joined: Dec 2011
Posts: 1,823 Netherlands
Reconnoiter
OP
Serious User
|
OP
Serious User
Joined: Dec 2011
Posts: 1,823
Netherlands
|
Hi, 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:
//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; }
}
2) Why do you skip over the first element in the arrays? , for me it sounds more logical beginning with 1 than 0 . Bonus question: Why not use two-dimensional arrays? , because I had never heard about those . 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?
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],"");
Last edited by Reconnoiter; 07/25/14 18:43.
|
|
|
Re: ptr_remove and array -> memory poblem
[Re: Reconnoiter]
#443841
07/25/14 19:59
07/25/14 19:59
|
Joined: Apr 2007
Posts: 3,751 Canada
WretchedSid
Expert
|
Expert
Joined: Apr 2007
Posts: 3,751
Canada
|
//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. for me it sounds more logical beginning with 1 than 0 . 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. 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.
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]
#443854
07/26/14 14:05
07/26/14 14:05
|
Joined: Dec 2011
Posts: 1,823 Netherlands
Reconnoiter
OP
Serious User
|
OP
Serious User
Joined: Dec 2011
Posts: 1,823
Netherlands
|
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! 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 .
|
|
|
Re: ptr_remove and array -> memory poblem
[Re: Reconnoiter]
#443860
07/26/14 16:55
07/26/14 16:55
|
Joined: Dec 2008
Posts: 1,660 North America
Redeemer
Serious User
|
Serious User
Joined: Dec 2008
Posts: 1,660
North America
|
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.
|
|
|
Re: ptr_remove and array -> memory poblem
[Re: Redeemer]
#443863
07/26/14 19:16
07/26/14 19:16
|
Joined: Apr 2007
Posts: 3,751 Canada
WretchedSid
Expert
|
Expert
Joined: Apr 2007
Posts: 3,751
Canada
|
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!
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]
#443880
07/27/14 11:48
07/27/14 11:48
|
Joined: Dec 2011
Posts: 1,823 Netherlands
Reconnoiter
OP
Serious User
|
OP
Serious User
Joined: Dec 2011
Posts: 1,823
Netherlands
|
Ty Superku going to try that. @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.
|
|
|
Moderated by mk_1, Perro, rayp, Realspawn, Rei_Ayanami, rvL_eXile, Spirit, Superku, Tobias, TSG_Torsten, VeT
|