PDA

View Full Version : Strings and pointers



wolphi
05-07-2008, 09:42 AM
I am working on a project right now which uses a lot of strings. I just run into some trouble because strings are getting overwriten by other strings (this is at least what I think)
I decided to write a little program to understand how spin allocates memory for strings.

CON
_clkmode = xtal1 + pll16x
_xinfreq = 5_000_000

OBJ

LCD : "LCD_16x2_8bit"

VAR

byte text,text1

PUB main
text:=STRING("0123456789")
text1:=STRING("9876543210")
LCD.START
LCD.MOVEDEC(1,1,@text) ' displays decimal value at position x=1 / y=1 on LCD
LCD.MOVEDEC(1,2,@text1) ' displays decimal value at position x=1 / y=2 on LCD
LCD.MOVESTR(5,1,text) ' displays string at position x=5 / y=1 on LCD
LCD.MOVESTR(5,2,text1) ' displays string at position x=5 / y=2 on LCD

On my LCD I get the following result:

684 0123456789
685 9876543210

The string "text" points to address 684 and "text1" points to address 685 in the memory.
My question shouldn't "text1" overwrite "text"?

stevenmess2004
05-07-2008, 01:55 PM
They are exactly the results I would expect. Your program has a couple of problems.

text and text1 are actually pointers. Therefore, they should be words and not bytes. Fortunately, since this program is short this isn't causing a problem yet. The string function returns the address of the zero terminated string passed to it. If you want to see what the addresses are use LCD.MOVEDEC(1,1,text) instead. The @text is giving you the location of the pointer, not the location of what the pointer is pointing to.

wolphi
05-08-2008, 03:10 AM
Thank you very much for your answer I do understand pointers much better.

I however have another question about strings and memory allocation. In the following code I set the string a with different values. Then I display the propellers memory from address 1 to 300.

CON

_clkmode = xtal1 + pll16x
_xinfreq = 5_000_000


OBJ

text : "vga_text"

VAR

WORD a

PUB start | i,c
a:=STRING("ABC")
a:=STRING("ABCD")
a:=STRING("ABCDE")
a:=STRING("ABCDEF")
a:=STRING("ABCDEFG")
a:=STRING("ABCDEFGH")
a:=STRING("ABC")
a:=STRING("ABCD")
a:=STRING("ABCDE")
a:=STRING("ABCDEF")
a:=STRING("ABCDEFG")
a:=STRING("ABCDEFGH")
text.start(16)
repeat i from 0 to 300
c:=BYTE
if (c>$0D)
text.out(c)

The propellers adds every string into the memory. For example at address 130 I have "ABC" at address 134 I have "ABCD" and so on.
I wrote another program which has the problem that strings are getting overwriten. I am using large arrays and new values are getting assigned to the array many times.
If I assign in the above program a new value to a lets say 1000 times will the propeller not run out of memory? If yes what can I do about it.
I hope I explained my problem in an understandable way. English is my second language...

Thanks

Mike Green
05-08-2008, 03:23 AM
The compiler is not smart enough to try to combine different strings so they use the same memory. If you use the same string twice, you get two copies. The way around this is to define your strings in a DAT section of the progam, give each one a name and use that name (and "@" to get the address). Look in the Propeller manual under the LONG / WORD / BYTE statements in the section on assembly language.

hippy
05-08-2008, 04:44 AM
wolphi said...
In the following code I set the string a with different values ...


I think that's a view / use of terminology you should get away from. The variable 'a' isn't a "string" it's a pointer to what happens to be a string as stevenmess2004 said earlier.

Once it's understood as a pointer not "a string" it becomes much easier to understand that altering 'a' doesn't alter the string pointed to, only alters where 'a' points to.

wolphi
05-08-2008, 09:15 AM
Thank you Mike!! I defined the strings in the DAT section and everything works fine now.

Hippy: I am sorry for using the wrong tems. I new to the propeller and I am learning.