Shop OBEX P1 Docs P2 Docs Learn Events
Strings and pointers — Parallax Forums

Strings and pointers

wolphiwolphi Posts: 8
edited 2008-05-08 02:15 in Propeller 1
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"?

Comments

  • stevenmess2004stevenmess2004 Posts: 1,102
    edited 2008-05-07 06:55
    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.
  • wolphiwolphi Posts: 8
    edited 2008-05-07 20:10
    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 GreenMike Green Posts: 23,101
    edited 2008-05-07 20:23
    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.
  • hippyhippy Posts: 1,981
    edited 2008-05-07 21:44
    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.
  • wolphiwolphi Posts: 8
    edited 2008-05-08 02:15
    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.
Sign In or Register to comment.