• AVR Freaks

Hot!contiguous variable allocation on pointer array

Author
vserena82
New Member
  • Total Posts : 13
  • Reward points : 0
  • Joined: 2012/04/28 02:53:35
  • Location: 0
  • Status: offline
2020/01/16 08:44:37 (permalink)
0

contiguous variable allocation on pointer array

Hello,
 
I have a pointer array
char *menu_list[ ] = { 
        // Menu 0
        "abc",
        "def",
        "ghi",
         "#1", // start menù 1
        "menu1.1",
        "menu1.2",
        "menu1.3",
        // Menu 2
        "#2", // start menù 2
        "menu2.1",
        "menu2.2",
        "menu2.3",
        // Menu 3
        "#3", // start menù 3
        "menu3.1",
        "menu3.2",
        "menu3.3",
        "menu3.4",
    };
:
 
I need the elements of this array to be saved contiguously because I have to make parsing to manage menu on LCD.
 
I noticed that array elements are not stored contiguously on flash: whent I run
LCD_write("var1 = %c",L1,0, *(menu_list[0]));
LCD_write("var1 = %c",L1,0, *(menu_list[0]+1));
LCD_write("var1 = %c",L1,0, *(menu_list[0]+2));
LCD_write("var2 = %c",L2,0, *(menu_list[0]+3));
LCD_write("var3 = %c",L3,0, *(menu_list[0]+4));
LCD_write("var4 = %c",L4,0, *(menu_list[0]+5));

it prints correct only characters of first element but after it prints wrong characters.
 
I was thinking that (in this case)
(menu_list[0]+3)  is the same character of (menu_list[1])
 
There is a way to declare *menu_list contoguous?
 
Thank you
 
 
#1

13 Replies Related Threads

    NKurzman
    A Guy on the Net
    • Total Posts : 18266
    • Reward points : 0
    • Joined: 2008/01/16 19:33:48
    • Location: 0
    • Status: online
    Re: contiguous variable allocation on pointer array 2020/01/16 09:25:44 (permalink)
    0
    Why would you expect that they would?
    The Pointers are consecutive.
    If you want the letters in order than you would need a 2D array (but that will not do what you want either.
    Or
    allocate each item by address yourself.
    Or
    Code what you want to do in a different way, that uses the pointers as they were designed.
    #2
    pcbbc
    Super Member
    • Total Posts : 1507
    • Reward points : 0
    • Joined: 2014/03/27 07:04:41
    • Location: 0
    • Status: offline
    Re: contiguous variable allocation on pointer array 2020/01/16 10:55:47 (permalink)
    +1 (1)
    Well, your thinking is wrong.
    vserena82I was thinking that (in this case)
    (menu_list[0]+3)  is the same character of (menu_list[1])

    No. (menu_list[0]+3) will be the null termination character of the string "abc".
     
    There is a way to declare *menu_list contoguous?

    char menu_items[] = "one\0two\0three\0four";

    char* menu_list[] = {
        &menu_items[0],
        &menu_items[4],
        &menu_items[8],
        &menu_items[14]
    };

    It's going to be a nightmare to update and maintain though.  I suggest you find another way...
    #3
    vserena82
    New Member
    • Total Posts : 13
    • Reward points : 0
    • Joined: 2012/04/28 02:53:35
    • Location: 0
    • Status: offline
    Re: contiguous variable allocation on pointer array 2020/01/16 14:56:43 (permalink)
    0
    Thank for your relpy.
     
    I tried to make this solution to avoid to use null character to save memory.
    I can't allocate each item by address because I want an easy way to modify menu voices.
     
    I will find another way using declaration as is, I already have an idea for my routine...
    #4
    ric
    Super Member
    • Total Posts : 25592
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: contiguous variable allocation on pointer array 2020/01/16 14:59:56 (permalink)
    0
    vserena82
    I tried to make this solution to avoid to use null character to save memory.

    How will you know where each string ends if they are of variable length?
     
    I guess you could work it out on the fly, by looking at the address of the following entry.
    You couldn't use any string functions on this data unless you copied the word to a RAM buffer (and added a NULL) first.
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #5
    pcbbc
    Super Member
    • Total Posts : 1507
    • Reward points : 0
    • Joined: 2014/03/27 07:04:41
    • Location: 0
    • Status: offline
    Re: contiguous variable allocation on pointer array 2020/01/16 15:07:18 (permalink)
    +1 (1)
    Eliminating the zero bytes really doesn’t save that much memory.
    How about declaring your arrays as "const" so they stay in ROM instead of getting copied into RAM?
    They’re menus. You don’t need to modify them, do you?
     
    The menus in my project are stored in ROM and use 7 bit ASCII and pack 2 characters into every 14-bit word of program ROM on a PIC16 device.
    That does require special code to read the strings though. You can’t access them as standard char arrays.

    There’s a post on here somewhere which shows you how to generate the packed strings. I’ll try and find it...
    Edit: Here it is.


    But try marking your arrays const first.
    post edited by pcbbc - 2020/01/16 15:11:20
    #6
    ric
    Super Member
    • Total Posts : 25592
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: contiguous variable allocation on pointer array 2020/01/16 15:11:59 (permalink)
    0
    pcbbc
    How about declaring your arrays as "const" so they stay in ROM instead of getting copied into RAM?

    Excellent point.
    I suspect the OP is just running out of RAM.
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #7
    NKurzman
    A Guy on the Net
    • Total Posts : 18266
    • Reward points : 0
    • Joined: 2008/01/16 19:33:48
    • Location: 0
    • Status: online
    Re: contiguous variable allocation on pointer array 2020/01/16 17:28:05 (permalink)
    +1 (1)
    ric
    Excellent point.
    I suspect the OP is just running out of RAM.



    Since this appears to be an XY Problem we do not know what the actual Problem is.
    #8
    vserena82
    New Member
    • Total Posts : 13
    • Reward points : 0
    • Joined: 2012/04/28 02:53:35
    • Location: 0
    • Status: offline
    Re: contiguous variable allocation on pointer array 2020/01/17 05:13:10 (permalink)
    0
      I dont put \0 on array to save flash but I add \0 on write function:
    i.e.
     
    LCD_write("%s\0",L3,0, *(menu_list[0]+4));

    #9
    ric
    Super Member
    • Total Posts : 25592
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: contiguous variable allocation on pointer array 2020/01/17 05:25:02 (permalink)
    0
    You seem to be misunderstanding a few things about C.
    The code you show in post#1 does have the NULL at the end of each string.
    The pointers are in RAM, but the strings themselves are in ROM, and they will be terminated with NULLs.
    That's why your offsets don't work.

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #10
    1and0
    Access is Denied
    • Total Posts : 10346
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: offline
    Re: contiguous variable allocation on pointer array 2020/01/17 08:08:57 (permalink)
    0
    ... and those string literals do not necessarily locate in contiguous memory and/or in that order.
    #11
    NKurzman
    A Guy on the Net
    • Total Posts : 18266
    • Reward points : 0
    • Joined: 2008/01/16 19:33:48
    • Location: 0
    • Status: online
    Re: contiguous variable allocation on pointer array 2020/01/17 08:09:59 (permalink)
    0
    I would strongly suggest you state the actual problem you were trying to solve.
    Your solution to it will be very difficult to make work.
    #12
    ric
    Super Member
    • Total Posts : 25592
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: contiguous variable allocation on pointer array 2020/01/17 14:21:15 (permalink)
    0
    1and0
    ... and those string literals do not necessarily locate in contiguous memory and/or in that order.


    Indeed, but if you don't try to do undocumented fiddles, then it will work.
    i.e. access the start of the string via the pointer array, and use the terminating NULL in the normal way.
     
    The one extra thing I would do is move the pointer array into ROM. It's in RAM right now, and doesn't need to be if the pointers never change.
     
    post edited by ric - 2020/01/17 14:22:39

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #13
    1and0
    Access is Denied
    • Total Posts : 10346
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: offline
    Re: contiguous variable allocation on pointer array 2020/01/17 17:23:07 (permalink)
    +2 (2)
    That is:
    const char * const menu_list[ ] = { 

     
    #14
    Jump to:
    © 2020 APG vNext Commercial Version 4.5