<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://devzone.nordicsemi.com/cfs-file/__key/system/syndication/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/" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Problem with malloc</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/49937/problem-with-malloc</link><description>Hi, 
 I need to create a dynamic array to send a different number of bytes of data using TWI. Here is how I create an array: 
 
 When using the malloc function, I always get an array with a size of 4, regardless of the value of the X variable. I also</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Wed, 17 Jul 2019 15:58:28 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/49937/problem-with-malloc" /><item><title>RE: Problem with malloc</title><link>https://devzone.nordicsemi.com/thread/199063?ContentTypeID=1</link><pubDate>Wed, 17 Jul 2019 15:58:28 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:02e46afb-4782-4e1b-9f33-6d3d143cb5a7</guid><dc:creator>awneil</dc:creator><description>[quote userid="75765" url="~/f/nordic-q-a/49937/problem-with-malloc"]I need to create a dynamic array to send a different number of bytes of data using TW[/quote]
&lt;p&gt;No, you don&amp;#39;t &lt;em&gt;&lt;strong&gt;need&lt;/strong&gt; &lt;/em&gt;a dynamic array for that!&lt;/p&gt;
&lt;p&gt;You could just create a &amp;quot;normal&amp;quot; array of your maximum size.&lt;/p&gt;
[quote userid="75765" url="~/f/nordic-q-a/49937/problem-with-malloc"] I always get an array with a size of 4[/quote]
&lt;p&gt;How do you determine that ?&lt;/p&gt;
[quote userid="75765" url="~/f/nordic-q-a/49937/problem-with-malloc"]regardless of the value of the X variable[/quote]
&lt;p&gt;What &amp;quot;X variable&amp;quot; ?&lt;/p&gt;
&lt;p&gt;You are also making the classic mistake of not checking the return value from malloc before using it!&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>