Gamestudio Links
Zorro Links
Newest Posts
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
MT5 bridge not working on MT5 v. 5 build 4160
by EternallyCurious. 04/25/24 20:49
Trading Journey
by howardR. 04/24/24 20:04
Zorro FIX plugin - Experimental
by flink. 04/21/24 07:12
Scripts not found
by juergen_wue. 04/20/24 18:51
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
3 registered members (wandaluciaia, AndrewAMD, 1 invisible), 765 guests, and 6 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
Runtime array sizing #446377
10/13/14 11:10
10/13/14 11:10
Joined: Oct 2014
Posts: 42
DEVON
S
scotpip Offline OP
Newbie
scotpip  Offline OP
Newbie
S

Joined: Oct 2014
Posts: 42
DEVON
Hi

I'm trying to define array length at runtime. In C I believe that any integer constant can be used as the array length.

In Lite-C, a #define constant compiles as expected if the value is a primitive integer

Code:
#define MYLENGTH 1
var myarray[MYLENGTH];



But use any kind of expression in the #define and you get a syntax error:

Code:
#define MYLENGTH 1 + 1
var myarray[MYLENGTH];



Even this minimal expression won't compile:

Code:
#define MYLENGTH (1)
var myarray[MYLENGTH];



New to C and to Lite-C so perhaps I'm doing something dim? Any advice much appreciated.

Re: Runtime array sizing [Re: scotpip] #446378
10/13/14 11:35
10/13/14 11:35
Joined: Mar 2011
Posts: 3,150
Budapest
sivan Offline
Expert
sivan  Offline
Expert

Joined: Mar 2011
Posts: 3,150
Budapest
you need sys_malloc and sys_free to define array size runtime and allocate memory for it, and free up memory in the end.

if you later need to enlarge it, you can also use malloc with realloc and free.


Free world editor for 3D Gamestudio: MapBuilder Editor
Re: Runtime array sizing [Re: sivan] #446379
10/13/14 12:43
10/13/14 12:43
Joined: Oct 2014
Posts: 42
DEVON
S
scotpip Offline OP
Newbie
scotpip  Offline OP
Newbie
S

Joined: Oct 2014
Posts: 42
DEVON
Hi sivan

My understanding is that what you're suggesting is the standard way to make a resizable array, and I'll use it if I have to.

But I'm looking for the simplest way to initialise a fixed-size array at runtime. I've seen more than one reputable source on standard C use the approach I tried above:

Code:
#define MYLENGTH (an integer expression)
var myarray[MYLENGTH];



As this seems to be legal in C it would be helpful to understand why it won't compile in Lite-C.

Re: Runtime array sizing [Re: scotpip] #446382
10/13/14 14:30
10/13/14 14:30
Joined: Mar 2011
Posts: 3,150
Budapest
sivan Offline
Expert
sivan  Offline
Expert

Joined: Mar 2011
Posts: 3,150
Budapest
probably simply limited by the compiler, there are several differences exactly known only by the developers...


Free world editor for 3D Gamestudio: MapBuilder Editor
Re: Runtime array sizing [Re: sivan] #446385
10/13/14 17:21
10/13/14 17:21
Joined: Oct 2014
Posts: 42
DEVON
S
scotpip Offline OP
Newbie
scotpip  Offline OP
Newbie
S

Joined: Oct 2014
Posts: 42
DEVON
Thanks sivan - I guess we just have to suck and see.

Found a workaround, so it's more of an academic issue now.

I find that if you look at older C books there are idioms for getting stuff done without the fancier features of the new compilers...


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