Announcement

Collapse
No announcement yet.

memory allocation problems

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

  • PALYGAP
    replied
    I nearly "went mad" yesterday trying to initialize and array of array (string) this way :
    HTML Code:
    // GLOBAL VARIABLES
    var lab1[10], lab2[10], lab3[10], lab4[10], lab5[10], lab6[10], lab7[10], lab8[10], lab9[10], lab10[10];
    var lab11[10], lab12[10], lab13[10], lab14[10], lab15[10], lab16[10], lab17[10], lab18[10], lab19[10], lab20[10];
    var lab21[10], lab22[10], lab23[10], lab24[10], lab25[10], lab26[10], lab27[10], lab28[10], lab29[10], lab30[10];
    var lab31[10], lab32[10], lab33[10], lab34[10], lab35[10], lab36[10], lab37[10], lab38[10], lab39[10], lab40[10];
    
    var controlPage_Labels[40] := [  lab1, lab2, lab3, lab4, lab5, lab6, lab7, lab8, lab9, lab10,
                        lab11 , lab12 , lab13 , lab14 , lab15 , lab16 , lab17 , lab18 , lab19 , lab20 ,
                        lab21 , lab22 , lab23 , lab24 , lab25 , lab26 , lab27 , lab28 , lab29 , lab30 ,
                        lab31 , lab32 , lab33 , lab34 , lab35 , lab36 , lab37 , lab38 , lab39 , lab40 ];and not being able to get anything workingTried the solution sugested here :var controlPage_Labels[40];
    
    main()
        *controlPage_Labels := [  lab1, lab2, lab3, lab4, lab5, lab6, lab7, lab8, lab9, lab10,
                        lab11 , lab12 , lab13 , lab14 , lab15 , lab16 , lab17 , lab18 , lab19 , lab20 ,
                        lab21 , lab22 , lab23 , lab24 , lab25 , lab26 , lab27 , lab28 , lab29 , lab30 ,
                        lab31 , lab32 , lab33 , lab34 , lab35 , lab36 , lab37 , lab38 , lab39 , lab40 ];
    and ..... IT WORKED. I am re-establish in my "sanity". Thanks a lot guys for working it out. But this should definitely be put in the language document in a special section on say "the in and outs of arrays and strings".Cool I can carry on with my task.

    Leave a comment:


  • capricornone
    replied


    Great!

    That's a much safer fix than adding the offset myself.

    As frustrating as this has been, I really do appreciate the extremely quick help. Thanks again!

    -Colin

    Leave a comment:


  • meldavia
    replied


    Actually theres something wrong with the way the compiler is building the startup code,

    Try it this way and it works fine.

    #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];

    func main()
    var i, n;
    *pageTextBuffers := [pageBuffer0,pageBuffer1,pageBuffer2,pageBuffer3,pageBuffer4,pageBuffer5]; //
    // or
    //*pageTextBuffers := [&pageBuffer0[0],&pageBuffer1[0],&pageBuffer2[0],&pageBuffer3[0],&pageBuffer4[0],&pageBuffer5[0]];

    Leave a comment:


  • capricornone
    replied


    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!

    Leave a comment:


  • capricornone
    replied


    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?

    Leave a comment:


  • meldavia
    replied
    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[] := [&pageBuffer0,&pageBuffer1,&pageBuffer2,&pageBuffer3,&pageBuffer4,&pageBuffer 5]; // WORKS BUT IS NOT CORRECT SYNTAX
    //var pageTextBuffers[] := [&pageBuffer0[0],&pageBuffer1[0],&pageBuffer2[0],&pageBuffer3[0],&pageBuffer4[0],&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

    Leave a comment:


  • capricornone
    replied
    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?

    Leave a comment:


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

    &amp;pageBuffer0[0]

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

    Example:a := &amp;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.

    Leave a comment:


  • capricornone
    replied


    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

    Leave a comment:


  • meldavia
    replied


    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];

    Leave a comment:


  • capricornone
    replied


    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.

    Leave a comment:


  • capricornone
    replied


    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.

    Leave a comment:


  • ESPsupport
    replied


    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

    Leave a comment:


  • ESPsupport
    replied


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

    Leave a comment:


  • capricornone
    replied
    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..

    Leave a comment:

Working...
X