It is currently Wed May 22, 2013 8:13 pm

All times are UTC + 1 hour




Post new topic Reply to topic  [ 7 posts ] 
Author Message
 Post subject: String Optimizations Issues
PostPosted: Thu May 17, 2012 2:06 pm 
Offline
Enthusiast
Enthusiast

Joined: Fri Jul 14, 2006 8:53 pm
Posts: 588
Location: Malta
Hi guys,

I am trying to speed up a program which parses millions of strings, but sometimes I get a crash

Code:

Protected            *ptr.String 
Protected            *s, *p

If j > 0
      *p = *Buffer + j
      *ptr = @*p
      Debug *ptr\s
      *ptr\s = Chr(0)   ;Crash
       *ptr = @*p
EndIf   




Any idea why?

Now j is verified that is it less than buffer which holds the string

_________________
Brazil, wait for me, here I come...

http://www.en3.biz


Top
 Profile  
 
 Post subject: Re: String Optimizations Issues
PostPosted: Thu May 17, 2012 3:31 pm 
Offline
Addict
Addict

Joined: Sat Dec 31, 2005 5:24 pm
Posts: 2970
Location: Where ya would never look.....
EDIT: Comment removed, mis-read the post

_________________
The advantage of a 64 bit operating system over a 32 bit operating system comes down to only being twice the headache.


Last edited by SFSxOI on Fri May 18, 2012 1:16 pm, edited 1 time in total.

Top
 Profile  
 
 Post subject: Re: String Optimizations Issues
PostPosted: Thu May 17, 2012 3:47 pm 
Offline
Addict
Addict
User avatar

Joined: Mon Jul 25, 2005 3:51 pm
Posts: 2401
Location: Utah, USA
kinglestat wrote:
Hi guys,

I am trying to speed up a program which parses millions of strings, but sometimes I get a crash

Code:

Protected            *ptr.String 
Protected            *s, *p

If j > 0
      *p = *Buffer + j
      *ptr = @*p
      Debug *ptr\s
      *ptr\s = Chr(0)   ;Crash
       *ptr = @*p
EndIf   




Any idea why?

Now j is verified that is it less than buffer which holds the string


When you assign a new string to *ptr\s, *ptr (which was pointing to *p which points to a string at *Buffer + j) may attempt to free the string memory that is pointed to by *p (before allocating new memory to receive the new string being assigned).

In my tests it would seem that because the new string Chr(0) is small it is just overwriting the previous string memory with the new value. If it instead tries to free the string memory and allocate new memory for the string it will cause a crash. I think it does this occasionally and when it does it causes your problem.

It might also occur if *p does not point to *Buffer (j <> 0). If an attempt is made to free the memory then, it would cause it to free memory at an invalid location (not the start) which is in the middle of the memory area pointed to by *Buffer.


If you are simply trying to overwrite the string at *Buffer + j with a #Null try using PokeS(*p, 0) instead. This will avoid any possible attempts to free and reallocate the affected memory.

_________________
Image


Top
 Profile  
 
 Post subject: Re: String Optimizations Issues
PostPosted: Thu May 17, 2012 4:27 pm 
Offline
Enthusiast
Enthusiast

Joined: Fri Jul 14, 2006 8:53 pm
Posts: 588
Location: Malta
@Demivec your logic is sound.
But I wanted to avoid using poke.

Is there any other way I can overwrite part of a string will null without using a library function?

_________________
Brazil, wait for me, here I come...

http://www.en3.biz


Top
 Profile  
 
 Post subject: Re: String Optimizations Issues
PostPosted: Thu May 17, 2012 5:01 pm 
Offline
Addict
Addict
User avatar

Joined: Wed Dec 23, 2009 10:14 pm
Posts: 1385
Location: Boston, MA
Do all parsing in memory and assign to a string only when necessary.
Take care whether you are working in Unicode(2 bytes/char) or not.

_________________
To understand recursion, you must first understand recursion. ~ unknown
I never make stupid mistakes. Only very, very clever ones. ~ John Peel


Top
 Profile  
 
 Post subject: Re: String Optimizations Issues
PostPosted: Thu May 17, 2012 5:03 pm 
Offline
Addict
Addict
User avatar

Joined: Mon Jul 25, 2005 3:51 pm
Posts: 2401
Location: Utah, USA
kinglestat wrote:
Is there any other way I can overwrite part of a string will null without using a library function?

Code:
Protected            *ptr.String 
Protected            *s, *p.Character

If j > 0
  *p = *buffer + j ;j needs to be stepped according to SizeOf(Character)
  *ptr = @*p
  Debug *ptr\s
  ;*ptr\s = Chr(0)   ;Crash
  *p\c = #Null
  ;*ptr = @*p
EndIf

_________________
Image


Top
 Profile  
 
 Post subject: Re: String Optimizations Issues
PostPosted: Thu May 17, 2012 8:21 pm 
Offline
Enthusiast
Enthusiast

Joined: Fri Jul 14, 2006 8:53 pm
Posts: 588
Location: Malta
yes that did the trick
and i should have thought about it myself
thanks for your help

_________________
Brazil, wait for me, here I come...

http://www.en3.biz


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 7 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye