Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 628281
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T19:37:10+00:00 2026-05-13T19:37:10+00:00

I have a struct call ‘A’, which has an attribute ‘i’, like this: typedef

  • 0

I have a struct call ‘A’, which has an attribute ‘i’, like this:

typedef struct a {
   a() { i = 0;}
   int i;
} A;

And I would like to maintain a stack of A in my Main class:

class Main {
   public:  
      void save();
      void doSomethingToModifyCurrentA();
      void restore();

   private:
     A currentA;
     stack<A> aStack;
  }

I would like to write the function save() which save the current values of A (e.g. i) to the stack and I can go on doSomethingToModifyCurrentA() to change currentA. And then later on, I can restort A by calling restore().

My question are

  • How can I allocate memory for a copy of A to the ‘stack’?
  • How can I pop out the copy of A and free the memory and restore the value of ‘currentA’?
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T19:37:11+00:00Added an answer on May 13, 2026 at 7:37 pm

    You don’t need to do anything to reserve memory on the stack. The underlying container (deque by default) manages memory for you.

    The three important methods are…

    mystack.push (myvalue);
    mystack.top ();
    mystack.pop ();
    

    The pop doesn’t read the top value – just discards it. The top method returns a reference to the current top value, so you can write…

    ??? = mystack.top ();
    mystack.top () = ???;
    

    To read or overwrite the top value.

    These methods translate to the following calls in the underlying deque…

    mydeque.push_back (myvalue);
    mydeque.back ();
    mydeque.pop_back ();
    

    Personally, I usually just use the deque directly – though strictly, the stack is better for readability and maintainability as it better expresses the intent and prevents you from doing some things incompatible with that intent.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Ok so I have struct like this typedef struct { float x; float y;
I have a struct Primitive, which has the following definition: typedef struct Primitive {
I have a struct which corresponds to a register in hardware. typedef unsigned int
Let's suppose I have a struct like this: struct my_struct { int a; int
I have a type defined like this: struct DynTab_s { int object_count; //other fields
I have this struct typedef struct grade { char firstName[SIZE]; char lastName[SIZE]; int stuNum;
I have a struct like this: typedef struct string { unsigned long length; unsigned
I have a struct like this: struct MESGE TEXT db 'Message',0 mLen db 8
so i have a struct call Process_Info struct Process_Info { char name[128]; int pid;
I want to call a function low_level_init which has signature void low_level_init(struct netif *netif)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.