<?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>How to tell the compiler a C++ const object is ROM-able ?</title><link>https://community.arm.com/developer/tools-software/tools/f/keil-forum/29836/how-to-tell-the-compiler-a-c-const-object-is-rom-able</link><description> 
I defined a global static const object of the user-defined class
type, assuming that the compiler may understand that object can be
plce on ROM.I found that the arm compiler is really good at empty
base class optimization, however, tt is not able to</description><dc:language>en-US</dc:language><generator>Telligent Community 10</generator><item><title>RE: How to tell the compiler a C++ const object is ROM-able ?</title><link>https://community.arm.com/thread/131158?ContentTypeID=1</link><pubDate>Sun, 02 Dec 2012 18:14:36 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:3df21f26-dcb4-441b-8a40-bc18f96ef525</guid><dc:creator>Pony279 Pony279</dc:creator><description>&lt;p&gt;&lt;p&gt;
I would use the direct static assignment too, if I could find any
alternative solution in this case. Maybe I have to accept the fact
that the compiler is not smart enough :) I was just wondering whether
there was any compiler specific instruction to tell the compiler that
an object is ROM-able.&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to tell the compiler a C++ const object is ROM-able ?</title><link>https://community.arm.com/thread/127898?ContentTypeID=1</link><pubDate>Sun, 02 Dec 2012 09:49:19 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:af16ad73-8cf5-4b91-9038-a62adc74827a</guid><dc:creator>nice day</dc:creator><description>&lt;p&gt;&lt;p&gt;
Sorry, that&amp;#39;s too much for me (I would say &amp;quot;Poor compiler&amp;quot;
:)).&lt;/p&gt;

&lt;p&gt;
I think if you want to get smart code generated, you should take
care that such variable initialisation is done in a straight-forward
clear way. I do not understand the reason why you use constructor
functions instead of the much easier direct static assignment.&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to tell the compiler a C++ const object is ROM-able ?</title><link>https://community.arm.com/thread/117101?ContentTypeID=1</link><pubDate>Sun, 02 Dec 2012 09:40:21 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:84d8a7d5-3c60-4028-88cf-04a9e6a51a01</guid><dc:creator>Pony279 Pony279</dc:creator><description>&lt;p&gt;&lt;pre&gt;
static const Test t1= { 5, 3};
&lt;/pre&gt;

&lt;p&gt;
&lt;br /&gt;
This may work, though, my actual code is more complicated than this.
I use template metaprogramming to build compile time datastructure.I
try to initialize an array at different places, therefore the code
like &amp;quot; static const Test t1= { 5, 3}; &amp;quot; will not satisfy my need.&lt;/p&gt;

&lt;p&gt;
the usage of my code is like:&lt;/p&gt;

&lt;pre&gt;

ARRAY_BEGIN(int, arr);
ARRAY_ELEMENT(arr,1);
ARRAY_ELEMENT(arr,2);
ARRAY_ELEMENT(arr,3);

ARRAY_END(arr);
int main(){
    using namespace std;
    int len = sizeof(arr)/sizeof(int);
    cout&amp;lt;&amp;lt;&amp;quot;sizeof(arr)/sizeof(int)=&amp;quot;&amp;lt;&amp;lt;len&amp;lt;&amp;lt;endl;
    const int* p = (const int*)&amp;amp;arr;
    for(int i=0; i&amp;lt;len; i++)
        cout&amp;lt;&amp;lt;p[i]&amp;lt;&amp;lt;endl;
}
&lt;/pre&gt;

&lt;p&gt;
&lt;br /&gt;
the output will be:&lt;br /&gt;
1 2&lt;br /&gt;
3&lt;/p&gt;

&lt;p&gt;
I&amp;#39;ve tested my code on armcc and gcc compilers respectively, it
does work as I expected. &lt;b&gt;The key thing is I expected that the arm
compiler would recognize the objects are ROM-able. (When programming
for PC with gcc compilers, we don&amp;#39;t need to care too much about
RAM)&lt;/b&gt;&lt;/p&gt;

&lt;p&gt;
The implementation code is really complicated, though, I&amp;#39;m pretty
sure the generated objects are ROM-able, at least theorecticly.
Because the constructor is just like that example I posted.&lt;/p&gt;

&lt;p&gt;
and here&amp;#39;s part of the code in case you may doubt about the
feasibility of my idea of initializing an array at different
places.&lt;/p&gt;

&lt;pre&gt;
//for the  STATIC_ASSERT MACRO, you may refer to the boost library.
#define DISTRIBUTED_ARRAY_MAX_LINES     400
#define finline inline
#define DISTRIBUTED_ERROR               -1

#ifndef __COUNTER__
#define __COUNTER__   __LINE__
#endif

#define ARRAY_BEGIN(ele_type,name)                              \ 
    enum { distributed_arr_##name##_begin_line                  \ 
    = __COUNTER__};                                             \ 
    template&amp;lt;int,typename T_##name=void&amp;gt;                        \ 
        struct distributed_arr##name{typedef ele_type type;}


#define ARRAY_ELEMENT(name, value)                              \ 
        template&amp;lt;typename T_##name&amp;gt; struct                      \ 
            distributed_arr##name&amp;lt;__COUNTER__                   \ 
                    ,T_##name&amp;gt;                                  \ 
        { typedef distributed_arr##name&amp;lt;0&amp;gt;::type type;          \ 
            const type e; finline distributed_arr##name():e(value){}}


#define ARRAY_END(name)                                     \ 
    enum { distributed_arr_##name##_end_line =              \ 
        __COUNTER__};                                       \ 
    template&amp;lt;int begin, int end&amp;gt;                            \ 
    struct array_end_##name:                                \ 
          public array_end_##name&amp;lt;begin,(begin+end)/2&amp;gt;      \ 
        , public  array_end_##name&amp;lt;(begin+end)/2+1,end&amp;gt;     \ 
    { finline array_end_##name(){}};                        \ 
    template&amp;lt;int begin&amp;gt;                                     \ 
        struct array_end_##name&amp;lt;begin, begin&amp;gt;:              \ 
          public distributed_arr##name&amp;lt;begin&amp;gt;               \ 
        { finline array_end_##name(){} };                   \ 
        template&amp;lt;int t&amp;gt;                                     \ 
        struct array_end_##name&amp;lt;t,DISTRIBUTED_ERROR&amp;gt;        \ 
        {STATIC_ASSERT(t==0);};                             \ 
    static const array_end_##name                           \ 
            &amp;lt;distributed_arr_##name##_begin_line,           \ 
            (distributed_arr_##name##_end_line              \ 
                -distributed_arr_##name##_begin_line        \ 
                &amp;gt;= DISTRIBUTED_ARRAY_MAX_LINES)?            \ 
                0 : distributed_arr_##name##_end_line       \ 
                    &amp;gt;                                       \ 
                name

class empty_base_class_optimization_assert{

    ARRAY_BEGIN(int, array_1);
    ARRAY_BEGIN(char, array_2);
    ARRAY_ELEMENT(array_1,1);
    ARRAY_ELEMENT(array_2,2);
    ARRAY_ELEMENT(array_1,1);
    ARRAY_ELEMENT(array_2,2);
    ARRAY_END(array_1);
    ARRAY_END(array_2);

    STATIC_ASSERT(sizeof(array_1)==2*sizeof(int)
               &amp;amp;&amp;amp; sizeof(array_2)==2*sizeof(char));
};
&lt;/pre&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to tell the compiler a C++ const object is ROM-able ?</title><link>https://community.arm.com/thread/106643?ContentTypeID=1</link><pubDate>Sun, 02 Dec 2012 08:36:42 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:c459b238-afd6-4a66-a9be-a0b1bf44684c</guid><dc:creator>nice day</dc:creator><description>&lt;p&gt;&lt;p&gt;
I think initialisation by a constructor function might be a bit
difficult for the compiler. You should better define it this way:&lt;/p&gt;

&lt;pre&gt;
struct Test{
  int t;
  int s;
};

static const Test t1= { 5, 3};
static const Test t2= { 5, 3};
static const Test t3= { 5, 3};
static const Test t4= { 5, 3};
static const Test t5= { 5, 3};
static const Test t6= { 5, 3};
&lt;/pre&gt;

&lt;p&gt;
In Opt-Level 1 you should not recognize any increase in data
(neither RO, RW or ZI) - these variables will in fact be handled
similar to defines / const numbers.&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to tell the compiler a C++ const object is ROM-able ?</title><link>https://community.arm.com/thread/80793?ContentTypeID=1</link><pubDate>Sun, 02 Dec 2012 07:37:11 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:8bd5ba26-d437-4d60-ab8c-1fdb3ce0814d</guid><dc:creator>Pony279 Pony279</dc:creator><description>&lt;p&gt;&lt;p&gt;
for example&lt;/p&gt;

&lt;pre&gt;
struct
Test{
    const int t;
    const int s;
    Test():t(5),s(3){}
};

static const Test t1;
static const Test t2;

&lt;/pre&gt;

&lt;p&gt;
the RW-data of the build output increases by 8 bytes for evere
static const Test object.&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to tell the compiler a C++ const object is ROM-able ?</title><link>https://community.arm.com/thread/61429?ContentTypeID=1</link><pubDate>Sun, 02 Dec 2012 07:08:56 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:e2a9eaad-b3e3-4009-9c17-3245f708882f</guid><dc:creator>nice day</dc:creator><description>&lt;p&gt;&lt;p&gt;
In ARM a &amp;quot;const static&amp;quot; object will be placed in ROM. How do you
come to the idea that it would be otherwise?&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>