Question about creating objects in arduino

Is there any way in which new objects of a class can be created periodically in an automatic way?

Local / non-static variables (including instances of objects) are created on the stack every time the function is entered.

Could you elaborate a little more, please?

You first, what do you really want to do?

It's for a little snake game I'm trying to make, and I want each segment of the snake to be an object so it is easier to control, but I don't know how to create a new segment when the food is eaten.

That's why I need to create new objects automatically.

Google for "C++ new operator"

Thank you.

Continuously creating and deleting dynamic object may lead to heap fragmentation problems on a low-memory AVR processor. You may be better of just allocating a static array of your objects (as many as you’re ever going to need) and keeping track of which ones are active at any given time.

Thank you very much for the help.

But I have a doubt:

I have read once that writing an array with as much space as I would ever need was wasting space and that it was better adding space as I go, that was why I was trying this way.

Could you clarify this if you don't mind?

I have read once that writing an array with as much space as I would ever need was wasting space and that it was better adding space as I go, that was why I was trying this way.

A very good question!

By NOT using the heap (new etc) you are forced to calculate the amount of RAM that you are using -

on systems with small amounts of RAM this is vital - you must know how much RAM you are using and why.

Many people who use String (for example) think what they get that space (in some way) for free!

By the way - use of the heap does NOT and cannot cause fragmentation!

So gfvalvo show us how this causes HEAP fragmentation! In fact PROVE that the HEAP is being used when a String is extened - hint look at how objects are returned in C++!

Mark

holmes4: By the way - use of the heap does NOT and cannot cause fragmentation!

That is a unique perspective. Soooooo.... malloc() and free() "cannot" fragment the heap??? You're really going to have to explain how you came to that conclusion! Regards, Ray L.

Thanks both for your answers…

Well, the board of my game is 8x8 in size so the limit in size for the snake would be 64, assuming the player would get that far, but the player will not get there.

So, I know the size limit, but the snake will not get there almost never.

Wouldn’t it be more wasting space and memory making a fixed array of size 64, instead of adding space as the game goes? Or is it still possible to get fragmentation?

Unless each time the game is played the memory is made in different places each time, it would be more waste of space.

Wouldn't it be more wasting space and memory making a fixed array of size 64, instead of adding space as the game goes? Or is it still possible to get fragmentation?

Remember that you are talking about a microcontroller that is only running one program, not some PC with many things trying to share memory. Many of the lessons for memory management for the PC are not going to apply the same here because this is a completely different environment.

AlexaDawn: Thanks both for your answers..

Well, the board of my game is 8x8 in size so the limit in size for the snake would be 64, assuming the player would get that far, but the player will not get there.

So, I know the size limit, but the snake will not get there almost never.

Wouldn't it be more wasting space and memory making a fixed array of size 64, instead of adding space as the game goes? Or is it still possible to get fragmentation?

Unless each time the game is played the memory is made in different places each time, it would be more waste of space.

If you can't reach maximum size with a maximum-sized statically allocated array, what makes you think you will be able to do it with a dynamically allocated array? Dynamic allocation wastes even MORE memory, due to the linked-list of headers that keeps track of each allocation. If you can't do it with a static list, you almost certainly cannot do it with a dynamic list. Regards, Ray L.

RayLivingston: If you can't reach maximum size with a maximum-sized statically allocated array, what makes you think you will be able to do it with a dynamically allocated array? Dynamic allocation wastes even MORE memory, due to the linked-list of headers that keeps track of each allocation. If you can't do it with a static list, you almost certainly cannot do it with a dynamic list. Regards, Ray L.

Sorry, what I meant was that if in 100 games the snake's size ends up being at the most 20 from 64, the rest of the space would never be used, and that adding only the necessary space would be better.

With your answer I see now that in both cases I'm wrong.

I think I will use fixed size arrays. Thanks for the help. Couldn’t have done it without it.