Forum - MCS Electronics

 

FAQFAQ SearchSearch RegisterRegister Log inLog in

Memory overflow

 
Post new topic   Reply to topic    www.mcselec.com Forum Index -> BASCOM-AVR
View previous topic :: View next topic  
Author Message
dk_akj

Bascom Member



Joined: 14 Sep 2004
Posts: 94

blank.gif
PostPosted: Fri May 10, 2019 9:44 pm    Post subject: Memory overflow Reply with quote

Hi,

Having a strange problem with my program, think its a memory overflow somewhere.

If I have these variables:
Dim v1 As byte
Dim v2 As byte
dim v3 as string * 10
dim v4 as string * 10

if v1 is assigned a value above 255, will it always make overflow into v2?
if v3 is asigned "hello bascom" will that overflow into v4
or is it random the overflow goes?

Hope this makes sense, difficult to explain for me Smile

Best regards
Anders

(BASCOM-AVR version : 2.0.8.1 )
Back to top
View user's profile MSN Messenger
AdrianJ

Bascom Expert



Joined: 16 Jan 2006
Posts: 2444
Location: Queensland

australia.gif
PostPosted: Sat May 11, 2019 12:31 am    Post subject: Reply with quote

You will get compiler errors if you just attempt to do something like
Code:

v1 = 300

v3 = "a123456789bcdef"
 


The first will cause error 49 - "Variable does not fit into byte"
the second will cause error 119 - "constant too big to fit in string"

However you will not get a compiler error if you do something like

Code:

Dim b1 As byte
Dim b2 As byte
Dim b3 as byte

dim b4 as byte

dim s5 as string * 10
dim s6 as string * 10
dim s7 as string * 10
dim s8 as string * 10



b1 = 200
b2 = 200
b3 = b1 + b2

print "b3 ";b3
print "b4 ";b4

s5 = "a12345678"
s6= "b12345678"

s7 = s5 + s6
print "s7 ";s7
print "s8 ";s8
 


Note that I have prefixed the names with indicators to tell me what the variable type is, b for byte, s for string
This means nothing to the compiler, but a lot to you when you are looking at the code later and wondering what type each variable is !

The numerics will work as expected:
b3 will end up with the value of 144, which is 400 truncated to 8 bits ( 1 byte )
b4 will not be affected - left at zero

The strings however will do really bad things
You wil get s7 = "a12345678b12345678"
and s8 = "2345678"

ie s7 will get the full concatenated string, and s8 will get the piece of that starting at the 11th position in s7 ! If you read up about how strings are formed and used in the Bascom Help you may see why this happens.

A good way of exploring this is to use the Bascom simulator, and try things out.

You also should always put a $regfile and the stack allocations in your code and examples, else no one knows what processor you are setting up for, and it matters ! Something like:
Code:

$regfile = "M64def.dat"
$Crystal=4000000
$hwstack=40
$swstack=16
$framesize=32
 


Its quite likely that what you are seeing as "memory overflow" is in fact stack overflow, by not setting the $swstack and $framesize allocations big enough. This can cause all sorts of wierd errors, and even processor crashes and restarts.

_________________
Adrian Jansen
Computer language is a framework for creativity
Back to top
View user's profile Visit poster's website
dk_akj

Bascom Member



Joined: 14 Sep 2004
Posts: 94

blank.gif
PostPosted: Sat May 11, 2019 9:34 am    Post subject: Reply with quote

Hi Adrian,

Thanks for taking time to reply Smile

I have the regfile and hw stack in code:

$regfile = "m128def.dat"
$crystal = 3686400
$swstack = 512
$hwstack = 512
$framesize = 100

Code is for a GSM remote, using a lot of strings up to ~150 chars thats why I have those big stacks, isnt that correct when working with long strings and subs HW and SW stack should be big?

I'll continue chasing the root cause of my problem Smile

Thanks again
Anders
Back to top
View user's profile MSN Messenger
albertsm

Administrator



Joined: 09 Apr 2004
Posts: 4723
Location: Holland

blank.gif
PostPosted: Sat May 11, 2019 2:33 pm    Post subject: Reply with quote

have a look at the code explorer info. it can show the stack space. it is a minimum. but it gives an idea.
all temp space goes in the the framespace so that should be large enough.

so this :

Code:
call sub1
end

sub sub1()
  local s as string * 100
  call sub2
end sub

sub sub2()
  local s as string * 50
  'HERE the frame uses 100 + 50 is 150
end sub
 


will use already 150 bytes.

the code explorer will make this visible.

_________________
Mark
Back to top
View user's profile Visit poster's website
MWS

Bascom Member



Joined: 22 Aug 2009
Posts: 1667

blank.gif
PostPosted: Sat May 11, 2019 2:36 pm    Post subject: Reply with quote

dk_akj wrote:
I'll continue chasing the root cause of my problem

The problem sits in front of your screen and is difficult to chase.
Quote:
if v1 is assigned a value above 255, will it always make overflow into v2?
if v3 is asigned "hello bascom" will that overflow into v4

If you don't know the difference how a byte overturns and an array overflows, I'd say: back to basics.
Back to top
View user's profile
dk_akj

Bascom Member



Joined: 14 Sep 2004
Posts: 94

blank.gif
PostPosted: Sun May 12, 2019 10:28 am    Post subject: Reply with quote

Mark, thanks for the tips!

MWS, wish I was as clever as you....

Best regards
Anders
Back to top
View user's profile MSN Messenger
MWS

Bascom Member



Joined: 22 Aug 2009
Posts: 1667

blank.gif
PostPosted: Sun May 12, 2019 11:08 am    Post subject: Reply with quote

dk_akj wrote:
MWS, wish I was as clever as you....

Not sure if learning the basics of owns work can be already qualified as 'clever', however not doing so in times when such information is widely and easily available, can be qualified as lazy.
Back to top
View user's profile
Display posts from previous:   
Post new topic   Reply to topic    www.mcselec.com Forum Index -> BASCOM-AVR All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
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
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum