<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://community.arm.com/utility/feedstylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>finding the size of an array before it exists?</title><link>https://community.arm.com/developer/tools-software/tools/f/keil-forum/37968/finding-the-size-of-an-array-before-it-exists</link><description> 
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</description><dc:language>en-US</dc:language><generator>Telligent Community 10</generator><item><title>RE: finding the size of an array before it exists?</title><link>https://community.arm.com/thread/89234?ContentTypeID=1</link><pubDate>Wed, 27 Aug 2008 07:47:45 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:db048061-fd5d-4b50-bf98-1e008e5ceab3</guid><dc:creator>stephen phillips</dc:creator><description>&lt;p&gt;&lt;p&gt;
I solved the problem and at the same time simplified the code.&lt;br /&gt;
What I &lt;b&gt;was&lt;/b&gt; doing:&lt;/p&gt;

&lt;pre&gt;
#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),
};
&lt;/pre&gt;

&lt;p&gt;
&lt;br /&gt;
This is great if you know for certain how many &amp;#39;positions&amp;#39; you have
but it&amp;#39;s NOT programmatically correct if you don&amp;#39;t. It leads to
errors if you aren&amp;#39;t paying attention to the number of menu&amp;#39;s (the
first char in the sequence).&lt;br /&gt;
What I am doing now&lt;/p&gt;

&lt;pre&gt;
#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),
};
&lt;/pre&gt;

&lt;p&gt;
&lt;br /&gt;
For the menu silly_putty definitions&lt;br /&gt;
now for the actual list of menus&lt;/p&gt;

&lt;pre&gt;
#define MENU_COUNT(N) (sizeof(N)/sizeof(menu_position))
#define MENU_DEF(N) { MENU_COUNT(N), &amp;amp;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),
}
&lt;/pre&gt;

&lt;p&gt;
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.&lt;/p&gt;

&lt;p&gt;
Stephen&lt;br /&gt;
PS the actual code looks much better than this slop I&amp;#39;m just
lazy.&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: finding the size of an array before it exists?</title><link>https://community.arm.com/thread/89235?ContentTypeID=1</link><pubDate>Tue, 26 Aug 2008 07:45:24 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:821656d7-f07b-4c04-bd6b-4079d8e4c47c</guid><dc:creator>erik  malund</dc:creator><description>&lt;p&gt;&lt;p&gt;
Jon, I see what you mean&lt;br /&gt;
my example was from a case where a struct had to fit a Flash
sector.&lt;/p&gt;

&lt;p&gt;
Erik&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: finding the size of an array before it exists?</title><link>https://community.arm.com/thread/52287?ContentTypeID=1</link><pubDate>Tue, 26 Aug 2008 07:06:33 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:9e82e0ea-882e-4abd-9a76-3475cf1c8578</guid><dc:creator>Jon Ward</dc:creator><description>&lt;p&gt;&lt;p&gt;
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.&lt;/p&gt;

&lt;pre&gt;
#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&amp;lt;ELEMENTS(array); i++)
.
.
.
&lt;/pre&gt;

&lt;p&gt;
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:&lt;/p&gt;

&lt;pre&gt;
#define ELEMENTS(x) (sizeof((x)) / sizeof((x)[0]))

struct unknown array [] = {1,2,3,4,5,6,7,8 };
int array_elements = ELEMENTS(array);
&lt;/pre&gt;

&lt;p&gt;
Then, you don&amp;#39;t pollute the array with superfluous data. Also, the
content of array_elements is obvious.&lt;/p&gt;

&lt;p&gt;
Jon&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: finding the size of an array before it exists?</title><link>https://community.arm.com/thread/89233?ContentTypeID=1</link><pubDate>Tue, 26 Aug 2008 05:32:07 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:5901ca2f-fb55-4425-9c76-1a6e3a12fcbc</guid><dc:creator>erik  malund</dc:creator><description>&lt;p&gt;&lt;p&gt;
what I often do in cases like this is that I insert a bit of code
like this in the initialization&lt;br /&gt;
if array[0] != sizeof (array)&lt;br /&gt;
{ I goofed&lt;br /&gt;
}&lt;/p&gt;

&lt;p&gt;
this gives a safety in case I happen to &amp;#39;forget&amp;#39; to change the
value.&lt;/p&gt;

&lt;p&gt;
Erik&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: finding the size of an array before it exists?</title><link>https://community.arm.com/thread/52286?ContentTypeID=1</link><pubDate>Mon, 25 Aug 2008 21:26:50 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:770180f7-3658-4a1f-9284-74861193e112</guid><dc:creator>Mike Kleshov</dc:creator><description>&lt;p&gt;&lt;p&gt;
&lt;i&gt;This of course gives 0 for the sizeof() value because the array
isn&amp;#39;t defined&lt;/i&gt;&lt;/p&gt;

&lt;p&gt;
That&amp;#39;s strange. I would expect a compilation error.&lt;br /&gt;
Why not describe what you are trying to do? Then we would be able to
suggest something...&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: finding the size of an array before it exists?</title><link>https://community.arm.com/thread/52291?ContentTypeID=1</link><pubDate>Mon, 25 Aug 2008 14:27:39 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:78287a18-9a96-435c-ac92-f525bcd65f3a</guid><dc:creator>scrungy doolittle</dc:creator><description>&lt;p&gt;&lt;p&gt;
I don&amp;#39;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&amp;#39;t know the particulars.&lt;br /&gt;
I know of no way that any compiler could guess the size of an unknown
array...&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>