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 8454909
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T12:08:54+00:00 2026-06-10T12:08:54+00:00

Programming a 8-bit AVR microcontroller I’ve came across a behavior that is shown on

  • 0

Programming a 8-bit AVR microcontroller I’ve came across a behavior that is
shown on this code:

  class classA
  {
      public:
          classA(Display *d) : _d(d) { _d->println("classA()", 0); }
          ~classA() { _d->println("~classA()", 1); }
          uint8_t array[200];
          Display *_d;
  };
  void useClassA(classA *a)
  {
      a->array[3] = 5;
  }
  void SomeClass::start()
  {
      SYSTEM_DISPLAY_FREE_RAM();
      debugMethod();
      _ui->lcd().println("after debugMethod", 3);
      SYSTEM_WAIT_DEBUG_BUTTON();
      SYSTEM_DISPLAY_FREE_RAM();
  }
  void SomeClass::debugMethod()
  {
      _ui->lcd().println("entered debugMethod", 3);
      SYSTEM_WAIT_DEBUG_BUTTON();
      SYSTEM_DISPLAY_FREE_RAM();

      _ui->lcd().println("before while", 3);
      SYSTEM_WAIT_DEBUG_BUTTON();
      volatile uint8_t i = 1;
      while (i != 0) 
      {
          classA cA(&_ui->lcd());
          SYSTEM_DISPLAY_FREE_RAM();
          cA.array[199] = i--;
          useClassA(&cA);
      }
      _ui->lcd().println("after while", 3);
      SYSTEM_WAIT_DEBUG_BUTTON();
      SYSTEM_DISPLAY_FREE_RAM();
  }

SYSTEM_DISPLAY_FREE_RAM() calculates available RAM as described in
http://jeelabs.org/2011/05/22/atmega-memory-use/.
When execution reaches SomeClass::start() I got the following output:

  Free Ram: 2677
  entered debugMethod
  Free Ram: 2458
  before while
  classA()
  Free Ram: 2458
  ~classA()
  after while
  Free Ram: 2458
  after debugMethod
  Free Ram: 2677

Despite classA object is created and destructed inside the while, the
memory seems to be allocated at the begging of debugMethod(), and remains
until the method ends. I expected memory to be allocated only inside of the
while, thus having a sigle print with Free Ram: 2458.

Any explanations of what is going on?

Is there a way to force the allocation to happen inside the while, without
using new keyword?

Compiler used: avr-gcc (WinAVR 20100110) 4.3.3

  • 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-06-10T12:08:55+00:00Added an answer on June 10, 2026 at 12:08 pm

    Typically the stack frame for the whole function is allocated at the start of the function.
    You can try the gcc argument --param min-pretend-dynamic-size=100
    which will try to dynamically allocate and deallocate stack for objects over 100 bytes [1].

    gcc can show you the assembly code with the -S switch, look at that to see under the hood on what’s going on, and whether the –param min-pretend-dynamic-size had any effect on your platform and function.

    Another solution in your case would be to move the body of your while() loop into a new function, as that would create/destroy the stack frame containing the classA object.

    [1] gcc docs:

    min-pretend-dynamic-size

    Force any automatic object whose size in bytes is equal to or greater
    than the specified value to be allocated dynamically, as if their size
    wasn’t known to compile time. This enables their storage to be
    released at the end of the block containing them, reducing total stack
    usage if multiple functions with heavy stack use are inlined into a
    single function. It won’t have any effect on objects that are suitable
    for allocation to registers (i.e., that are sufficiently small and
    that don’t have their address taken), nor on objects allocated in the
    outermost block of a function. The default, zero, causes objects whose
    sizes are known at compile time to have storage allocated at function
    entry.

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

Sidebar

Related Questions

I'm programming an iPhone app that uses a little bit of javascript code for
I've read http://dbaspot.com/sqlserver-programming/463313-32-bit-clr-procedure-64-bit-sql-server.html Does that mean it's not possible to run a 32 bits
We all hear that math at least helps a little bit with programming. My
I am programming a AVR MCU. It has a POT that reads off an
This might be bit non programming related but thought to add it here. I
Hello I am learning a bit of droid programming, specifically for class I am
I am still learning quite a bit about programming so this may be an
I am starting to get a bit bored of programming little toys that I
What is this concept called in programming? Particularly the bit in the constructor. I'd
a bit new to programming and had a general question that I just thought

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.