Gamestudio Links
Zorro Links
Newest Posts
Zorro 2.70
by jcl. 09/29/25 09:24
optimize global parameters SOLVED
by dBc. 09/27/25 17:07
ZorroGPT
by TipmyPip. 09/27/25 10:05
assetHistory one candle shift
by jcl. 09/21/25 11:36
Plugins update
by Grant. 09/17/25 16:28
AUM Magazine
Latest Screens
Rocker`s Revenge
Stug 3 Stormartillery
Iljuschin 2
Galactic Strike X
Who's Online Now
3 registered members (AndrewAMD, TipmyPip, OptimusPrime), 15,359 guests, and 7 spiders.
Key: Admin, Global Mod, Mod
Newest Members
krishna, DrissB, James168, Ed_Love, xtns
19168 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 | 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