Consider that I have a struct as below. I want to reset this struct.
struct StProduct {
int weight;
int price[100];
StProduct():
weight( 0 ),
price( ) {}
};
This way to reset the struct is given in a lot of examples and works :
StProduct apple;
// some code ...
apple = StProduct();
Now we'll dynamically allocate. Is it valid to reset the struct like this:
StProduct* pbanana = new StProduct();
// some code ...
*pbanana = StProduct();
Or could it be that there is some undefined behavior involved?
Clarification of the embedded nature of the question:
The question is really only the one above, and can be answered as such.
People seem to be concerned about a Minimal, Complete, and Verifiable example.
Therefore let me add the following explanation:
My x86 unit tests with the code above work. But on an embedded device (STM32 F427) with gcc 4.7.6, an RTOS and having the struct as permanent buffer in the core coupled memory, the line *pbanana = StProduct(); in the code called every 100ms leads to a crash after roughly 30min, while commenting this line out has the device running for 3 days now. This is why I was asking, if the code above might be associated with undefined behaviour. Since it apparently is not, I am thankful for the comments and have to look somewhere else - and my bet is on a compiler bug. If someone has any idea how I can provide a Minimal, Complete, and Verifiable example that does not involve sending hardware to everyone, I'm all ears.