user43489: while(condition){ 
    Object object = new Object();
}

So my question is, will each iteration would create a different object in different memory location or it will overwrite the once created object every iteration.

user37622: Dynamic object creation is using operator new. Operator new creates object on heap and the object exits until you delete it with operator delete.
Object object creates object on stack. It will be removed from stack when the variable goes out of scope on the end of the block.
Some classes have copy constructors. If your Object object = new Object() compiles, then the class Object has copy constructor. The code creates an object on stack, then creates an object on heap and then copies it's content into object on stack. The object on heap stays and you have no pointer to it to delete it. The program soon crashes on low memory.
Static variables have keyword static. If you would have static Object* object = new Object(), then the pointer is created on global level and the object on heap is created and assigned only ones.
EDIT:
I tested this. Class String has a copy constructor, but the Arduino compiler doesn't allow String s = new String(a). It can be misleaded with String s = *(new String(a)) (a is some variable).

user31105: It will be created every iteration.
Assuming, you are not using it to add to a list (e.g. to use in later loops), what you better can do is make it in a global, you also don't need to use 'new', thus:
Object object;

Also, if you do not delete/remove the object at the end of each while condition (with the delete keyword), you will lose the memory (and this is a memory leak). Also, you run the risk of memory shortage (the Uno has only a few KB of memory).
What I also miss in your code is the pointer (*, because the new keyword creates a pointer), and also Obj and Object are inconsistent types (they should be equal), so:
Object* object = new Object();

If you only need the object within the condition and it can be removed in the next while, than you don't need to use new, so just declare it as a vaiable
Object object;

This also has the benefit that the (local) variable will be destroyed at the end of scope (which is at the end of the while).

43489: <pre><code>while(condition){ 
    Object object = new Object();
}
</code></pre>

<p>So my question is, will each iteration would create a different object in different memory location or it will overwrite the once created object every iteration.</p>
 37622: <p><strong>Dynamic</strong> object creation is using operator <code>new</code>. Operator <code>new</code> creates object on <em>heap</em> and the object exits until you delete it with operator <code>delete</code>.</p>

<p><code>Object object</code> creates object on <em>stack</em>. It will be removed from stack when the variable goes out of scope on the end of the block.</p>

<p><strong>Some classes have copy constructors. If your <code>Object object = new Object()</code> compiles, then the class Object has copy constructor. The code creates an object on stack, then creates an object on heap and then copies it's content into object on stack. The object on heap stays and you have no pointer to it to delete it. The program soon crashes on low memory.</strong></p>

<p><strong>Static</strong> variables have keyword static. If you would have <code>static Object* object = new Object()</code>, then the pointer is created on global level and the object on heap is created and assigned only ones.</p>

<p>EDIT:
I tested this. Class String has a copy constructor, but the Arduino compiler doesn't allow <code>String s = new String(a)</code>. It can be misleaded with <code>String s = *(new String(a))</code> (<code>a</code> is some variable).</p>
 31105: <p>It will be created every iteration.</p>

<p>Assuming, you are not using it to add to a list (e.g. to use in later loops), what you better can do is make it in a global, you also don't need to use 'new', thus:</p>

<pre><code>Object object;
</code></pre>

<p>Also, if you do not delete/remove the object at the end of each while condition (with the <em>delete</em> keyword), you will lose the memory (and this is a memory leak). Also, you run the risk of memory shortage (the Uno has only a few KB of memory).</p>

<p>What I also miss in your code is the pointer (*, because the <em>new</em> keyword creates a pointer), and also Obj and Object are inconsistent types (they should be equal), so:</p>

<pre><code>Object* object = new Object();
</code></pre>

<p>If you only need the object within the condition and it can be removed in the next while, than you don't need to use new, so just declare it as a vaiable</p>

<pre><code>Object object;
</code></pre>

<p>This also has the benefit that the (local) variable will be destroyed at the end of scope (which is at the end of the while).</p>
