Gamestudio Links
Zorro Links
Newest Posts
Executing Trades on Next Bar Open
by vicknick. 06/13/24 08:51
Zorro Beta 2.61: PyTorch
by jcl. 06/10/24 14:42
New FXCM FIX Plugin
by flink. 06/04/24 07:30
AlpacaZorroPlugin v1.3.0 Released
by kzhao. 05/22/24 13:41
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
2 registered members (AndrewAMD, monarch), 1,211 guests, and 10 spiders.
Key: Admin, Global Mod, Mod
Newest Members
AemStones, LucasJoshua, Baklazhan, Hanky27, firatv
19059 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
Page 2 of 2 1 2
Re: Split question. [Re: SurudoiRyu] #218356
07/28/08 08:46
07/28/08 08:46
Joined: Oct 2004
Posts: 4,134
Netherlands
Joozey Offline
Expert
Joozey  Offline
Expert

Joined: Oct 2004
Posts: 4,134
Netherlands

(I could still be wrong in the following post, please correct me in this case)

Quote:
If you declare structures and/or array pointers as local variables, and fill them using
creation functions (eg str_create), does the contents of those pointers get purged on the function
closing/terminating or does the contents become "leaked memory" (as I understand is the case).
Does it vary between pointer types or anything if I am wrong?
I believe it becomes leaked memory, because everything you allocate (using str_create, ent_create or even vector()) is pasted in the memory heap while local variables are stored in the stack.
And according to some random website:
Quote:
  • storage objects in stack storage die (are deallocated) when the stack frame is popped.
  • storage objects in heap storage must be explicitly killed in C, but in other languages are implicitly killed when they are no longer referenced
The former meaning everything within the function's stack frame is killed when the function ends (local variables etc.). The latter one meaning you need to delete the memory manually (implicitly killing memory when there is no reference pointer is called garbage collection, not standard in lite-c). Since allocating memory goes into the heap, you need to kill it manually with free(). So indeed you're missing a free(TempData); in your function smile. (as well as a return type since you return a var).

ref: http://www.cs.jcu.edu.au/Subjects/cp2003/1997/foils/heapAndStack/heapAndStack.html


Gr, Joozey


Click and join the 3dgs irc community!
Room: #3dgs
Re: Split question. [Re: Joozey] #218547
07/29/08 07:55
07/29/08 07:55
Joined: Feb 2008
Posts: 3,232
Australia
EvilSOB Offline
Expert
EvilSOB  Offline
Expert

Joined: Feb 2008
Posts: 3,232
Australia
madBE WARNED! Despite my following comments, this still has SERIOUS memory leaks. mad
mad Im working on it! mad


AT LAST! laugh
Here is the re-write I promised and Ive turned it into a REAL function, like was originally requested.

Ive spent the whole of today on it (at work hehehe wink ), and I was willing to spend the time as I'm sure
to need it myself later as I often use that format to store data in other apps/languages (VB#,C#). crazy

The first block of code is THE Function itself, after much work tired and research smile I believe is close to ready.
Now all I need if some of the experts here to fine tune it a bit.

The Second block is two examples of the code in use, which should be easily copied into a new or existing
project so you can see how it is used. I've supplied an easy version and a "clean" version.

The Function itself if very heavily documented and if I seem to be doing simple things in odd ways, its
just the way I code unfortunately, I seem to have a slightly twisted mind crazy, and Im only a Lite-C noob.

Enjoy and feel free to ask questions or make suggestions if you need.

PS Many thanks Joozey for clarifying my Garbage collection questions. VERY helpful!!
I now know the Garbage Collection Ive built into this function is necessary, if rather unwieldy.

Code:
function string_split(STRING* SourceString)
{	
   if(str_len(SourceString)==0)   return(str_create("0"));      //Invalid SourceString
   //
   char* Delim = ",";       // Delimiter to 'split' around
   STRING* tmpItem;         // Temp String Space used for element building 
   STRING* TempData = str_create(SourceString);   // Temp String used to step through SourceString
   ///Count Number Of Elements 
   var ItemCount = 1;       //Assume 1 already so no trailing comma needed after last
   while(str_len(TempData)>0)
   {	
      ItemCount += str_cmpni(TempData,Delim);      //if this byte is a Delimiter,  increment ItemCount
      str_clip(TempData,1);                        //remove checked character/byte 
   }
   /// Create Results array, with extra element[0] reserved for ItemCount return
   STRING* *ReturnArray = (STRING*)malloc(sizeof(STRING*)*(ItemCount+1));		
   /// Create Each Element in Results Array - Starting at element[1] because 0 is reserved
   TempData = str_create(SourceString);			
   for(cnt=1;cnt<=ItemCount+1;cnt++)
   {	
      tmpItem=str_create(TempData);        //put remaining SourceString proxy into Element
      if(str_stri(tmpItem, _str(Delim))>0)      //look for next Delimiter
      {	
         str_clip(TempData,str_stri(tmpItem, _str(Delim)));   //remove THIS element from SourceString proxy
         str_trunc(tmpItem, str_len(tmpItem) - str_stri(tmpItem, _str(Delim)) + 1);   //Remove Delimiter and beyond
      }
      else   str_cpy(TempData,"");   //Remove rest of SourceString proxy as this element was the last
      ReturnArray[cnt] = str_create(tmpItem);   //copy "temp" item into Results Array at correct element
      str_remove(tmpItem);         //flush used "temp" to avoid memory leak
   }
   //save ItemCount into element[0]. Can be retrieved from Target as follows "XXXXX = str_to_num(MyArray[0]);"
   ReturnArray[0] = str_for_num(TempData,ItemCount);	
   return(ReturnArray);	
}


And here is the sample/demo code.

Code:
//
   //
   //Suggested Clean and Safe Usage      (to my mind anyway)
   ...
   ...
// vvvvvvvv  'STRING**' type is VERY important.	Wont work otherwise.
   STRING**   DataArray =   0;         // LOCALLY defined Result Array Pointer
   var        ArrayLength = 0;         // LOCALLY defined Result Array Length storage
   ...
   ...
   //Valid string from any source
   STRING*   DataList = "Entry1,Entry2,Entry3,Entry4,Entry5,Entry6,Entry7,Entry8";
   ...
   ...
   if(DataArray != 0)   free(DataArray);      //Flush and Free resources from last use if any.
   DataArray = string_split("Entry1,Entry2,Entry3,Entry4,Entry5,Entry6,Entry7,Entry8");
   ArrayLength = str_to_num(DataArray[0]);
   if(ArrayLength == 0) break;   // Nothing in string - Do something about it.
   ...
   ...
   //Do something with Array of Strings.
   ...
   ...
   free(DataArray);   //Release and Free used memory.	
   //
   //
   //
   //
   /////////////////////////////////////////////////////////////////////////////////////////////
   //Simplified Usage
// vvvvvvvv  'STRING**' type is VERY important.	Wont work otherwise.
   STRING** DataArray = string_split("Entry1,Entry2,Entry3,Entry4,Entry5,Entry6,Entry7,Entry8");	
   var ArrayLength = str_to_num(DataArray[0]);
   //
   //
   ///////////// Debugging code - loog in acklog.txt in working folder after running
      diag("\n\nDebug Tag--> 'string_split' has decoded "); 
      diag(str_for_num(tmpStr,ArrayLength));	diag(" elements.\n");
      for(cnt=1;cnt<=ArrayLength;cnt++)
      {
         diag(DataArray[cnt]);	diag(" <-\n");
      }
   ///////////// End of Debugging code
   //
   //Once finished with it empty it as follows
   free(DataArray);
   //
   //
   //



Last edited by EvilSOB; 07/29/08 22:08. Reason: LEAKY MEMORY STILL

"There is no fate but what WE make." - CEO Cyberdyne Systems Corp.
A8.30.5 Commercial
Page 2 of 2 1 2

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