Gamestudio Links
Zorro Links
Newest Posts
zorro 64bit command line support
by 7th_zorro. 04/20/24 10:06
StartWeek not working as it should
by jcl. 04/20/24 08:38
Data from CSV not parsed correctly
by jcl. 04/20/24 08:32
Zorro FIX plugin - Experimental
by jcl. 04/20/24 08:30
folder management functions
by VoroneTZ. 04/17/24 06:52
lookback setting performance issue
by 7th_zorro. 04/16/24 03:08
Zorro FIX plugin - Experimental
by flink. 04/14/24 07:46
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
3 registered members (7th_zorro, Aku_Aku, 1 invisible), 579 guests, and 1 spider.
Key: Admin, Global Mod, Mod
Newest Members
EternallyCurious, howardR, 11honza11, ccorrea, sakolin
19047 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
Page 1 of 2 1 2
Is there a way to define an array bigger than 999999? #466088
05/24/17 14:50
05/24/17 14:50
Joined: Apr 2002
Posts: 1,246
ny
jumpman Offline OP
Serious User
jumpman  Offline OP
Serious User

Joined: Apr 2002
Posts: 1,246
ny
Hello, I'm just learning about the data types in C after some googling, and I'm beginning to understand the limits of var and the advantages of the others like int and double.

So is there a way to define an array in C that is bigger than 999999? I tried:

#define huge 123456789

Int big_boy[huge];

That crashes on startup.

Also, I was able to at one point define this array:

Int nodes[9999999];

This actually worked until I tried saving the game, which I ran out of memory.

Re: Is there a way to define an array bigger than 999999? [Re: jumpman] #466089
05/24/17 15:24
05/24/17 15:24
Joined: Oct 2011
Posts: 1,082
Germany
C
Ch40zzC0d3r Offline
Serious User
Ch40zzC0d3r  Offline
Serious User
C

Joined: Oct 2011
Posts: 1,082
Germany
This might be only true for C/C++ and not necessarily Lite-C since I didnt reverse engineer it yet:
Those arrays get "allocated" depending on where you put the definition to.
If its global it gets "allocated" in the .data or .rdata section of your resulting/compiled PE file.
If the definition is on the stack, this data is directly allocated on the stack.
Theres another way to get memory dynamicly at runtime, which is called malloc.
It can allocate memory on the fly and also free it again, also you dont want the engine to save this array since you can read it yourself from a file if you need it. The engines save function is a way more complex then you might think, it has an internal structure (linked list) to keep track of all objects and there state in the game.

TL;DR:
Arrays are designed for data you know the size of, if the size is by any mean dynamic you have to use malloc/free

Last edited by Ch40zzC0d3r; 05/24/17 15:26.
Re: Is there a way to define an array bigger than 999999? [Re: Ch40zzC0d3r] #466091
05/24/17 15:45
05/24/17 15:45
Joined: Apr 2002
Posts: 1,246
ny
jumpman Offline OP
Serious User
jumpman  Offline OP
Serious User

Joined: Apr 2002
Posts: 1,246
ny
This array is a global array, and by stack, you mean a local array created by an entity, correct?

Re: Is there a way to define an array bigger than 999999? [Re: jumpman] #466093
05/24/17 17:09
05/24/17 17:09
Joined: Aug 2003
Posts: 118
Deutschland
E
Ezzett Offline
Member
Ezzett  Offline
Member
E

Joined: Aug 2003
Posts: 118
Deutschland
There are two important memory areas when you're running your program. One area is called the stack and one area is called the heap. The stack saves everything that is required when a function is called and starts running. All variables that are declared inside of your function are saved on the stack. When another function is called and your current function is still running, this other function data and all it's variables are stored on the stack by stacking it on top of the memory of the first function. Because everything is stacked on top of each other it is called the stack memory. When a function is done the memory the function has used on the stack is freed. The idea of the stack is, that only the memory of the function on the top of the stack can be freed. Everything below this memory has to wait. And you can only put new needed memory on top of the stack. This is called the last-in-first-out principle.

When you're running the function with this huge array all the memory that is needed for this array is occupied on the stack. If the stack is full no other memory area on top of the stack is free to store more data. You'll get a stack overflow and the program crashes.

The stack is small. With Acknex you can place about 10 000 functions on top of each other until the stack is full. If you use large arrays this will happen even faster.
You need another memory area to store such huge data. This area is called the heap. Gamestudio can use about 1.5 GB of your system memory for its heap. To save data on the heap you need to use special functions. For arrays use the function called malloc.

Acknex provides a special version of malloc called sys_malloc:

int* nodes;

nodes = sys_malloc(huge * sizeof(int) ); //needs about 470 MB

Re: Is there a way to define an array bigger than 999999? [Re: Ezzett] #466098
05/24/17 18:15
05/24/17 18:15
Joined: Apr 2002
Posts: 1,246
ny
jumpman Offline OP
Serious User
jumpman  Offline OP
Serious User

Joined: Apr 2002
Posts: 1,246
ny
Wow.... thank you Ezzett, that was really helpful and informative. So the heap is like a bigger contiguous storage space? This is much safer to use, correct?

why Does that int, with sys_malloc require 470 mb? thats huge! When do I use sys_free? Before the game exits?

Re: Is there a way to define an array bigger than 999999? [Re: jumpman] #466099
05/24/17 19:04
05/24/17 19:04
Joined: Aug 2003
Posts: 118
Deutschland
E
Ezzett Offline
Member
Ezzett  Offline
Member
E

Joined: Aug 2003
Posts: 118
Deutschland
One integer needs 4 bytes of memory. You allocate 123456789 integers. This are 4 bytes * 123456789 = 493 827 156 bytes. If you calculate 1 MB = 1024 KB and 1 KB = 1024 bytes this will be around 470 MB.

The heap doesn't need to be contiguous. It is provided by the operating system. Gamestudios nexus, which is also saved on the heap, should be contiguous. This helps to avoid memory fragmentation. Memory fragmentation can be a big problem. Your huge array is also allocated in one contiguous block of heap memory. If you have enough free system memory (let's say 1 GB is unused) but this free memory is fragmented in many small blocks (and each block is smaller than 470 MB) then you can't allocate the memory for this array. sys_malloc will return NULL if it couldn't allocate memory on the heap.

Other problems are memory leaks. You have to free the heap memory you used if you don't need it anymore. Maybe before exiting the game or maybe earlier. It depends on how long you will need the data stored in it.

sys_free (nodes);
nodes = NULL;

If you don't free the memory area and you lose/change your nodes-pointer to this memory area you can't use this unfreed area for future memory allocation. You'll need to restart your game to get the memory back.

If your game is closed all allocated memory is freed by Windows. You don't need to free memory before closing the engine, but it's a VERY BAD practice to do this. Maybe you'll never find a memory leak if you don't free memory because you count on Windows help to do this.

Therefore heap memory is not safer to use.

Re: Is there a way to define an array bigger than 999999? [Re: Ezzett] #466115
05/25/17 16:42
05/25/17 16:42
Joined: Apr 2002
Posts: 1,246
ny
jumpman Offline OP
Serious User
jumpman  Offline OP
Serious User

Joined: Apr 2002
Posts: 1,246
ny
Thank you Ezzett, That was amazing. I looked all over my script, and saw many huge Var arrays that I made "just in case i wanted 10k entities to store vars each" and didnt realize many of these huge arrays were taking up megs!! I shortened the arrays after laugh

I tried:

int* nodes;

nodes = sys_malloc(huge * sizeof(int) ); //needs about 470 MB

and GS gave me an error on compile saying:

Wrong type P_MUL:POINTER:LONG:POINTER

Re: Is there a way to define an array bigger than 999999? [Re: jumpman] #466117
05/25/17 18:06
05/25/17 18:06
Joined: Aug 2003
Posts: 118
Deutschland
E
Ezzett Offline
Member
Ezzett  Offline
Member
E

Joined: Aug 2003
Posts: 118
Deutschland
Hmm, it works for me. It looks like you are multiplying a pointer variable with a long variable. Of what type is huge?

Re: Is there a way to define an array bigger than 999999? [Re: Ezzett] #466120
05/25/17 19:45
05/25/17 19:45
Joined: Apr 2002
Posts: 1,246
ny
jumpman Offline OP
Serious User
jumpman  Offline OP
Serious User

Joined: Apr 2002
Posts: 1,246
ny
My mistake. The below snippet doesnt give me errors. Did i do this right? I wanted to make it the maximum value:

Code:
int* mega_nodes;
mega_nodes = sys_malloc(2147483647*sizeof(int));



I did that not inside a function or anything, just where the other vars are declared. After doing this, can i do the following?

mega_nodes[2147483647] = 2147483647?

which is to go to the last slot and give it a value.


A question:

1. 2147483647 x 4 = 8589934588 bytes, which comes out to 8.589 gigs! This is allocated in the GS Nexus, correct? I dont notice an increase in MB in the nexus statistics panel. I duplicated the sys_malloc with multiple different ints, each to the max int value, and my memory usage nexus-wise hasnt increased. Is this because nothing was actually stored in the array, and its just marking this memory area as "DO NOT TOUCH" for any other memory calls?

Re: Is there a way to define an array bigger than 999999? [Re: jumpman] #466121
05/25/17 20:47
05/25/17 20:47
Joined: Oct 2011
Posts: 1,082
Germany
C
Ch40zzC0d3r Offline
Serious User
Ch40zzC0d3r  Offline
Serious User
C

Joined: Oct 2011
Posts: 1,082
Germany
How do you expect a 32 bit process to be able to allocate 8gb data?
How would you address the memory when a 32 bit app can address a maximum of 4gb?
This obviously wont work with such high numbers. As pointed out before the maximum you can allocate is arorund 1.6gb

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