<?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>Address space overflow and garbage values while using &amp;#39;sprintf&amp;#39;</title><link>https://community.arm.com/developer/tools-software/tools/f/keil-forum/31104/address-space-overflow-and-garbage-values-while-using-sprintf</link><description> 
Dear all, 
The linker gives following error: 

 
*** ERROR L107: ADDRESS SPACE OVERFLOW
 SPACE: DATA
 SEGMENT: ?DT?TIMER0_ISR?TIMER
 LENGTH: 0002H
 

 
I have a global buffer. If i declare it idata then the
linker does not give any error. But the sprintf</description><dc:language>en-US</dc:language><generator>Telligent Community 10</generator><item><title>RE: Address space overflow and garbage values while using 'sprintf'</title><link>https://community.arm.com/thread/106482?ContentTypeID=1</link><pubDate>Thu, 18 Oct 2012 13:16:44 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:dd61ea37-43c7-4b14-a0d3-4ea5af987877</guid><dc:creator>ImPer Westermark</dc:creator><description>&lt;p&gt;&lt;p&gt;
I do the #2 when I want to be explicit. Else I use the #1.&lt;/p&gt;

&lt;p&gt;
But I don&amp;#39;t like type defines that are arrays - normally hide
inside a struct to make the data a real black-box and avoid the
array/pointer duality.&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Address space overflow and garbage values while using 'sprintf'</title><link>https://community.arm.com/thread/92139?ContentTypeID=1</link><pubDate>Thu, 18 Oct 2012 12:30:32 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:c8850e8b-adcd-43ad-8a71-7471f66aca3c</guid><dc:creator>HansBernhard Broeker</dc:creator><description>&lt;p&gt;&lt;p&gt;
&lt;i&gt;They all have in common that buff already is a const pointer to
the buffer.&lt;/i&gt;&lt;/p&gt;

&lt;p&gt;
Not really. buff &amp;quot;is&amp;quot; not a pointer --- it&amp;#39;s an array. It just
automatically &lt;b&gt;turns into&lt;/b&gt; a pointer in most of its usages.&lt;/p&gt;

&lt;p&gt;
&lt;i&gt;You&amp;#39;re passing a pointer to a pointer&lt;/i&gt;&lt;/p&gt;

&lt;p&gt;
No, he isn&amp;#39;t. He&amp;#39;s passing a pointer to an array. Because appling
the address-of operator &amp;#39;&amp;amp;&amp;#39; is actually one of the exceptions to
the above &amp;quot;most of&amp;quot; rule.&lt;/p&gt;

&lt;p&gt;
But you&amp;#39;re still correct that &amp;amp;buff is not what should be
passed to sprintf() --- even though it would usually work anyway. If
you get a warning from your tools when you do this, you should
consider that a good thing.&lt;/p&gt;

&lt;p&gt;
The Right Way(no TM) to do this is one of&lt;/p&gt;

&lt;pre&gt;
sprintf(buff, ...);
sprintf(&amp;amp;buff[0], ...);
sprintf(&amp;amp;(buff[0]), ...);
&lt;/pre&gt;

&lt;p&gt;
These are all exactly equivalent, so most experienced C
programmers will use the first form simply because it&amp;#39;s shorter. But
you&amp;#39;ll find the other two forms quite frequently, too.&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Address space overflow and garbage values while using 'sprintf'</title><link>https://community.arm.com/thread/61262?ContentTypeID=1</link><pubDate>Thu, 18 Oct 2012 08:07:31 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:8a139fbb-b228-4a22-b63e-b231cd8bc33a</guid><dc:creator>Dominic Fandrey</dc:creator><description>&lt;p&gt;&lt;pre&gt;
char buff[] = &amp;quot;1234567&amp;quot;;
char buff[7];
char buff[] = {1, 2, 3, 4, 5, 6, 7};
&lt;/pre&gt;

&lt;p&gt;
&lt;br /&gt;
They all have in common that buff already is a const pointer to the
buffer. You&amp;#39;re passing a pointer to a pointer to sprintf, don&amp;#39;t.&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Address space overflow and garbage values while using 'sprintf'</title><link>https://community.arm.com/thread/92141?ContentTypeID=1</link><pubDate>Mon, 15 Oct 2012 06:38:15 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:14eb93d2-8431-437c-98e2-d32f12814e7a</guid><dc:creator>&amp;#178;erik malund</dc:creator><description>&lt;p&gt;&lt;p&gt;
fixing it with IDATA only works if that leaves enough stack You,
probably, have stack overflow&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Address space overflow and garbage values while using 'sprintf'</title><link>https://community.arm.com/thread/106496?ContentTypeID=1</link><pubDate>Mon, 15 Oct 2012 04:58:40 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:933d89a3-b59b-40c1-a4a1-fbce106b2547</guid><dc:creator>Ash J</dc:creator><description>&lt;p&gt;&lt;p&gt;
ya. the values returned from the sprintf in the buffer are correct
(not garbage).&lt;/p&gt;

&lt;pre&gt;
strlen = sprintf(dtptr, &amp;quot;%d&amp;quot;, (uint16_t)(PrbTemperature*1000));
&lt;/pre&gt;

&lt;p&gt;
even after modifying to this, i get garbage the values as seen on
terminal. the values are given in the next post&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Address space overflow and garbage values while using 'sprintf'</title><link>https://community.arm.com/thread/80651?ContentTypeID=1</link><pubDate>Mon, 15 Oct 2012 04:51:56 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:1ccfbfdf-5965-497a-84d7-d1c718b7b5b7</guid><dc:creator>Ash J</dc:creator><description>&lt;p&gt;&lt;p&gt;
1. strlen is a variable that i have declared in my function.&lt;br /&gt;
2. I think float is required. or may be i can manipulate temperature
by multiplying it with 1000 or 100. (other way round which i can
think of, but dnt want to use. I am not confident about its working
for all values of temperature)&lt;br /&gt;
3. I wrote &amp;amp;buff just because i want the compiler to know that i
am giving the address of an array and not a variable
(foolishness)&lt;br /&gt;
4. ya, i am defining buff as idata.&lt;/p&gt;

&lt;p&gt;
regards,&lt;br /&gt;
Dhaval&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Address space overflow and garbage values while using 'sprintf'</title><link>https://community.arm.com/thread/61264?ContentTypeID=1</link><pubDate>Mon, 15 Oct 2012 04:33:24 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:c537de34-4226-448f-8e97-f1a0effca547</guid><dc:creator>Ash J</dc:creator><description>&lt;p&gt;&lt;p&gt;
this is what i see on my terminal:&lt;br /&gt;
&lt;i&gt;&lt;br /&gt;
Input Set-point Temperature&lt;br /&gt;
20201&lt;br /&gt;
&amp;euml;GA&amp;Egrave;20264&lt;br /&gt;
&amp;icirc;GA&amp;iuml;20298&lt;br /&gt;
&amp;eth;GB&lt;br /&gt;
20309&lt;br /&gt;
&amp;iuml;GB 20336&lt;br /&gt;
&amp;eth;GB20176&lt;br /&gt;
&amp;oacute;GB&amp;#39;19968&lt;br /&gt;
&amp;ograve;GB219977&lt;br /&gt;
&amp;ograve;GB&amp;gt;20239&lt;br /&gt;
&amp;otilde;GBN20425&lt;br /&gt;
&amp;otilde;GBT20433&lt;br /&gt;
&amp;ouml;GBZ20447&lt;br /&gt;
&amp;otilde;GBi20443&lt;br /&gt;
&amp;otilde;GBr20464&lt;br /&gt;
&amp;ugrave;GB&amp;euro;20443&lt;br /&gt;
&amp;ugrave;GB&amp;circ;20165&lt;br /&gt;
&amp;uacute;GB&amp;quot;20087&lt;br /&gt;
&amp;ugrave;GB&amp;Yuml;20260&lt;br /&gt;
&amp;uacute;GB&amp;laquo;20542&lt;br /&gt;
&amp;ucirc;GB&amp;reg;20572&lt;br /&gt;
&amp;ucirc;GB&amp;cedil;20549&lt;br /&gt;
&amp;uuml;GB&amp;iquest;20565&lt;br /&gt;
&amp;ucirc;GB&amp;Aring;20585&lt;br /&gt;
&amp;uuml;GB&amp;Igrave;20579&lt;br /&gt;
&amp;yacute;GB&amp;times;20342&lt;br /&gt;
&amp;thorn;GB&amp;auml;20220&lt;br /&gt;
&amp;yuml;GB&amp;ecirc;20275&lt;br /&gt;
20597&lt;br /&gt;
20702&lt;/i&gt;&lt;/p&gt;

&lt;p&gt;
The numbers are correctly displayed except that they are prefixed
with some garbage values which i am unaware of how they creep in.&lt;br /&gt;
and this is the change made in the code:&lt;/p&gt;

&lt;pre&gt;
strlen = sprintf(dtptr, &amp;quot;%d&amp;quot;, (uint16_t)(PrbTemperature*1000));
&lt;/pre&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Address space overflow and garbage values while using 'sprintf'</title><link>https://community.arm.com/thread/67782?ContentTypeID=1</link><pubDate>Mon, 15 Oct 2012 04:28:28 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:e5fc2fad-c120-4fc9-a2b1-827d69dca34f</guid><dc:creator>ImPer Westermark</dc:creator><description>&lt;p&gt;&lt;p&gt;
strlen() is a function available in the C standard library - are
you saying that you have added own variables with same name?&lt;/p&gt;

&lt;p&gt;
Next thing - why do you have a float? You sure you really need a
float to handle that temperature? Have you considered fixed point
integers? That removes the need for linking in floating point
support.&lt;/p&gt;

&lt;p&gt;
Another thing - you do &amp;amp;buff. You realize that the name buff
itself represents the address of the first element of the buff[]
array.&lt;/p&gt;

&lt;p&gt;
Have you verified that sprintf() gives your expected printout if
you throw away some code/data so you don&amp;#39;t have to change that buffer
to idata? By the way - is it buff[] that you change to idata?&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>