0
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.

2
  • 2
    You are strongly encouraged not to use dynamic memory allocations. Due to the low ammount of memory you will get into trouble if you are not careful. Try to avoid calls like new and malloc. instead you should use Object object(args); to initialize your object in the loop. It will be disposed at the end of the loop automatically.
    – Kwasmich
    Mar 7 '18 at 9:02
  • 1
    This code is not proper C/C++, it should be Object* object = new Object(); otherwise this would not even compile.
    – jfpoilpret
    Mar 8 '18 at 6:16
2

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).

4
  • 1
    maybe also mention cleaning up the object when using new Mar 7 '18 at 9:06
  • @ratchetfreak I already put a notice for that, but I extended the comment slightly. Mar 7 '18 at 9:08
  • 1
    Did you perhaps mean memory leak instead of memory breach? First time I hear "memory breach", google does not seem to know what a memory breach is neither.
    – wondra
    Mar 7 '18 at 9:48
  • @wondrea memory leak is indeed a better word, I change it Mar 7 '18 at 9:54
1

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).

1
  • reason for the down-vote?
    – Juraj
    Mar 8 '18 at 7:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.