Gamestudio Links
Zorro Links
Newest Posts
contractUpdate() is filtering if >2 rows
by mistery. 11/09/26 12:06
New Zorro version 3.15
by jcl. 11/09/26 09:24
Removing outliers from files
by jcl. 11/04/26 10:30
[MED] Import from FBX (2010) missing!!!
by rayp. 11/03/26 14:33
What are you working on?
by rayp. 11/02/26 13:57
No connection to MT4
by JeyKey II. 10/29/26 09:34
AUM Magazine
Latest Screens
Dorifto samurai
Shadow 2
Rocker`s Revenge
Stug 3 Stormartillery
Who's Online Now
4 registered members (LoT, blobplayintennis, AndrewAMD, 1 invisible), 28,318 guests, and 3 spiders.
Key: Admin, Global Mod, Mod
Newest Members
MaliceTheMaker, Haitao_Ren, JimSimons, julienp, julienper
19248 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