+ Post New Thread
Results 1 to 4 of 4
  1. #1

    Getting a constant ragged array entirely in flash memory from standard C99

    If I have a ragged array of strings, defined like this...

    Code:
    static const char * const labels[] = {"Elk", "Horse", "Hog"};
    with GCC the strings go into flash memory but the array of char pointers goes into RAM. I know I can force both parts into flash by creating a section in the linker script and referring to it in the source code before the array definition, but this then means that my code is compiler specific and also depends on an outside file as well (the linker script). Is anyone aware of any trick using only standard C99 that will get all parts into flash memory so that it compiles and does what I want with both GCC and other different compilers?

    •   Alt18th February 2018, 23:11

      advertising

        
       

  2. #2
    Advanced Member level 5
    Points: 8,426, Level: 21
    Easyrider83's Avatar
    Join Date
    Oct 2011
    Location
    Tallinn, Estonia
    Posts
    1,510
    Helped
    338 / 338
    Points
    8,426
    Level
    21

    Re: Getting a constant ragged array entirely in flash memory from standard C99

    May be good to try specify pointers location directly?
    Code:
    static const char value1[] = "Elk";
    static const char value2[] = "Horse";
    static const char value3[] = "Hog";
    
    static const char * const labels[] = {value1, value2, value3};
    Love me or hate me. All infractions will be ignored.



    •   Alt19th February 2018, 16:25

      advertising

        
       

  3. #3
    Newbie level 3
    Points: 31, Level: 1

    Join Date
    Feb 2018
    Posts
    3
    Helped
    0 / 0
    Points
    31
    Level
    1

    Re: Getting a constant ragged array entirely in flash memory from standard C99

    Quote Originally Posted by Easyrider83 View Post
    May be good to try specify pointers location directly?
    Code:
    static const char value1[] = "Elk";
    static const char value2[] = "Horse";
    static const char value3[] = "Hog";
    
    static const char * const labels[] = {value1, value2, value3};
    Thank you for the suggestion. I will try that on a variety of compilers and see what they do and report back.



    •   Alt20th February 2018, 13:00

      advertising

        
       

  4. #4
    Advanced Member level 5
    Points: 8,426, Level: 21
    Easyrider83's Avatar
    Join Date
    Oct 2011
    Location
    Tallinn, Estonia
    Posts
    1,510
    Helped
    338 / 338
    Points
    8,426
    Level
    21

    Re: Getting a constant ragged array entirely in flash memory from standard C99

    Another option:
    Code:
    static const char * const labels[] = {(const char *)"Elk", (const char *)"Horse", (const char *)"Hog"};
    Love me or hate me. All infractions will be ignored.



--[[ ]]--