<?xml version="1.0" encoding="UTF-8"?>
<!-- generator="bbPress/1.0.2" -->
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom">
	<channel>
		<title>LeafLabs Garden &#187; Topic: Strange errors when using `new` to allocate a pointer.</title>
		<link>http://forums.leaflabs.com/topic.php?id=161</link>
		<description>A place to share, learn, and grow...</description>
		<language>en-US</language>
		<pubDate>Fri, 22 Jan 2016 00:19:39 +0000</pubDate>
		<generator>http://bbpress.org/?v=1.0.2</generator>
		<textInput>
			<title><![CDATA[Search]]></title>
			<description><![CDATA[Search all topics from these forums.]]></description>
			<name>q</name>
			<link>http://forums.leaflabs.com/search.php</link>
		</textInput>
		<atom:link href="http://forums.leaflabs.com/rss.php?topic=161" rel="self" type="application/rss+xml" />

		<item>
			<title>mbolivar on "Strange errors when using `new` to allocate a pointer."</title>
			<link>http://forums.leaflabs.com/topic.php?id=161#post-12075</link>
			<pubDate>Fri, 27 Jul 2012 18:17:54 +0000</pubDate>
			<dc:creator>mbolivar</dc:creator>
			<guid isPermaLink="false">12075@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;You're absolutely right. Strange; I swear this was working for v0.0.12; we used it in the Maple Native guitar effects demo.&#60;/p&#62;
&#60;p&#62;In any case, this was fixed in &#60;a href=&#34;https://github.com/leaflabs/libmaple/commit/c4e08477202d31149510262a1fd52a09e3e71337&#34;&#62;libmaple c4e0847&#60;/a&#62; by adding an abort() implementation. You can either take that approach, or just use your workaround. The fix will be part of the next release.&#60;/p&#62;
&#60;p&#62;Sorry for the mistake.
&#60;/p&#62;</description>
		</item>
		<item>
			<title>shark2600 on "Strange errors when using `new` to allocate a pointer."</title>
			<link>http://forums.leaflabs.com/topic.php?id=161#post-12074</link>
			<pubDate>Fri, 27 Jul 2012 17:17:17 +0000</pubDate>
			<dc:creator>shark2600</dc:creator>
			<guid isPermaLink="false">12074@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;No, a 'new' will create the same error. Try a simple piece of code, with 'new' operator, say creating a pointer of integer.&#60;br /&#62;
You will see the same undefined reference error
&#60;/p&#62;</description>
		</item>
		<item>
			<title>mbolivar on "Strange errors when using `new` to allocate a pointer."</title>
			<link>http://forums.leaflabs.com/topic.php?id=161#post-12071</link>
			<pubDate>Fri, 27 Jul 2012 16:40:49 +0000</pubDate>
			<dc:creator>mbolivar</dc:creator>
			<guid isPermaLink="false">12071@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;Thanks for the report. I've never tested the STL classes, so it's good to know you got vector working.&#60;/p&#62;
&#60;p&#62;Just guessing, but it sounds like the issue isn't with new/delete, but that the std::vector constructor or destructor is calling those routines, which we don't provide by default.
&#60;/p&#62;</description>
		</item>
		<item>
			<title>shark2600 on "Strange errors when using `new` to allocate a pointer."</title>
			<link>http://forums.leaflabs.com/topic.php?id=161#post-12019</link>
			<pubDate>Thu, 26 Jul 2012 01:32:08 +0000</pubDate>
			<dc:creator>shark2600</dc:creator>
			<guid isPermaLink="false">12019@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;Hi mbolivar,&#60;/p&#62;
&#60;p&#62;I'm using v0.0.12, the 'new' and 'delete' operators still not working. I have to put dummy _exit, _kill and _getpid() in syscalls.c to get them work, any suggestion?&#60;/p&#62;
&#60;p&#62;FYI: I added the following to \maple-ide-0.0.12-windowsxp32\hardware\leaflabs\cores\maple\syscalls.c to get new and delete work.&#60;/p&#62;
&#60;p&#62;The reason why I need this is I need stl::vector and so on support in my project.&#60;/p&#62;
&#60;p&#62;Thanks&#60;/p&#62;
&#60;p&#62;/*&#60;br /&#62;
 * getpid -- only one process, so just return 1.&#60;br /&#62;
 */&#60;br /&#62;
#define __MYPID 1&#60;br /&#62;
int _getpid()&#60;br /&#62;
{&#60;br /&#62;
  return __MYPID;&#60;br /&#62;
}&#60;/p&#62;
&#60;p&#62;/*&#60;br /&#62;
 * kill -- go out via exit...&#60;br /&#62;
 */&#60;br /&#62;
int _kill(int pid, int sig)&#60;br /&#62;
{&#60;/p&#62;
&#60;p&#62;  return -1;&#60;br /&#62;
}&#60;/p&#62;
&#60;p&#62;int _exit(int val)&#60;br /&#62;
{&#60;br /&#62;
  return (-1);&#60;br /&#62;
}
&#60;/p&#62;</description>
		</item>
		<item>
			<title>mbolivar on "Strange errors when using `new` to allocate a pointer."</title>
			<link>http://forums.leaflabs.com/topic.php?id=161#post-10497</link>
			<pubDate>Sat, 05 May 2012 23:28:29 +0000</pubDate>
			<dc:creator>mbolivar</dc:creator>
			<guid isPermaLink="false">10497@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;RichardE:&#60;/p&#62;
&#60;p&#62;Dynamic memory should work as of v0.0.12. Can you present a program that doesn't work as expected?&#60;/p&#62;
&#60;p&#62;Edit: Fix typo. Also see: &#60;a href=&#34;http://leaflabs.com/docs/faq.html#how-can-i-use-malloc-new&#34; rel=&#34;nofollow&#34;&#62;http://leaflabs.com/docs/faq.html#how-can-i-use-malloc-new&#60;/a&#62;
&#60;/p&#62;</description>
		</item>
		<item>
			<title>RichardE on "Strange errors when using `new` to allocate a pointer."</title>
			<link>http://forums.leaflabs.com/topic.php?id=161#post-10301</link>
			<pubDate>Sat, 21 Apr 2012 14:04:23 +0000</pubDate>
			<dc:creator>RichardE</dc:creator>
			<guid isPermaLink="false">10301@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;I take your point. It is hard to do and there are certainly dangers of memory leaks, but I still think that beyond a certain minimum RAM size dynamic memory allocation is important and becomes less important as the RAM size increases.&#60;/p&#62;
&#60;p&#62;The program I am working on at the moment is an example. You could argue that this is not an embedded program at all. I started on the Arduino with a Gameduino shield (truly awesome shield which works fine with the Maple - buy one even if you have no intention of writing games) and started writing a clone of the Robotron arcade game. It went very well. Every level has a number of enemies that can be of a number of different types. All enemies are implemented as classes derived from a common base class (EnemyObject). At the start of every level you read counts for the number of enemies of each type and then dynamically allocate memory for each enemy. When the level ends you delete all the enemies freeing up memory so the next level can re-use it.&#60;/p&#62;
&#60;p&#62;Eventually, even sticking as much as possible in program memory, RAM started to get a bit tight (Arduino only has 2K) so I bought a Maple and started porting the code. Then I realised there was no dynamic memory allocation. The simplest way around this is to declare arrays containing all the enemies you will need, for all levels but this is staggeringly wasteful. Suppose you are allowed a maximum number of 10 enemies of a particular type on each level. Then you need 10 arrays with 10 of enemy type A, 10 of enemy type B, 10 of enemy type C and so on. These are occupying RAM all the time, even when no enemies of a particular type appear in a level so you are bound to run out of memory sooner rather than later.&#60;/p&#62;
&#60;p&#62;Eventually I got around this by allocating memory on the stack. Each level is a class with a PlayLevel method. The PlayLevel method creates a lot of enemy arrays as *local* variables and then calls a method in a base class that runs around the game loop until the level ends, passing these enemy arrays to the method. When the level ends and PlayLevel finishes all the array vanish as they are local variables. It works, but it is rather cumbersome and in the end I had to write a C# program to generate the source code for each level. It would be so much easier if I could allocate memory dynamically.
&#60;/p&#62;</description>
		</item>
		<item>
			<title>gbulmer on "Strange errors when using `new` to allocate a pointer."</title>
			<link>http://forums.leaflabs.com/topic.php?id=161#post-9817</link>
			<pubDate>Sun, 18 Mar 2012 15:19:38 +0000</pubDate>
			<dc:creator>gbulmer</dc:creator>
			<guid isPermaLink="false">9817@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;RichardE - It is quite unusual for embedded programs to use dynamic memory. &#60;/p&#62;
&#60;p&#62;I strongly agree dynamic allocation and garbage collection are very useful and valuable features, but I don't agree that it is more important when RAM is scarce.&#60;/p&#62;
&#60;p&#62;The main problems are:&#60;br /&#62;
1. It is extremely hard to test a program of even moderate complexity, and know that under all circumstances it will not run out of memory and break. Dynamic allocation makes this worse than simple static allocation and stack frames. This is a 'show stopper' for some developers.&#60;/p&#62;
&#60;p&#62;2. Having managed memory can consume quite a lot of RAM in various overheads or fragmentation. This waste can be in the form of management overhead (which is needed to be able to manage memory), 'internal' fragmentation, and 'external' fragmentation. &#60;/p&#62;
&#60;p&#62;Typically the smallest block that can be managed is some minimum-size because it needs to have a pointer to other blocks (4 bytes), and extra management typically the size of the block. The size must be held somewhere, so the correct size of block can always be recovered by free. Maybe we limit this to a 2 bytes. So say 6 bytes are used in &#60;strong&#62;every&#60;/strong&#62; block of memory for management. Not too bad if user-objects are large, but very bad if user-objects are small.&#60;/p&#62;
&#60;p&#62;The block might need to be aligned for the worst case data type (maybe an 8 byte double), which may make it impossible to use blocks smaller than 8 bytes. &#60;/p&#62;
&#60;p&#62;To make things easier to manage, it is common to allocate memory in multiples of minimum-size. So if the whole block isn't full, the space is wasted. If the minimum block size were 8-bytes, then, on average 4 bytes would be wasted. That is 'internal' fragmentation - there is unused space, but you can't give it to the user's program because it can't be managed.&#60;/p&#62;
&#60;p&#62;As blocks are freed, and then reallocated in random patterns, free space will appear between the other blocks which are in use. In languages like Java or Python, this can be solved, because the Java/Python VM could move the used blocks around to collect together the unused space. Normally in C or C++, those used blocks can't be moved. So there may be enough total free space to satisfy a new request, but because it is fragmented by in-use blocks, the request can't be satisfied. That type of wasted space is often called external fragmentation.&#60;/p&#62;
&#60;p&#62;Summary:&#60;br /&#62;
1. It is hard to test programs and know memory fragmentation will not cause them to fail&#60;br /&#62;
2. Memory management and garbage collection have overhead, and can't always make good use of RAM dues to fragmentation.&#60;/p&#62;
&#60;p&#62;HTH
&#60;/p&#62;</description>
		</item>
		<item>
			<title>RichardE on "Strange errors when using `new` to allocate a pointer."</title>
			<link>http://forums.leaflabs.com/topic.php?id=161#post-9134</link>
			<pubDate>Sat, 17 Mar 2012 06:13:22 +0000</pubDate>
			<dc:creator>RichardE</dc:creator>
			<guid isPermaLink="false">9134@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;Arduino 1.0 does now support dynamic memory allocation. You could argue that dynamic memory allocation is more important than ever on a device with limited memory. It means you can re-use the memory more easily at different points in your program. Without it your variables waste memory even when not being used.&#60;/p&#62;
&#60;p&#62;Slightly disappointed this isn't supported.
&#60;/p&#62;</description>
		</item>
		<item>
			<title>poslathian on "Strange errors when using `new` to allocate a pointer."</title>
			<link>http://forums.leaflabs.com/topic.php?id=161#post-1077</link>
			<pubDate>Tue, 07 Sep 2010 15:54:48 +0000</pubDate>
			<dc:creator>poslathian</dc:creator>
			<guid isPermaLink="false">1077@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;For some odd reason the &#34;new&#34; and &#34;delete&#34; keywords are highlighted in arduino/maple ide. We will fix this!
&#60;/p&#62;</description>
		</item>
		<item>
			<title>Amir Hadzic on "Strange errors when using `new` to allocate a pointer."</title>
			<link>http://forums.leaflabs.com/topic.php?id=161#post-1057</link>
			<pubDate>Tue, 07 Sep 2010 04:16:00 +0000</pubDate>
			<dc:creator>Amir Hadzic</dc:creator>
			<guid isPermaLink="false">1057@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;You'r right, I don't need it really. I just thought it might be a bug :-)
&#60;/p&#62;</description>
		</item>
		<item>
			<title>mbolivar on "Strange errors when using `new` to allocate a pointer."</title>
			<link>http://forums.leaflabs.com/topic.php?id=161#post-1052</link>
			<pubDate>Mon, 06 Sep 2010 19:28:56 +0000</pubDate>
			<dc:creator>mbolivar</dc:creator>
			<guid isPermaLink="false">1052@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;Hi Amir,&#60;/p&#62;
&#60;p&#62;You are correct, dynamic memory allocation is not supported (Arduino doesn't have it, either) -- and on a board with only 20K RAM, it might not really be what you want!&#60;/p&#62;
&#60;p&#62;Are you sure you need to dynamically allocate memory?  Perhaps for your application, statically allocating the object you need will work.
&#60;/p&#62;</description>
		</item>
		<item>
			<title>Amir Hadzic on "Strange errors when using `new` to allocate a pointer."</title>
			<link>http://forums.leaflabs.com/topic.php?id=161#post-1024</link>
			<pubDate>Mon, 06 Sep 2010 01:31:23 +0000</pubDate>
			<dc:creator>Amir Hadzic</dc:creator>
			<guid isPermaLink="false">1024@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;Hi!&#60;/p&#62;
&#60;p&#62;I'v created &#60;a href=&#34;http://forums.leaflabs.com/topic.php?id=159&#34;&#62;this&#60;/a&#62; topic last night and while playing with the code I noticed that I can't use the &#60;code&#62;new&#60;/code&#62; keyword to allocate pointers. I can't check the correct erros, I'm at work now but it was something about undefined references to _exit, _getpid or something like that. &#60;/p&#62;
&#60;p&#62;Thank you!
&#60;/p&#62;</description>
		</item>

	</channel>
</rss>
