Announcement

Collapse
No announcement yet.

memory allocation problems

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • memory allocation problems

    Hi,

    I've tried this code on both the uOLED96 module and the uLCD28-pt both with the GFX firmware loaded, they don't have the exact same response, but both aren't doing what I want.

    What I'm trying/need to do, is store 6 arrays which I can fill up with different strings. Replacing the string stored in the buffer as necessary. I thought this would be pretty straight forward, but I'm running into a lot of strange results when I try this. Here's a very simplified version of my code :

    #platform "GOLDELOX-GFX2"#inherit "4DGL_16bitColours.fnc"#inherit "VisualConst.inc" #constant TEXT_BUFFER_SIZE 4 var pageBuffer0[TEXT_BUFFER_SIZE];var pageBuffer1[TEXT_BUFFER_SIZE];var pageBuffer2[TEXT_BUFFER_SIZE];var pageBuffer3[TEXT_BUFFER_SIZE];var pageBuffer4[TEXT_BUFFER_SIZE];var pageBuffer5[TEXT_BUFFER_SIZE]; var pageTextBuffers[6] := [pageBuffer0,pageBuffer1,pageBuffer2,pageBuffer3,pageBuffer4,pageBuffer5]; func main() gfx_Set(SCREEN_MODE,LANDSCAPE_R); var i:=0; for(i:=0;i

  • #2


    Type putting a pause:


    for(i:=0;i

    Comment


    • #3


      Thanks for the quick reply!

      No change though, same output, no matter how long of a pause I put in. The more I play with it, the more I see that it's a problem with the to(); putstr(); seems to be overwriting memory locations, screwing things up. If I change the size of the buffers, I get different results every time, never the right result though. In addition, if I simply add variables that I don't use to the program, effectively moving memory locations of other things, I get different results. There seems to be something very wrong with the way memory locations/registers are being stored and read from in the pmmc. Not very impressed with these chips so far...

      Comment


      • #4
        I have found one thing out, and that is that :

        to(string_ptr);
        putstr("text");

        will put the "text" at the beginning of the string, and push everything else back. I think what it's doing is actually pushing the data back out of the array, and corrupting the registers behind the last array index. It's hard to tell, but it also may be appending a '\n' after the "text" as well. Very frustrating...

        Is there a way to allocate space on the chip from the start? I really just need to define 6 chunks of memory and be able to read and write to them, seems like a very trivial task, can't believe it's this hard..

        Comment


        • #5


          Did you, perhaps mean to 'play with' the address contained within pageTextBuffers[i], not the address of pageTextBuffers[i]?
          Mark

          Comment


          • #6


            push everything else back
            Nothing gets 'pushed back', the old contents are simply overwritten.

            You may be gettng confused by the error mentioned in the last post
            Mark

            Comment


            • #7


              Nothing gets 'pushed back', the old contents are simply overwritten.
              That's the way I read the documentation as well, unfortunately, it doesn't seem to be the case. Try the following code :

              HTML Code:
              var pageBuffer0 := "buff0";var pageBuffer1 := "buff1";var pageBuffer2 := "buff2";var pageBuffer3 := "buff3";var pageBuffer4 := "buff4";var pageBuffer5 := ""; func main()     gfx_Set(SCREEN_MODE,LANDSCAPE_R);     to(pageBuffer0);    print("BUFF0");    txt_MoveCursor(1,0);    putstr(pageBuffer0);    pause(50);     to(pageBuffer1);    putstr("BUFF1");    txt_MoveCursor(2,0);    putstr(pageBuffer1);    pause(50);     to(pageBuffer2);    putstr("BUFF2");    txt_MoveCursor(3,0);    putstr(pageBuffer2);    pause(50);     to(pageBuffer3);    putstr("BUFF3");    txt_MoveCursor(4,0);    putstr(pageBuffer3);    pause(50);     to(pageBuffer4);    putstr("BUFF4");    txt_MoveCursor(5,0);    putstr(pageBuffer4);    pause(50);     to(pageBuffer5);    putstr("BUFF5");    txt_MoveCursor(6,0);    putstr(pageBuffer5);    pause(50);     repeat    forever
              If indeed putstr was "overwritten", the output would be :

              BUFF0
              BUFF1
              BUFF2
              BUFF3
              BUFF4
              BUFF5

              What I get on the screen is :

              BUFF0
              buff0BUFF1
              buff1BUFF2
              buff2BUFF3
              buff3BUFF4
              buff4BUFF5

              I can't explain how else I could be seeing the original string characters, unless it is not replacing them, but instead shifting them.

              Comment


              • #8


                Did you, perhaps mean to 'play with' the address contained within pageTextBuffers[i], not the address of pageTextBuffers[i]?
                What I take from the documentation in this pseudo-c language is that pageTextBuffers[i] should contain a pointer to another array, as I have specified earlier in the code. The array that it points to should contain the list of characters that I output to it using putstr().

                The bottom line is, regardless of what's wrong with my code, if you needed to have 6 buffers full of text and be able to read and write to those buffers in this 4DGL language, how would you do it? Because apparently I'm doing something wrong.

                Comment


                • #9


                  When you initialize a global array with a sequence, you must use the address operator else the contents of the first element of the array will be used, eg:-

                  var pageTextBuffers[] := [&pageBuffer0,&pageBuffer1,&pageBuffer2,&pageBuffer3,&pageBuffer4,&pageBuffer5];
                  Regards,
                  Dave

                  Comment


                  • #10


                    ah! thank you! I figured it was something like that. I think I tried that a while back, but it threw an error so I didn't think it was right. I definitely think it is though, that makes a lot more sense, but I'm getting this error :

                    Error: Can't use '&' as a initializer value for element 0

                    Comment


                    • #11
                      hmm....actually, I don't think that's right... the only way I can get the '&' to work is if I do :

                      &pageBuffer0[0]

                      and similar to C as well as stated in the documentation (pg.16 of pgm. ref. manual)

                      Example:a := &myArray[0];a := myArray; // same effect as above. The array name without indices// implies an address, same as in the C language.

                      Additionally, if I throw that value into a print function, i.e.

                      pageBuffer0[BUFFER_SIZE];
                      pageBuffer1[BUFFER_SIZE];
                      ...
                      print( [DEC]pageBuffer0,"\n",[DEC]pageBuffer1,"\n"....);

                      I get a list of numbers, the difference of which is equal to the BUFFER_SIZE. So it seems like that's working correctly.

                      Comment


                      • #12
                        Okay, here's something pretty interesting,

                        When I run this code :

                        #platform "GOLDELOX-GFX2"#inherit "4DGL_16bitColours.fnc"#inherit "VisualConst.inc" #constant TEXT_BUFFER_SIZE 10 var pageBuffer0[TEXT_BUFFER_SIZE];var pageBuffer1[TEXT_BUFFER_SIZE];var pageBuffer2[TEXT_BUFFER_SIZE];var pageBuffer3[TEXT_BUFFER_SIZE];var pageBuffer4[TEXT_BUFFER_SIZE];var pageBuffer5[TEXT_BUFFER_SIZE]; var pageTextBuffers[6] := [pageBuffer0,pageBuffer1,pageBuffer2,pageBuffer3,pageBuffer4,pageBuffer5]; func main() gfx_Set(SCREEN_MODE,LANDSCAPE_R); var i:=0; txt_MoveCursor(0,0); print(pageBuffer0,"\n",pageBuffer1,"\n",pageBuffer2,"\n",pageBuffer3,"\n",pageBuffer4,"\n",pageBuffe r5); pause(3000); gfx_Cls(); for(i:=0; i< 6; i++) txt_MoveCursor(i,0); print(pageTextBuffers[i]); next repeat forever endfunc
                        I get for the first output

                        129
                        139
                        149
                        159
                        169
                        179

                        which makes sense, since I have allocated 10 words for each buffer, but when I print out the contents of the pageTextBuffers[i], I get :

                        0
                        20
                        40
                        60
                        80
                        100

                        If I use &amp;pageTextBuffers[i], I get

                        189
                        190
                        191
                        192
                        193
                        194

                        Which also makes sense, since it is declared right after the other arrays, those are the pointers to the other arrays. So you would think, if i did [DEC]*pageTextBuffers[i], I would see the addresses stored in those pointers, but instead I get :

                        0
                        0
                        0
                        0
                        319
                        20

                        Any thoughts?? Have you ever actually done this in your code? In seems like it would be a common thing to do, but maybe not?

                        Comment


                        • #13
                          yes you are correct. Looks like the latest compiler I have has relaxed the need for the array offset and is possibly still buggy,
                          however, it will compile the code correctly...

                          //tested with compiler v2.7.0.4
                          //var pageTextBuffers[6] := [pageBuffer0,pageBuffer1,pageBuffer2,pageBuffer3,pageBuffer4,pageBuffer5]; // SHOULD WORK BUT DOESNT
                          var pageTextBuffers[] := [&amp;pageBuffer0,&amp;pageBuffer1,&amp;pageBuffer2,&amp;pageBuffer3,&amp;pageBuffer4,&amp;pageBuffer 5]; // WORKS BUT IS NOT CORRECT SYNTAX
                          //var pageTextBuffers[] := [&amp;pageBuffer0[0],&amp;pageBuffer1[0],&amp;pageBuffer2[0],&amp;pageBuffer3[0],&amp;pageBuffer4[0],&amp;pageBuffer5[0]]; // WONT ACCEPT BUT SHOULD

                          func main()
                          var i, n;

                          // show buffer addresses
                          for(i:=0;i<6;i++)
                          print(pageTextBuffers[i],"\n");
                          next

                          n:=1111;

                          for(i:=0;i<6;i++)
                          to(pageTextBuffers[i]);
                          print(n);
                          n+=1111;
                          txt_MoveCursor(i+10,0);
                          putstr(pageTextBuffers[i]);
                          next

                          repeat
                          forever

                          Regards,
                          Dave

                          Comment


                          • #14


                            Ah, okay, well it compiles now that I've updated to the most recent versions of everything. Unfortunately, as you pointed, out, it still doesn't work. So basically at this point arrays of pointers don't work? When I print out the values in the array, they are offset correctly from each other (10), but they don't start at the correct number, which should be the address of pageBuffer0. So.... any idea when this will be available?

                            Comment


                            • #15


                              So, I guess this is the fix :

                              instead of calling :

                              pageTextBuffers[i] ...

                              I call :

                              pageTextBuffers[i]+pageTextBuffer0;

                              or

                              #constant pageBuffers $pageTextBuffer0+pageTextBuffers

                              then, pageBuffers[i]

                              That gives me the right offset, and everything works now. So long of course as all my arrays are declared one after the other so the compiler puts them in the "expected" memory location.

                              Looking forward to the fix for this!

                              Comment

                              Working...
                              X