Gamestudio Links
Zorro Links
Newest Posts
lookback setting performance issue
by 7th_zorro. 04/16/24 03:08
folder management functions
by 7th_zorro. 04/15/24 10:10
zorro 64bit command line support
by 7th_zorro. 04/15/24 09:36
Zorro FIX plugin - Experimental
by flink. 04/14/24 07:48
Zorro FIX plugin - Experimental
by flink. 04/14/24 07:46
LPDIRECT3DCUBETEXTUR
E9

by Ayumi. 04/12/24 11:00
Sam Foster Sound | Experienced Game Composer for Hire
by titanicpiano14. 04/11/24 14:56
SGT_FW
by Aku_Aku. 04/10/24 16:09
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
2 registered members (7th_zorro, Quad), 373 guests, and 3 spiders.
Key: Admin, Global Mod, Mod
Newest Members
11honza11, ccorrea, sakolin, rajesh7827, juergen_wue
19045 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
Page 1 of 2 1 2
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 Offline OP
Serious User
Reconnoiter  Offline 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 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

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 Offline
Expert
WretchedSid  Offline
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: WretchedSid] #443811
07/25/14 02:54
07/25/14 02:54
Joined: Apr 2005
Posts: 1,988
Canadian, Eh
DLively Offline
Serious User
DLively  Offline
Serious User

Joined: Apr 2005
Posts: 1,988
Canadian, Eh
I am sorry I am no help for this issue but JustSid, you gave me a chuckle grin
Quote:
Bonus question:

Last edited by DLively; 07/25/14 02:54.

A8 Pro 8.45.4
YouTube: Create Games For Free
Free Resources: www.CGForFree.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 Offline OP
Serious User
Reconnoiter  Offline OP
Serious User

Joined: Dec 2011
Posts: 1,823
Netherlands
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],"");


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 Offline
Expert
WretchedSid  Offline
Expert

Joined: Apr 2007
Posts: 3,751
Canada
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.


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 Offline OP
Serious User
Reconnoiter  Offline 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!

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.

Re: ptr_remove and array -> memory poblem [Re: Reconnoiter] #443856
07/26/14 14:17
07/26/14 14:17
Joined: Sep 2003
Posts: 6,861
Kiel (Germany)
Superku Offline
Senior Expert
Superku  Offline
Senior Expert

Joined: Sep 2003
Posts: 6,861
Kiel (Germany)
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;
}


"Falls das Resultat nicht einfach nur dermassen gut aussieht, sollten Sie nochmal von vorn anfangen..." - Manual

Check out my new game: Pogostuck: Rage With Your Friends
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 Offline
Serious User
Redeemer  Offline
Serious User

Joined: Dec 2008
Posts: 1,660
North America
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.


Eats commas for breakfast.

Play Barony: Cursed Edition!
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 Offline
Expert
WretchedSid  Offline
Expert

Joined: Apr 2007
Posts: 3,751
Canada
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!


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 Offline OP
Serious User
Reconnoiter  Offline OP
Serious User

Joined: Dec 2011
Posts: 1,823
Netherlands
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.

Page 1 of 2 1 2

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