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
2 registered members (TipmyPip, 1 invisible), 18,699 guests, and 8 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
Page 2 of 5 1 2 3 4 5
Re: I need some outsider's perspective... [Re: Carlos3DGS] #416897
02/05/13 19:31
02/05/13 19:31
Joined: Jan 2002
Posts: 4,225
Germany / Essen
Uhrwerk Offline
Expert
Uhrwerk  Offline
Expert

Joined: Jan 2002
Posts: 4,225
Germany / Essen
1. No. Every malloc/sys_malloc requires one free/sys_free. You'd have to take the loop approach.

2. This sets all the 500 array elements of Bwater[j] to the integer value 100.

Last edited by Uhrwerk; 02/05/13 20:01. Reason: The second statement was wrong. Sorry.

Always learn from history, to be sure you make the same mistakes again...
Re: I need some outsider's perspective... [Re: Uhrwerk] #416898
02/05/13 19:47
02/05/13 19:47
Joined: Oct 2008
Posts: 513
Carlos3DGS Offline OP
User
Carlos3DGS  Offline OP
User

Joined: Oct 2008
Posts: 513
Thanks Uhrwerk!

1. Would this free all the allocated memory?
Code:
for(tx=0;tx<1000;tx++)
{
	for(ty=0;ty<500;ty++)
	{
		free((Bwater[tx])[ty]);
	}
}


Would there be a ton of pointers left-over? If so, how would I remove those?



2. I still dont understand "memset( Bwater[j] , (int)100 , (int)500 * sizeof(int) );"
Running the following code testi=1684300900
Code:
int** Bwater = NULL;
int testi;

void test_array()
{
	Bwater = (int**)sys_malloc( (int)1000 * sizeof(int*) );
	int j;
	for (j=0;j<1000;j++)
	{
		Bwater[j] = (int*)sys_malloc( (int)500 * sizeof(int) );
		
		memset( Bwater[j] , (int)100 , (int)500 * sizeof(int) );
	}
	testi=(Bwater[999])[499];
}


anyone know why testi=1684300900 instead of 100?
EDIT:
For anyone just popping in that wants to help, the above code is supposed to be a subsitute for an array "int Bwater[1000][500];"
With a final line at the end to use "testi" to retrieve the value of the last position in the array [999],[499] to test if it works.

Last edited by Carlos3DGS; 02/05/13 20:02.

"The more you know, the more you realize how little you know..."

I <3 HORUS
http://www.opserver.de/ubb7/ubbthreads.php?ubb=showflat&Number=401929&page=1
Re: I need some outsider's perspective... [Re: Carlos3DGS] #416899
02/05/13 20:03
02/05/13 20:03
Joined: Jan 2002
Posts: 4,225
Germany / Essen
Uhrwerk Offline
Expert
Uhrwerk  Offline
Expert

Joined: Jan 2002
Posts: 4,225
Germany / Essen
1. No, that is nonsense. Bwater is a two-dimensional array, not three-dimensional! You don't need to free every element, but every array.

2. Sorry, for the confusion. The second statement in my previous post was wrong. memset works bytewise. So your statement will 0x64 as an unsigned char to the whole memory area. If you want to initialize all array elements you have to use a loop for that unless you want to initialize with 0. But that's what sys_malloc does for you in the first place.


Always learn from history, to be sure you make the same mistakes again...
Re: I need some outsider's perspective... [Re: Uhrwerk] #416900
02/05/13 20:14
02/05/13 20:14
Joined: Oct 2008
Posts: 513
Carlos3DGS Offline OP
User
Carlos3DGS  Offline OP
User

Joined: Oct 2008
Posts: 513
1. Would this do the trick to free all the memory?
Code:
void remove_array()
{
   //REMOVE THE DATA
	//free the data (integers)
	for(tx=0;tx<1000;tx++)
	{
		for(ty=0;ty<500;ty++)
		{
			free((Bwater[tx])[ty]);
		}
	}
	
    //REMOVE THE POINTERS
	//free the colums
	for (ty=0;ty<1000;ty++)
	{
		free(Bwater[j]);
	}
	//free the rows
	free(Bwater);
}



2. Does that mean that I would not need the memset() instruction at all if I want the array to start off as full of zeros? (I want to make sure I understood correctly. I don't want to end up with a huge array of pointers pointing to nothing)

Last edited by Carlos3DGS; 02/05/13 20:16.

"The more you know, the more you realize how little you know..."

I <3 HORUS
http://www.opserver.de/ubb7/ubbthreads.php?ubb=showflat&Number=401929&page=1
Re: I need some outsider's perspective... [Re: Carlos3DGS] #416901
02/05/13 20:26
02/05/13 20:26
Joined: Jan 2002
Posts: 4,225
Germany / Essen
Uhrwerk Offline
Expert
Uhrwerk  Offline
Expert

Joined: Jan 2002
Posts: 4,225
Germany / Essen
1. No, you're thinking too complicated. ^^ There are no pointers to memory areas in the array, just ints. Just use the code below "//REMOVE THE POINTERS". The code above it won't do no good. Also remember to set Bwater to NULL afterwards.

2. When using sys_malloc(!) the memory area allocated is automatically filled with zero bytes. Hence the int array would contain all zeros. Be aware that this is not true for malloc!

Also be careful concerning sys_malloc and free. This will be the direct road to hell. When you allocate with sys_malloc you have to use sys_free. When allocating with malloc you have to use free. Don't mix that!


Always learn from history, to be sure you make the same mistakes again...
Re: I need some outsider's perspective... [Re: Uhrwerk] #416906
02/05/13 20:56
02/05/13 20:56
Joined: Oct 2008
Posts: 513
Carlos3DGS Offline OP
User
Carlos3DGS  Offline OP
User

Joined: Oct 2008
Posts: 513
for anyone else having a similar problem in the future I leave the final results.

1. final version of the remove function
Code:
void remove_array()
{
        int ty;
	for (ty=0;ty<1000;ty++)
	{
		sys_free(Bwater[ty]);
	}
	sys_free(Bwater);
        Bwater=NULL;
}



2. final version of the creating function
Code:
void create_array()
{
        int ty;
	Bwater = (int**)sys_malloc( (int)1000 * sizeof(int*) );
	for (ty=0;ty<1000;ty++)
	{
		Bwater[ty] = (int*)sys_malloc( (int)500 * sizeof(int) );
	}
}



All tested and seems to work fine. Thankyou very much Uhrwerk, Sivan, and Superku!

3. Even after reading the manual for sys_malloc() and malloc() I never really understood the difference/advantages/disadvatages of using one or the other. (The only difference I understand now thanks to you is that one sets all data to "0" and the other dosn't?) I always used malloc() and free() for my linked lists, but withough understanding why. I will just go with sys_malloc() and sys_free() because you all seem to use those. ^^

EDIT:
For anyone else using this in the future I also leave declaration, assignment and retrieval examples below:
Code:
//declaration example
int** Bwater = NULL;
//assignment example
(Bwater[0])[12]=666;
//retrieval example
testi=(Bwater[0])[12];


Last edited by Carlos3DGS; 02/05/13 21:14. Reason: examples for future users

"The more you know, the more you realize how little you know..."

I <3 HORUS
http://www.opserver.de/ubb7/ubbthreads.php?ubb=showflat&Number=401929&page=1
Re: I need some outsider's perspective... [Re: Carlos3DGS] #416907
02/05/13 21:06
02/05/13 21:06
Joined: Jan 2002
Posts: 4,225
Germany / Essen
Uhrwerk Offline
Expert
Uhrwerk  Offline
Expert

Joined: Jan 2002
Posts: 4,225
Germany / Essen
1. There is a serious bug in your first snippet. The index has to be ty, not j!

2. That snippet is perfect.

3. sys_malloc and sys_free are the engine versions of malloc and free. They got several advantages over the original c versions. For example the engine will warn you in the log file if there is memory that was allocated with sys_malloc but has not been freed on engine shutdown. You can also mark memory allocated this way with the sys_mark function. The engine will warn you when you accidentally overwrite such marked memory areas.

Last but not least I guess(!) that sys_malloc may be faster than malloc. A call to malloc will definitely ask the operating system for memory while a call to sys_malloc may very well return memory that has been already allocated by the engine and hence is immediately available.

Also do not underestimate the feature that memory returned by sys_malloc is completely cleared. This can really save your arse, when you forgot to initialize memory.


Always learn from history, to be sure you make the same mistakes again...
Re: I need some outsider's perspective... [Re: Uhrwerk] #416909
02/05/13 21:11
02/05/13 21:11
Joined: Oct 2008
Posts: 513
Carlos3DGS Offline OP
User
Carlos3DGS  Offline OP
User

Joined: Oct 2008
Posts: 513
Thanks for pointing out that mistake. In my test I changed that when I tried to run it and got a warning, but forgot to reflect that change here


"The more you know, the more you realize how little you know..."

I <3 HORUS
http://www.opserver.de/ubb7/ubbthreads.php?ubb=showflat&Number=401929&page=1
Re: I need some outsider's perspective... [Re: Uhrwerk] #416910
02/05/13 21:20
02/05/13 21:20
Joined: Oct 2008
Posts: 513
Carlos3DGS Offline OP
User
Carlos3DGS  Offline OP
User

Joined: Oct 2008
Posts: 513
Originally Posted By: Uhrwerk
A call to malloc will definitely ask the operating system for memory while a call to sys_malloc may very well return memory that has been already allocated by the engine and hence is immediately available.

Yeah, now I remember why I initially decided to use malloc() instead of sys_malloc() when I started to use linked lists...
When I read the manuall (but didnt understand most of it) and saw the part talking about that... It scared me away from sys_malloc() and sys_nxalloc()... I assumed sys_malloc() would be faster but limited by nexus size or something, and that malloc() would be slower but have all your RAM available for use. Would that assumption be true?


"The more you know, the more you realize how little you know..."

I <3 HORUS
http://www.opserver.de/ubb7/ubbthreads.php?ubb=showflat&Number=401929&page=1
Re: I need some outsider's perspective... [Re: Carlos3DGS] #416912
02/05/13 21:26
02/05/13 21:26
Joined: Sep 2003
Posts: 6,861
Kiel (Germany)
Superku Offline
Senior Expert
Superku  Offline
Senior Expert

Joined: Sep 2003
Posts: 6,861
Kiel (Germany)
You should normally always use sys_malloc and sys_free with lite-C, I don't think there is no reason not to use those commands. sys_nxalloc or similar uses memory from the level cache and is faster, but cannot be freed easily and is limited in availability.


"Falls das Resultat nicht einfach nur dermassen gut aussieht, sollten Sie nochmal von vorn anfangen..." - Manual

Check out my new game: Pogostuck: Rage With Your Friends
Page 2 of 5 1 2 3 4 5

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