How to re-instantiate a global struct instance?

Experts,

I'll try to frame this in simple code:

struct A {
    int one;
    int two;
} aStruct;

aStruct has a global scope. How can I either delete it's variables/attributes, re-instantiate it, or something else that essentially deletes it's values as it was, allowing you to start over without worrying about old values in those variables/attributes?

My use case involves a global instance of a struct. I do this so setup(), loop(), and other functions/methods can use it easily.
-But one function might want to start over again, re-populating the instance-without the old values.
-I guess you could say I'm trying to re-use the memory taken up by a single instance. Failing in that, destroying the space the original took, and carving out more for a second instance, but with the same scope.

Is doing this along the lines of this post ("How to create a global server object AFTER executing setup()")?

Please forgive my ignorance with the subtleties of C/C++. I'm hoping this is a fitting topic for the 'Programming Questions' forum.

TIA!

  memset( &aStruct, 0, sizeof(aStruct) );

I guess you meant this post in your link: [SOLVED] How to create a global server object AFTER executing setup() - Programming Questions - Arduino Forum

You've probably oversimplified the example so it is not expressing fully what you mean. Otherwise this could also be a possible solution:

aStruct.one = newValue1 ;
aStruct.two = newValue2 ;

6v6gt:
I guess you meant this post in your link: [SOLVED] How to create a global server object AFTER executing setup() - Programming Questions - Arduino Forum

You've probably oversimplified the example so it is not expressing fully what you mean.

Fair enough. Let's just simplify things to a question of re-use.
If I want to re-use instanceA, and not create another one, I assume there are 2 choices:

  1. Delete/release the memory for the initial (first) instance; Create a new one (instanceB); trust garbage collection,
  2. Write code to go through every attribute in the sctruct and zero/null each out (yuk!),
  3. Create a new instance of the struct with the same instance name as the first one (compilers no like this),
  4. ?
    I originally thought that simply releasing the memory by deleting the instance would be the most economical, but I couldn't find a way in arduino c/c++ to do this as a method of the instance. In writing for the ATMega series, this probably is never a concern, but I'm targeting the ATTiny series to see what I can do with no such luxury (512 vs. 2048).
    As a non-expert, releasing/re-instantiating seems to me the fastest, most efficient way-far easier than writing a line of code for each possible attribute in the struct. But I am not as knowledgeable as you all are. :slight_smile:
    So how do you conserve SRAM? 512 can go mighty fast! :slight_smile:
    TIA!

holesflow:
I originally thought that simply releasing the memory by deleting the instance would be the most economical, but I couldn't find a way in arduino c/c++ to do this as a method of the instance.

// Allocate memory from the heap
aStruct* first = new aStruct;

// What happens if new fails?

// Use it
first->one = 1;
first->two = 2;

// Release the memory
delete first;

A slightly more efficient version (that does not call the constructor / destructor)...

// Allocate memory from the heap
aStruct* first = malloc( sizeof aStruct );

if ( first != NULL )
{
  // Use it
  first->one = 1;
  first->two = 2;

  // Release the memory
  free( first );
}

Bear in mind that pulling in the heap management code will consume considerably more SRAM than your four byte structure.

@CodingBadly,
Thanks for writing!

aStruct* first = new aStruct;

In early CS classes, asterisks/pointers gave me headaches! So you are saying here to point to the first instance, then change values? I'd have to still change every value in the struct, though, correct?

holesflow:
So you are saying here to point to the first instance...

No. aStruct* is the type. A pointer to a aStruct.

It is also a mistake. I should have used the tag...

  struct A * first = new struct A;

Or, made use of a typedef...

typedef struct A {
    int one;
    int two;
} aStruct_t;

...
  // Allocate memory from the heap
  aStruct_t* first = new aStruct_t;

I apologize for the mistake.

I'd have to still change every value in the struct, though, correct?

I don't understand the question.