Gamestudio Links
Zorro Links
Newest Posts
Blobsculptor tools and objects download here
by NeoDumont. 03/28/24 03:01
Issue with Multi-Core WFO Training
by aliswee. 03/24/24 20:20
Why Zorro supports up to 72 cores?
by Edgar_Herrera. 03/23/24 21:41
Zorro Trader GPT
by TipmyPip. 03/06/24 09:27
VSCode instead of SED
by 3run. 03/01/24 19:06
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
2 registered members (monk12, Quad), 830 guests, and 4 spiders.
Key: Admin, Global Mod, Mod
Newest Members
sakolin, rajesh7827, juergen_wue, NITRO_FOREVER, jack0roses
19043 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
Page 3 of 3 1 2 3
Re: Three more questions [Re: 3run] #444972
08/24/14 18:21
08/24/14 18:21
Joined: May 2009
Posts: 5,370
Caucasus
3run Offline OP
Senior Expert
3run  Offline OP
Senior Expert

Joined: May 2009
Posts: 5,370
Caucasus
is it just so hard to give me an answer guys?


Looking for free stuff?? Take a look here: http://badcom.at.ua
Support me on: https://boosty.to/3rung
Re: Three more questions [Re: 3run] #444976
08/24/14 19:12
08/24/14 19:12
Joined: Jun 2007
Posts: 1,337
Hiporope and its pain
txesmi Offline
Serious User
txesmi  Offline
Serious User

Joined: Jun 2007
Posts: 1,337
Hiporope and its pain
Hi,
lemming tried to answer you.
Quote:

And you don't need to delete the string pointers as they are on the stack and get deleted when the function ends, but the strings they are pointing to.

The stack is the memory where local variables and function parameters are allocated.

If you create a new engine object and save its memory address in a pointer, there are two actors in the scene: The object and the pointer. All created objects have to be removed, you know. But pointers will probably be local and are automatically managed as simple variables.

Code:
STRING *new_string ( char *text )
{
   STRING *string = str_create ( text );
   return string;
}


'str_create' returns the address of a newly create string and the code saves the address in the pointer. The pointer is local and is allocated into the stack but the string created by 'str_create' is a global object that has to be deleted at the end.

I guess this confusion comes from the literature around liteC. In order to make things easier to understand it is said that the pointer is the object itself. But no, it is not.


Re: Three more questions [Re: txesmi] #444978
08/24/14 20:29
08/24/14 20:29
Joined: May 2009
Posts: 5,370
Caucasus
3run Offline OP
Senior Expert
3run  Offline OP
Senior Expert

Joined: May 2009
Posts: 5,370
Caucasus
Thank you a lot txesmi! This is what I call an answer laugh

So from what I understand, I don't need to care about this local pointers (in this situation STRING*), cause they all are saved in the stack (place where all local stuff and func. parameters are saved) and automatically they will be deleted as soon as function ends, but new string (your example) itself will still exist as it was returned (created) as a global object, right?

F.e. I have an empty global string, I copy results of the function (your example) into it, will it create two objects? The one that returned (created) by the function and that global string that I had from the start? If yes, what would be the best way to delete the one that created by 'new_string'? And do I really need to delete it in order to save some memory?
Code:
str_cpy(globalStr, new_string("Hello?");



greets


Looking for free stuff?? Take a look here: http://badcom.at.ua
Support me on: https://boosty.to/3rung
Re: Three more questions [Re: 3run] #444979
08/24/14 20:51
08/24/14 20:51
Joined: Oct 2011
Posts: 1,082
Germany
C
Ch40zzC0d3r Offline
Serious User
Ch40zzC0d3r  Offline
Serious User
C

Joined: Oct 2011
Posts: 1,082
Germany
Yeah, just do
Code:
str_cpy(globalStr, "Hello?");


And it wont trash your heap.

But if you need to allocate memory on the heap then you HAVE to delete it. If you wont and call it like every frame it will eat your memory in 2 mins xD
Best is to read up on heap and stack and maybe some asm.

Short explenation:
Heap will keep data
Stack will keep data till it calls a return

If you want some bigger explanation I could write a bit, but I guess Sid could also write one of his nice to read posts =D (Enjoy them every time xD)

Last edited by Ch40zzC0d3r; 08/24/14 20:53.
Re: Three more questions [Re: 3run] #444980
08/24/14 21:34
08/24/14 21:34
Joined: Jun 2007
Posts: 1,337
Hiporope and its pain
txesmi Offline
Serious User
txesmi  Offline
Serious User

Joined: Jun 2007
Posts: 1,337
Hiporope and its pain
Originally Posted By: 3run
I have an empty global string, I copy results of the function (your example) into it, will it create two objects? The one that returned (created) by the function and that global string that I had from the start?

Yes. You have two string objects.

Quote:
Code:
str_cpy ( globalStr, new_string("Hello?") );


This code causes the address of the new string get lost because the return of the 'new_string' is not saved so you will not be able to remove the new string.

I think the better choice is to direcly use the new string instead of copying it to another but it depends on the rest of the code xP

Code:
STRING *strResult = new_string ( "hello" );
...
str_remove ( strResult );



Quote:
And do I really need to delete it in order to save some memory?

Yes. Totalmente. Every object has to be deleted before exiting.

Salud!

Re: Three more questions [Re: txesmi] #444993
08/25/14 09:24
08/25/14 09:24
Joined: Mar 2012
Posts: 927
cyberspace
W
Wjbender Offline
User
Wjbender  Offline
User
W

Joined: Mar 2012
Posts: 927
cyberspace
http://www.programmerinterview.com/index.php/data-structures/difference-between-stack-and-heap/

in this example the keyword "new" can be replaced with "malloc" or others ..

I like to think of the stack as privately assigned/managed memory per function , and the heap as global managed/assigned memory ..

if you create something within a function scope that
does not get stored globally (it cannot be accessed outside of this function scope) then it should be taken care of when the function scope runs out ,the local (stack) would be taken care of .

if you can globally access memory created within another function its not on the stack, you can take
care of removing it , however there is no harm in destroying a local created object within the end scope of that function if you feel like it I think.

However using malloc seems to be mentioned by the manual as automaticly taken care of upon exit , but this also does not mean you should ignore such objects unless you need them to exist throughout runtime ..

you should create a function to release memory you
created globally ,so that you may call it whenever you
need to ,sometimes you need to destroy a bunch
of created objects before the application exit e.g when
going to a new level you most likely need to get rid
of some stored data.

I am coming off as confusing most likely but I tried.

what becomes confusing here is the word "create"
because , depending on how the created object gets created makes a difference ,if the called function creates the object on the heap you take care of it , if it is on the stack it wil be taken care of as soon as the function that called the "create function" runs out ,I mention this because not all functions that create objects are written the same eg.

you may find a function that is written to create on the heap but called localy within another function ..

Last edited by Wjbender; 08/25/14 09:59.

Compulsive compiler
Re: Three more questions [Re: Wjbender] #444994
08/25/14 10:22
08/25/14 10:22
Joined: May 2009
Posts: 5,370
Caucasus
3run Offline OP
Senior Expert
3run  Offline OP
Senior Expert

Joined: May 2009
Posts: 5,370
Caucasus
Originally Posted By: txesmi
I think the better choice is to direcly use the new string instead of copying it to another but it depends on the rest of the code xP

Code:
STRING *strResult = new_string ( "hello" );
...
str_remove ( strResult );



Quote:
And do I really need to delete it in order to save some memory?

Yes. Totalmente. Every object has to be deleted before exiting.

Salud!
txesmi@ thank you again man! This explains a lot! laugh

Originally Posted By: Wjbender
http://www.programmerinterview.com/index.php/data-structures/difference-between-stack-and-heap/
I am coming off as confusing most likely but I tried.

what becomes confusing here is the word "create"
because , depending on how the created object gets created makes a difference ,if the called function creates the object on the heap you take care of it , if it is on the stack it wil be taken care of as soon as the function that called the "create function" runs out ,I mention this because not all functions that create objects are written the same eg.

you may find a function that is written to create on the heap but called localy within another function ..
Wjbender@ thank you for the useful link, that will be very useful for me to learn from laugh

I have one more question guys, still about memory issues but this time about structures.
What will be the best way to remove a structure once we don't need it?
I've seen in some examples people use 'free()', is that a good way to go?

Thank you all once again laugh

Greets


Looking for free stuff?? Take a look here: http://badcom.at.ua
Support me on: https://boosty.to/3rung
Re: Three more questions [Re: 3run] #444996
08/25/14 10:58
08/25/14 10:58
Joined: Oct 2011
Posts: 1,082
Germany
C
Ch40zzC0d3r Offline
Serious User
Ch40zzC0d3r  Offline
Serious User
C

Joined: Oct 2011
Posts: 1,082
Germany
if you use new you use delete, if you use malloc you use free.
In lite-c you just use ptr_remove and your fine. Just keep and mind not to use local pointers from stack.

Re: Three more questions [Re: Ch40zzC0d3r] #444997
08/25/14 11:01
08/25/14 11:01
Joined: May 2009
Posts: 5,370
Caucasus
3run Offline OP
Senior Expert
3run  Offline OP
Senior Expert

Joined: May 2009
Posts: 5,370
Caucasus
Ch40zzC0d3r@ sorry, I've missed your post above.. Thank you for informantion anyway laugh

Greets


Looking for free stuff?? Take a look here: http://badcom.at.ua
Support me on: https://boosty.to/3rung
Page 3 of 3 1 2 3

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