September 26, 2022, 07:26:31 AM

News:

Own IWBasic 2.x ? -----> Get your free upgrade to 3.x now.........


My testclass crash

Started by Techno, March 31, 2011, 02:54:59 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Techno

Best support

I try to execute the test program for testing the class.
The windows XP crash if when I run the application.
What have I done wrong?
Intent is the declaration and implementation of the class separately to keep
Here post I the 3 seprate files
Please can someone help what is wrong

Best regards
Stephane

'================================================================'
' Declaration of the class                                                                                   '
'================================================================'

class clsDynString
private
pointer pstr
uint allocsize
uint s_length
public
declare DynString( )
declare _DynString( )

declare set( DynString s )
declare setz( string s )

declare append( DynString s )
declare appendz( string s )

declare c_str( ), string
declare length( ), int
end class


'================================================================'
' Implemantation of the class members class                                                                                   '
'================================================================'


$include "clsDynString.inc"

sub DynString::DynString( )
pstr = new(char, 1)
allocsize = 1
s_length = 0
end sub

sub DynString::_DynString( )
delete pstr
end sub

sub DynString::set( DynString s )
setz( s.c_str( ) )
end sub

sub DynString::setz( string s )
uint slen : slen = len( s )

if slen = 0 then
*<char>pstr[0] = 0
s_length = 0
end if

if slen >= allocsize then
allocsize = slen + 1
delete pstr
pstr = new( char, allocsize )
end if

*<string>pstr = s
s_length = slen
end sub

sub DynString::append( DynString s )
appendz( s.c_str( ) )
end sub

sub DynString::appendz( string s )
uint slen : slen = len( s )

if slen + s_length >= allocsize then
if 2 * allocsize > slen then _
allocsize *= 2 _
else _
allocsize += slen * 2

pointer temp : temp = new( char, s_length + 1 )
*<string>temp = *<string>pstr
delete pstr
pstr = new( char, allocsize )
*<string>pstr = *<string>temp
delete temp
end if

*<string>pstr += s
s_length += slen
end sub

sub DynString::c_str( )
return *<string>pstr
end sub

sub DynString::length( )
return s_length
end sub





'=========================================================='
'Test the class                                            '
'=========================================================='
'$include "clsDynString.inc"
clsDynString dstr
dstr.setz( "Hello" )
print dstr.length( ), dstr.c_str( )
dstr.appendz( " World" )
print dstr.length( ), dstr.c_str( )


sapero

Wrong is that you have undefined symbol DynString. From the implementation we know that it must be a class, but the class is not defined.

After renaming clsDynString to DynString (in all files), your program compiles and does not crash.

BJ

Nice code!  One suggestion for greater speed when there's lots of appending.

In sub DynString::appendz( string s )

Change
*<string>pstr += s
to
*<string>(pstr+s_length) = s

Techno

Quote from: sapero on March 31, 2011, 05:25:08 AM
Wrong is that you have undefined symbol DynString. From the implementation we know that it must be a class, but the class is not defined.

After renaming clsDynString to DynString (in all files), your program compiles and does not crash.

Techno

I have no access to my zip file !
strange

LarryMc

April 07, 2011, 04:25:53 PM #5 Last Edit: April 07, 2011, 04:27:47 PM by LarryMc
Quote from: Techno on April 07, 2011, 03:21:19 PM
I have no access to my zip file !
strange
what do you mean?
are you talking about the zip file I posted that was lost when the forums had to be reloaded this week?

LarryMc
Larry McCaughn :)
Author of IWB+, Custom Button Designer library, Custom Chart Designer library, Snippet Manager, IWGrid control library, LM_Image control library