Gamestudio Links
Zorro Links
Newest Posts
Trading Journey
by howardR. 04/28/24 09:55
basik85278
by basik85278. 04/28/24 08:56
Zorro Trader GPT
by TipmyPip. 04/27/24 13:50
Help with plotting multiple ZigZag
by M_D. 04/26/24 20:03
Data from CSV not parsed correctly
by jcl. 04/26/24 11:18
M1 Oversampling
by jcl. 04/26/24 11:12
Why Zorro supports up to 72 cores?
by jcl. 04/26/24 11:09
Eigenwerbung
by jcl. 04/26/24 11:08
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
2 registered members (AndrewAMD, Quad), 755 guests, and 5 spiders.
Key: Admin, Global Mod, Mod
Newest Members
wandaluciaia, Mega_Rod, EternallyCurious, howardR, 11honza11
19049 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
Page 1 of 2 1 2
baffling crash #430867
10/03/13 21:01
10/03/13 21:01
Joined: Jun 2007
Posts: 1,337
Hiporope and its pain
txesmi Offline OP
Serious User
txesmi  Offline OP
Serious User

Joined: Jun 2007
Posts: 1,337
Hiporope and its pain
How can this crash in main?

Code:
#include <acknex.h>

#define INTARRAY    128

void main ()
{
	var array[INTARRAY][INTARRAY];
	var i, ii;
	
	for ( i=0; i<INTARRAY; i+=1 )
	{
		for ( ii=0; ii<INTARRAY; ii+=1 )
		{
			array[i][ii] = maxv ( i/INTARRAY, ii/INTARRAY );
		}
	}
	
	while ( !key_esc )
	{
		wait(1);
	}
	
	sys_exit ( NULL );
}



Could someone test it in order to know if it is my pc problem? I can't increase the array size without a crash... I can't believe it crazy

Re: baffling crash [Re: txesmi] #430868
10/03/13 22:11
10/03/13 22:11
Joined: Nov 2006
Posts: 497
Ohio
xbox Offline
Senior Member
xbox  Offline
Senior Member

Joined: Nov 2006
Posts: 497
Ohio
I just tried it on my pc, and through placing beeps after every line, it is crashing at the very beginning of the first for loop, more precisely on this line.
Code:
for ( i=0; i<INTARRAY; i+=1 )


Re: baffling crash [Re: xbox] #430869
10/04/13 00:35
10/04/13 00:35
Joined: Jan 2002
Posts: 4,225
Germany / Essen
Uhrwerk Offline
Expert
Uhrwerk  Offline
Expert

Joined: Jan 2002
Posts: 4,225
Germany / Essen
It works with 127, but crashes with 128. If you sum up the required memory it is 128 * 128 * 4 bytes = 65536 bytes = 64 kbytes. I guess that's the maximum stack size. You shouldn't allocate such arrays on the stack anyways.

Just make array a global variable or allocate it with sys_malloc and release it with sys_free.


Always learn from history, to be sure you make the same mistakes again...
Re: baffling crash [Re: Uhrwerk] #430871
10/04/13 06:49
10/04/13 06:49
Joined: Sep 2003
Posts: 6,861
Kiel (Germany)
Superku Offline
Senior Expert
Superku  Offline
Senior Expert

Joined: Sep 2003
Posts: 6,861
Kiel (Germany)
Exactly, the manual says the same as Uhrwerk:

Originally Posted By: Variables
Be careful when defining huge local arrays. All local variables are stored on a special memory area called the stack. This area has a limited size that depends on where your function is running and whether it's called by other functions. Exceeding the stack size causes any program to crash . Thus, when you need huge local arrays of ten thousands of variables, or when you want to determine the array size dynamically, use the sys_malloc / sys_free method.


I had to learn this limitation myself the hard way: Many years ago I worked on a puzzle solving algorithm with lite-C and I could not understand why it crashed or even "went the wrong way" on the puzzle board after some 100 iterations. It took me quite literally multiple days to find out that the local array declaration of a function in my recursive program would fail without notice after a certain recursive depth.


"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: baffling crash [Re: Superku] #430874
10/04/13 07:41
10/04/13 07:41
Joined: Jun 2007
Posts: 1,337
Hiporope and its pain
txesmi Offline OP
Serious User
txesmi  Offline OP
Serious User

Joined: Jun 2007
Posts: 1,337
Hiporope and its pain
fvk, man, what a rookie failure. I should think about the limit of the stack. I feel like a absolute noob right now. Earth, swallow me!

Lots of thanks for your answers.

Re: baffling crash [Re: txesmi] #430888
10/04/13 11:59
10/04/13 11:59
Joined: Jan 2002
Posts: 4,225
Germany / Essen
Uhrwerk Offline
Expert
Uhrwerk  Offline
Expert

Joined: Jan 2002
Posts: 4,225
Germany / Essen
Originally Posted By: txesmi
I feel like a absolute noob right now. Earth, swallow me!
Don't feel bad. This can happen to anybody. Especially if you're used to a managed programming language where none of this stuff gets placed on the stack with the exception of the corresponding reference.


Always learn from history, to be sure you make the same mistakes again...
Re: baffling crash [Re: Uhrwerk] #430889
10/04/13 12:27
10/04/13 12:27
Joined: Aug 2002
Posts: 3,258
Mainz
oliver2s Offline
Expert
oliver2s  Offline
Expert

Joined: Aug 2002
Posts: 3,258
Mainz
Does it always crash immediatly if I exceed the stack or can I also get a crash somewhere later in the script?

Re: baffling crash [Re: oliver2s] #430891
10/04/13 13:15
10/04/13 13:15
Joined: Jan 2002
Posts: 4,225
Germany / Essen
Uhrwerk Offline
Expert
Uhrwerk  Offline
Expert

Joined: Jan 2002
Posts: 4,225
Germany / Essen
I guess(!) exceeding the stack's size always crashes immediately. (Overwriting random memory could also be an option according to my lite-c experiences. ^^) But to be sure I'd suggest asking jcl.


Always learn from history, to be sure you make the same mistakes again...
Re: baffling crash [Re: Uhrwerk] #430895
10/04/13 14:06
10/04/13 14:06
Joined: Apr 2007
Posts: 3,751
Canada
WretchedSid Offline
Expert
WretchedSid  Offline
Expert

Joined: Apr 2007
Posts: 3,751
Canada
I tried to get anything meaningful out of the MSDN, but it doesn't appear like the last map of the stack is unmapped intentionally. So you might very well end up with memory corruption upon a stack overflow instead of a segfault.

Edit: Write your own test case: Create a recursive function and call it repeatedly. Each recursion will add 8 bytes on the stack, simply look how far down the rabbit hole you get before it crashes. I don't think that Lite-C supports tail-call optimization, but if it does, simply thwart it by making another call into anything that the compiler only has the declaration of.

Last edited by JustSid; 10/04/13 14:13.

Shitlord by trade and passion. Graphics programmer at Laminar Research.
I write blog posts at feresignum.com
Re: baffling crash [Re: txesmi] #430901
10/04/13 17:51
10/04/13 17:51
Joined: Oct 2005
Posts: 12
I
irchel2 Offline
Newbie
irchel2  Offline
Newbie
I

Joined: Oct 2005
Posts: 12
I'd give malloc() a shot. It allocates memory on the heap instead of the stack. Look up a C language documentation how to use it.

You would have to use int instead of var then, I presume. Leave a note if you want me to figure out an exact implementation for it.

Page 1 of 2 1 2

Moderated by  HeelX, Lukas, rayp, Rei_Ayanami, Superku, Tobias, TWO, VeT 

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