Gamestudio Links
Zorro Links
Newest Posts
loading historical data 1st time
by AndrewAMD. 04/14/23 12:54
Trade at bar open
by juanex. 04/13/23 19:43
Bug in Highpass2 filter
by rki. 04/13/23 09:54
Adding Limit Orders For IB
by scatters. 04/11/23 16:16
FisherN
by rki. 04/11/23 08:38
AUM Magazine
Latest Screens
SHADOW (2014)
DEAD TASTE
Tactics of World War I
Hecknex World
Who's Online Now
4 registered members (fogman, Grant, AndrewAMD, juanex), 989 guests, and 8 spiders.
Key: Admin, Global Mod, Mod
Newest Members
rki, FranzIII, indonesiae, The_Judge, storrealba
18919 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
Initializing and deleting structs and variables #404233
07/05/12 12:01
07/05/12 12:01
Joined: Apr 2006
Posts: 159
Latvija
Arrovs Offline OP
Member
Arrovs  Offline OP
Member

Joined: Apr 2006
Posts: 159
Latvija
I readed much from manual, but got really confused.
So - if i use ptr_remove() - im deleting object(atleast i hope so).
so if im deleting my_struct im deleting all sub elements and then object itself - hov exatcly i can delete struct itself and variables in it? I mean to release all struct elements allocated memory and get to pointers understand theres no object anymore in and to do it in right manner.

And im really confused about TEXT strings. If im making text vithout any string and then adding one like my_text.pstring = my_string;
it should be pointer to string and so it should delete together vith text if im deleting its texts.(At least im thinking so)
But in manual im very conserned about str_cpy((my_text.pstring)[i], my_string);
it should copy content from string to my_text string and vhen im removing TEXT vith it strings it should left my_string alone.
I really hope it vorks like that, not point to my used my_string and deletes it too.

Already big thanks if any can make things brighter.

Last edited by Arrovs; 07/05/12 12:04.

Arrovs once will publish game
Re: Initializing and deleting structs and variables [Re: Arrovs] #404239
07/05/12 14:26
07/05/12 14:26
Joined: Jul 2005
Posts: 187
L
lostzac Offline
Member
lostzac  Offline
Member
L

Joined: Jul 2005
Posts: 187
If I understand you you want to delete a custom struct ?
if so use free(YOUR_STRUCT);

as for the Text thing I am not sure I understand you but I think should help you with it.

STRING* myString;
STRING* txtString;

TEXT* txt_object =
{
string(txtString);
}

to copy the contents of string to the struct

str_cpy((txt_object .pstring)[0], myString);

This way when you delete the text it will not effect your string


John C Leutz II

Re: Initializing and deleting structs and variables [Re: lostzac] #404240
07/05/12 14:51
07/05/12 14:51
Joined: Apr 2006
Posts: 159
Latvija
Arrovs Offline OP
Member
Arrovs  Offline OP
Member

Joined: Apr 2006
Posts: 159
Latvija
Small example:

typedef struct Struct
{
PANEL* panel;
int number;
}Struct;

function main()
{
//initialization
Struct* mystruct = malloc(sizeof(Struct));
mystruct.panel = pan_create("bmap = blabla", 1);
mystruct.number = 5;
//deleting
ptr_remove(mystruct.panel);
free(mystruct);
}

So this should delete all created content?
Experienced struct users could give advices.


Arrovs once will publish game
Re: Initializing and deleting structs and variables [Re: Arrovs] #404242
07/05/12 15:47
07/05/12 15:47
Joined: Jul 2005
Posts: 187
L
lostzac Offline
Member
lostzac  Offline
Member
L

Joined: Jul 2005
Posts: 187
Yes I believe so


John C Leutz II

Re: Initializing and deleting structs and variables [Re: lostzac] #404243
07/05/12 16:18
07/05/12 16:18
Joined: Nov 2002
Posts: 913
Berlin, Germany
S
SchokoKeks Offline
User
SchokoKeks  Offline
User
S

Joined: Nov 2002
Posts: 913
Berlin, Germany
The "small example" you gave above is perfectly right. However, the manual advices you to use "sys_malloc/sys_free" instad of "malloc/free", because the sys_-functions are engine functions with advanced error handling. You can then also use sys_marker for debugging.


As for your text question: check the manual for txt_create :

Quote:
Removing a text (with ptr_remove) does not automatically remove its strings (because the engine does not know if the strings were replaced at runtime). If this is desired, use ptr_remove with a loop over the strings of the text (see example) .
.
The example on that page tells you how you can remove the strings from memory

Re: Initializing and deleting structs and variables [Re: SchokoKeks] #404245
07/05/12 17:10
07/05/12 17:10
Joined: Apr 2006
Posts: 159
Latvija
Arrovs Offline OP
Member
Arrovs  Offline OP
Member

Joined: Apr 2006
Posts: 159
Latvija
About strings - i just readed this part from manual and got curious about this:

STRING* mystring = "Some tekst";
TEXT* mytext = txt_create(0, 1);//0 strings, 1. layer
(mytext.pstring)[0] = mystring;//it vorks, but i have no idea if it nov is
//removable string from text
str_cpy((mytext.pstring)[0], mystring);//gives error - possible because zere isnt
//any text strings in vhich to copy
TEXT* mytext1 = txt_create(1, 1);
(mytext1.pstring)[0] = mystring;//it vorks too, but i have no idea if it
//replaces created string or inserts it. So here question is if im not filling
//memory vith pointer lost strings
str_cpy((mytext1.pstring)[0], mystring);//i assume this copy content from
//second string to first created one
//And about zis again:
(mytext1.pstring)[0] = mystring;
//So in case im deleting strings from situation above
ptr_remove((mytext1.pstring)[0]));//is it removes string from text nov or
//defined mystring
======
Hov you see i got very much questions in STRING TEXT case.


Arrovs once will publish game
Re: Initializing and deleting structs and variables [Re: Arrovs] #404246
07/05/12 17:17
07/05/12 17:17
Joined: Apr 2006
Posts: 159
Latvija
Arrovs Offline OP
Member
Arrovs  Offline OP
Member

Joined: Apr 2006
Posts: 159
Latvija
Ou and about structs i found this case too:
typedef struct
{
//some other info
Struct next_struct;
}Struct;

//im assuming this one creates vhole struct not just pointer like - int a;
Struct struktura;
//I feel this could lead to error if nov nex_struct is auto created because its
//vhole struct (not initializable pointer) - im just confused, but i could hear
//good explanation about object structs because im not vorked vith zem like that.


Arrovs once will publish game
Re: Initializing and deleting structs and variables [Re: Arrovs] #404255
07/05/12 19:24
07/05/12 19:24
Joined: Apr 2006
Posts: 159
Latvija
Arrovs Offline OP
Member
Arrovs  Offline OP
Member

Joined: Apr 2006
Posts: 159
Latvija
sys_malloc and sys_free do nice job - although i exatcly dont knov hov much it frees, but i suppose all objects and pointers need manually.
Vorst part here is pointer inability to understand about unexisting object. Its almost impossible to all pointers set to NULL after freeing object.
Garbage collector should be must item in A9.
=============================
About Text part - im not get clever.


Arrovs once will publish game
Re: Initializing and deleting structs and variables [Re: Arrovs] #404271
07/05/12 22:54
07/05/12 22:54
Joined: Nov 2002
Posts: 913
Berlin, Germany
S
SchokoKeks Offline
User
SchokoKeks  Offline
User
S

Joined: Nov 2002
Posts: 913
Berlin, Germany
Please use code-tags on your program code, it makes it much more readable. Also, don't post several times after you wrote something, wait for replys or edit your first post.

So lets go through this

Code:
About strings - i just readed this part from manual and got curious about this:

STRING* mystring = "Some tekst";
TEXT* mytext = txt_create(0, 1);//0 strings, 1. layer
(mytext.pstring)[0] = mystring;//it vorks, but i have no idea if it nov is
//removable string from text
str_cpy((mytext.pstring)[0], mystring);//gives error - possible because zere isnt


Not okay. (mytext.pstring)[0] is not initialized.
use txt_addstring(mytext, mystring); instead.

Code:
//any text strings in vhich to copy
TEXT* mytext1 = txt_create(1, 1);
(mytext1.pstring)[0] = mystring;//it vorks too, but i have no idea if it
//replaces created string or inserts it.


Will not crash, BUT will replace an automatically created string. This string is no longer available and this is a memory leak because you can never ptr_remove it.

Code:
So here question is if im not filling
//memory vith pointer lost strings
str_cpy((mytext1.pstring)[0], mystring);//i assume this copy content from
//second string to first created one


This is a good way to do it.

Code:
//And about zis again:
(mytext1.pstring)[0] = mystring;
//So in case im deleting strings from situation above
ptr_remove((mytext1.pstring)[0]));//is it removes string from text nov or
//defined mystring


This will remove the defined mystring. as i said above the originaly created string from the text is no longer available.


Code:
typedef struct
{
//some other info
Struct next_struct;
}Struct;



This is okay, "next_struct" becomes a part of "Struct" and when you free a Struct object, it also frees the next_struct part.
BUT for every Pointer in next_struct, you have to create and also remove it.


You should write constructors and destructors for your own structs, example:

Code:
typedef struct {
  int* dyn_array;
  int dyn_array_size;
  STRING* str;
  PANEL* pan
} mystruct;

mystruct* mystruct_create(int arraysize)
{
  mystruct *s = sys_malloc(sizeof(mystruct));
  s.dyn_array_size = arraysize;
  s.dyn_array = sys_malloc(sizeof(int) * arraysize);

  s.str = str_create("lol");
  s.pan = pan_create("....", 1);

  return s;
}

void mystruct_delete(mystruct *s)
{
  sys_free (s.dyn_array);
  ptr_remove(s.str);
  ptr_remove(s.pan);

  // remove the struct memory itself
  sys_free(s);
}



Only create and remove the struct with this, and you are pretty safe. Remember to use sys_free when you used sys_malloc, and ptr_remove for the engine *_create functions!

Garbage collector will probably never be added, because lite-C is based on the C language, and it doesn't have a garbage collector too.


Re: Initializing and deleting structs and variables [Re: SchokoKeks] #404272
07/05/12 23:24
07/05/12 23:24
Joined: Apr 2006
Posts: 159
Latvija
Arrovs Offline OP
Member
Arrovs  Offline OP
Member

Joined: Apr 2006
Posts: 159
Latvija
Thanks you made things vhole bunch clearer.
Interesting example vith int array.
===
This all vorks great - only bad part is from pointers vhich dont knov about freed memory.
Code:
Removing an object does not automatically set its pointers to NULL. The safe_remove macro (defined in acknex.h) removes an object and delectes its pointer to prevent further access to it.


One area can have multiple pointers, but it is removing just pointer vhich is given to object removing.
Its the same as:
Code:
Mystruct* mystruct = nev(Mystruct);
Mystruct* mystruct1 = mystruct;
sys_free(mystruct);mystruct = NULL;


Although i have in mind some control methods i should test them in code;
===
If any knov good solution to zis give ansver belov :-)


Arrovs once will publish game

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