Gamestudio Links
Zorro Links
Newest Posts
blogherenowcenter
by 3s05bmmc. 06/05/24 06:08
New FXCM FIX Plugin
by flink. 06/04/24 07:30
AlpacaZorroPlugin v1.3.0 Released
by kzhao. 05/22/24 13:41
Free Live Data for Zorro with Paper Trading?
by AbrahamR. 05/18/24 13:28
Change chart colours
by 7th_zorro. 05/11/24 09:25
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
1 registered members (3s05bmmc), 745 guests, and 3 spiders.
Key: Admin, Global Mod, Mod
Newest Members
AemStones, LucasJoshua, Baklazhan, Hanky27, firatv
19057 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
Problem with dynamic multidimensional arrays #438578
03/17/14 20:35
03/17/14 20:35
Joined: Mar 2014
Posts: 33
Germany: Sachsen
N
Nicros Offline OP
Newbie
Nicros  Offline OP
Newbie
N

Joined: Mar 2014
Posts: 33
Germany: Sachsen
Hi,
I want to define and initialize two 2-dimensional dynamic Arrays.
Here is my code:

Code:
#include <acknex.h>
#include <default.c>

short **field;	// [width][height]
short **arr;	// [foo]  [2]

int main()
{
	int height,width,foo,i,j;
	height = 11;
	width = 12;      	// initialize height and width
	field = sys_malloc(sizeof(short**) * width);
	for(i=0;i<width;i++)
	{
		field[i] = sys_malloc(sizeof(short*) * height);	// allocate memory for field
	}
	printf("1");
	for(i=0;i<width;i++)
	{
		for(j=0;j<height;j++)
		{
			(field[i])[j] = 2;		// set pointless numbers in field
		}
	}
	foo=10;						// initialize foo
	
	arr = sys_malloc(sizeof(short**) * foo);
	printf("2");
	for(i=0;i<foo;i++)
	{
		arr = sys_malloc(sizeof(short*) * 2);
	}
	printf("3");
	for(i=0;i<foo;i++)
	{
		(arr[i])[0] = 5;		// crash
		(arr[i])[1] = 5;
	}
	printf("4");
	return 0;	
}


I reduced my code to that "simple" part.
All numbers up to 3 were print out. So the mistake must be in the last loop.
I know it can only be a very simple mistake but I really didn't found it. (Sorry for my bad english...)

thanks for your help

Re: Problem with dynamic multidimensional arrays [Re: Nicros] #438579
03/17/14 21:05
03/17/14 21:05
Joined: Jun 2007
Posts: 1,337
Hiporope and its pain
txesmi Offline
Serious User
txesmi  Offline
Serious User

Joined: Jun 2007
Posts: 1,337
Hiporope and its pain
Hi,
there is a mistake in the memory allocation. It should be as follows:
Code:
field = sys_malloc(sizeof(short*) * width); // array of pointers
	for(i=0;i<width;i++)
	{
		field[i] = sys_malloc(sizeof(short) * height);	// array of variables
	}



salud!

Re: Problem with dynamic multidimensional arrays [Re: txesmi] #438599
03/18/14 13:24
03/18/14 13:24
Joined: Mar 2014
Posts: 33
Germany: Sachsen
N
Nicros Offline OP
Newbie
Nicros  Offline OP
Newbie
N

Joined: Mar 2014
Posts: 33
Germany: Sachsen
Oh, yes. Thank you. That was a really beginner mistake.
But why the script crashed in the last loop and not already in the first ?

Re: Problem with dynamic multidimensional arrays [Re: Nicros] #438601
03/18/14 13:55
03/18/14 13:55
Joined: Mar 2011
Posts: 3,150
Budapest
sivan Offline
Expert
sivan  Offline
Expert

Joined: Mar 2011
Posts: 3,150
Budapest
allocation is possible as you want, but filling with wrong values can cause it.


Free world editor for 3D Gamestudio: MapBuilder Editor

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