Gamestudio Links
Zorro Links
Newest Posts
Change chart colours
by 7th_zorro. 05/11/24 09:25
Data from CSV not parsed correctly
by dr_panther. 05/06/24 18:50
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
1 registered members (Ayumi), 662 guests, and 3 spiders.
Key: Admin, Global Mod, Mod
Newest Members
Hanky27, firatv, wandaluciaia, Mega_Rod, EternallyCurious
19051 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
String memory allocation #365436
03/26/11 18:23
03/26/11 18:23
Joined: Jul 2005
Posts: 1,930
Austria
Dark_samurai Offline OP
Serious User
Dark_samurai  Offline OP
Serious User

Joined: Jul 2005
Posts: 1,930
Austria
Hi!

I have a short question regarding to the memory managment of strings:

Code:
while(1)
{
STRING* TestStr = "#100";
//...
wait(1);
}



Would this code blew up my memory?

Thanks for your help!


ANet - A stable and secure network plugin with multi-zone, unlimited players, voip, server-list features,... (for A7/A8)!
get free version
Re: String memory allocation [Re: Dark_samurai] #365441
03/26/11 18:59
03/26/11 18:59
Joined: Jan 2002
Posts: 4,225
Germany / Essen
Uhrwerk Offline
Expert
Uhrwerk  Offline
Expert

Joined: Jan 2002
Posts: 4,225
Germany / Essen
No, this does no do any harm as "#100" is handled like a constant, that is assigned to TestStr. You can do that without problems. Be carefull with
Code:
STRING* TestStr = str_create("#100");

in a while loop though. This will create a memory leak.



Always learn from history, to be sure you make the same mistakes again...
Re: String memory allocation [Re: Uhrwerk] #365448
03/26/11 19:45
03/26/11 19:45
Joined: Jul 2005
Posts: 1,930
Austria
Dark_samurai Offline OP
Serious User
Dark_samurai  Offline OP
Serious User

Joined: Jul 2005
Posts: 1,930
Austria
Thanks for your fast reply!


ANet - A stable and secure network plugin with multi-zone, unlimited players, voip, server-list features,... (for A7/A8)!
get free version
Re: String memory allocation [Re: Dark_samurai] #365466
03/26/11 23:38
03/26/11 23:38
Joined: May 2007
Posts: 2,043
Germany
Lukas Offline

Programmer
Lukas  Offline

Programmer

Joined: May 2007
Posts: 2,043
Germany
But wait, cou can't define a string inside a function like you did above! You have to use Uhrwerk's code. And that would require you to ptr_remove the string each frame or create if before the beginning of the loop.

Re: String memory allocation [Re: Lukas] #365467
03/26/11 23:47
03/26/11 23:47
Joined: Jan 2002
Posts: 4,225
Germany / Essen
Uhrwerk Offline
Expert
Uhrwerk  Offline
Expert

Joined: Jan 2002
Posts: 4,225
Germany / Essen
Of cause you can. Why shouldn't you be able to use a local variable? I didn't find anything that tells not to do this in the manual and it even compiles and runs like a charme...


Always learn from history, to be sure you make the same mistakes again...
Re: String memory allocation [Re: Uhrwerk] #365522
03/27/11 12:01
03/27/11 12:01
Joined: May 2007
Posts: 2,043
Germany
Lukas Offline

Programmer
Lukas  Offline

Programmer

Joined: May 2007
Posts: 2,043
Germany
Doesn't the manual explicitly say somewhere that you can't define engine objects inside a function?? Also if you look at the manual page for strings:
http://manual.conitec.net/astring.htm
"Defines a global string pointer..."
It says GLOBAL there.

This is what I think will happen in the line
STRING* TestStr = "#100";
It defines a local string pointer and it's set to a pointer of a temporary char array. All attempts to use it as a string will most propably cause a crash.

Re: String memory allocation [Re: Lukas] #365525
03/27/11 12:21
03/27/11 12:21
Joined: Jul 2005
Posts: 1,930
Austria
Dark_samurai Offline OP
Serious User
Dark_samurai  Offline OP
Serious User

Joined: Jul 2005
Posts: 1,930
Austria
Hmm... I think we have to ask JCL for clearing that up.


ANet - A stable and secure network plugin with multi-zone, unlimited players, voip, server-list features,... (for A7/A8)!
get free version
Re: String memory allocation [Re: Lukas] #365526
03/27/11 12:21
03/27/11 12:21
Joined: Feb 2011
Posts: 135
Myrkling Offline
Member
Myrkling  Offline
Member

Joined: Feb 2011
Posts: 135
Quote:
This is what I think will happen in the line
STRING* TestStr = "#100";
It defines a local string pointer and it's set to a pointer of a temporary char array. All attempts to use it as a string will most propably cause a crash.

Apparently the Lite-C precompiler creates global STRINGs even for local STRING declarations. So if you have five local STRING declarations in your code, five global STRING objects will be created additionally (you can check this with ptr_first() for example).
I don't know whether using such 'local' strings could cause any problems, though. Probably depends on what "#100" is replaced with. laugh

Re: String memory allocation [Re: Myrkling] #365572
03/27/11 16:17
03/27/11 16:17
Joined: Jan 2002
Posts: 4,225
Germany / Essen
Uhrwerk Offline
Expert
Uhrwerk  Offline
Expert

Joined: Jan 2002
Posts: 4,225
Germany / Essen
"#100" must be replaced with a global String with 100 chars. Otherwise it could not be assigned to TestStr. Of course the string created is global, as you can't have local engine objects. But that does not play a role for the code here. Of course the local variable is valid. You can also have local bitmap or sound pointers. That does not make the corresponding engine objects local.


Always learn from history, to be sure you make the same mistakes again...

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