This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

finding the size of an array before it exists?

What I have is an array of unsigned chars the first byte is actually the number of elements in the array. So is it possible to have the compiler calculate the size of the array using sizeof()? or is there some other way?

#define MENU(ID)                menu_##ID
#define MENU_CNT_P(N)           ((sizeof(MENU(N))-1)/sizeof(menu_position))


This of course gives 0 for the sizeof() value because the array isn't defined.
Suggestions/thoughts welcome.
Stephen

  • I don't think so, unless you tell the compiler the size of the string. You might declare the array with a fixed length that is longer than the longest one you will use +1, but that might defeat the purpose. I don't know the particulars.
    I know of no way that any compiler could guess the size of an unknown array...

  • This of course gives 0 for the sizeof() value because the array isn't defined

    That's strange. I would expect a compilation error.
    Why not describe what you are trying to do? Then we would be able to suggest something...

  • what I often do in cases like this is that I insert a bit of code like this in the initialization
    if array[0] != sizeof (array)
    { I goofed
    }

    this gives a safety in case I happen to 'forget' to change the value.

    Erik

  • You can use sizeof to determine the size of things (duh). You can determine the total memory for an array using sizeof(array). You can determine the size of a single element in the array using sizeof(array[0]). You can then divide these to get the number of elements in the array as shown below.

    #define ELEMENTS(x) (sizeof((x)) / sizeof((x)[0]))
    
    struct unknown array [] = {1,2,3,4,5,6,7,8 };
    int i;
    .
    .
    .
    for (i=0; i<ELEMENTS(array); i++)
    .
    .
    .
    

    However, in order for sizeof to work, the definition of the array must be visible to the sizeof operator. Otherwise, you have to calculate the number of elements in the array and store the value somewhere. For example:

    #define ELEMENTS(x) (sizeof((x)) / sizeof((x)[0]))
    
    struct unknown array [] = {1,2,3,4,5,6,7,8 };
    int array_elements = ELEMENTS(array);
    

    Then, you don't pollute the array with superfluous data. Also, the content of array_elements is obvious.

    Jon

  • Jon, I see what you mean
    my example was from a case where a struct had to fit a Flash sector.

    Erik

  • I solved the problem and at the same time simplified the code.
    What I was doing:

    #define MENU_COUNT(N) (N)
    #define MENU_POS(X, Y) (X), (Y)
    char code menu_silly_putty[] =
    { MENU_COUNT(3),
      MENU_POS(1,2),
      MENU_POS(3,4),
      MENU_POS(5,6),
    };
    


    This is great if you know for certain how many 'positions' you have but it's NOT programmatically correct if you don't. It leads to errors if you aren't paying attention to the number of menu's (the first char in the sequence).
    What I am doing now

    #define MENU_POS(X, Y) { (X), (Y) }
    menu_position code menu_silly_putty[] =
    { MENU_POS(1,2),
      MENU_POS(3,4),
      MENU_POS(5,6),
    };
    


    For the menu silly_putty definitions
    now for the actual list of menus

    #define MENU_COUNT(N) (sizeof(N)/sizeof(menu_position))
    #define MENU_DEF(N) { MENU_COUNT(N), &N }
    typedef struct menu_def
    {
     unsigned char menu_positions;
     menu_position code *positions;
    } menu_def;
    
    menu_def code menu_list[] =
    {
     MENU_DEF(menu_silly_putty),
    }
    

    This seems to work and actually compiles more compactly than the goofy things I was doing before, mostly from the code to access it being simpler.

    Stephen
    PS the actual code looks much better than this slop I'm just lazy.