<?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>Whiy do memory allocation functions return a void pointer ?</title><link>https://community.arm.com/developer/tools-software/tools/f/keil-forum/34457/whiy-do-memory-allocation-functions-return-a-void-pointer</link><description> 
The answer seems obvious, but considering this MISRA rule 

 
Rule 11.5 A conversion should not be performed from pointer to
void into 
pointer to object 

 
would it not be safer to have memory allocation functions like
&amp;quot;malloc&amp;quot; return a pointer to</description><dc:language>en-US</dc:language><generator>Telligent Community 10</generator><item><title>RE: Whiy do memory allocation functions return a void pointer ?</title><link>https://community.arm.com/thread/108296?ContentTypeID=1</link><pubDate>Sun, 21 Jun 2015 08:21:08 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:05f2ccc0-699b-48e0-8e97-06d45b2d41f4</guid><dc:creator>Misha Mish</dc:creator><description>&lt;p&gt;&lt;p&gt;
Thanks folks.&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Whiy do memory allocation functions return a void pointer ?</title><link>https://community.arm.com/thread/93455?ContentTypeID=1</link><pubDate>Sun, 21 Jun 2015 07:29:17 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:aa34b296-9c18-4916-b42f-0813c36be0b4</guid><dc:creator>ImPer Westermark</dc:creator><description>&lt;p&gt;&lt;p&gt;
MISRA or not - dynamic memory isn&amp;#39;t a great idea in embedded
devices with long uptimes. Too much problems with fragmented heap,
resulting in failed memory allocations even if there are lots of free
memory.&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Whiy do memory allocation functions return a void pointer ?</title><link>https://community.arm.com/thread/63834?ContentTypeID=1</link><pubDate>Sun, 21 Jun 2015 07:05:45 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:9d7707d5-2141-415b-9e2a-fe478aab48b9</guid><dc:creator>HansBernhard Broeker</dc:creator><description>&lt;p&gt;&lt;p&gt;
Well, MISRA is not in a position to overturn decisions made by the
ISO standard committee about the C programming language, some 25
years ago.&lt;/p&gt;

&lt;p&gt;
But that&amp;#39;s irrelevant anyway, because you&amp;#39;re overlooking MISRA
2004 Rule 20.4, which forbids any use of malloc() &amp;amp; friends.&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Whiy do memory allocation functions return a void pointer ?</title><link>https://community.arm.com/thread/69252?ContentTypeID=1</link><pubDate>Sat, 20 Jun 2015 12:25:39 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:c7f3eb43-efe8-46a5-9dae-6d7b83707b52</guid><dc:creator>ImPer Westermark</dc:creator><description>&lt;p&gt;&lt;p&gt;
A char* has no alignment issues. But that is irrelevant for all
the times when you are allocating objects of a type that does have
alignment issues.&lt;/p&gt;

&lt;p&gt;
Note that malloc() can&amp;#39;t know what type you are allocating. And so
can&amp;#39;t know what the alignment requirements may be. So the only
workable solution is for malloc() to always produce a pointer of the
&amp;quot;best&amp;quot; possible alignment class that the processor might require.
This means it&amp;#39;s safe to type cast the returned pointer into a pointer
to int or a pointer to double or whatever you need.&lt;/p&gt;

&lt;p&gt;
MISRA has to limit itself into what is possible by the programming
language. And malloc() is part of the C language standard. But the
goal with the MISRA rule isn&amp;#39;t to point finger at malloc(), but to
make sure that people don&amp;#39;t try to send random void pointers as
parameters or store in structs just to later decide what to recast
the pointers into. So developers should avoid trying to implement
abstract data types centered around &amp;quot;base class&amp;quot; code that operates
on void pointers.&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Whiy do memory allocation functions return a void pointer ?</title><link>https://community.arm.com/thread/63845?ContentTypeID=1</link><pubDate>Sat, 20 Jun 2015 10:03:31 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:d0f0291c-c5b0-4018-b411-2c7c9ff2ff6a</guid><dc:creator>Misha Mish</dc:creator><description>&lt;p&gt;&lt;p&gt;
Ah, conversion to/from void* is implicit. Do that is a part of the
answer.&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>