1 registered members (Ayumi),
662
guests, and 3
spiders. |
Key:
Admin,
Global Mod,
Mod
|
|
|
String memory allocation
#365436
03/26/11 18:23
03/26/11 18:23
|
Joined: Jul 2005
Posts: 1,930 Austria
Dark_samurai
OP
Serious User
|
OP
Serious User
Joined: Jul 2005
Posts: 1,930
Austria
|
Hi! I have a short question regarding to the memory managment of strings:
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
Expert
|
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
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
OP
Serious User
|
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: Lukas]
#365467
03/26/11 23:47
03/26/11 23:47
|
Joined: Jan 2002
Posts: 4,225 Germany / Essen
Uhrwerk
Expert
|
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
Programmer
|
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
OP
Serious User
|
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
Member
|
Member
Joined: Feb 2011
Posts: 135
|
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.
|
|
|
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
Expert
|
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...
|
|
|
|