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

  • Home
  • SEARCH
  • 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 60559
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T18:10:51+00:00 2026-05-10T18:10:51+00:00

I have worked on projects for embedded systems in the past where we have

  • 0

I have worked on projects for embedded systems in the past where we have rearranged the order of declaration of stack variables to decrease the size of the resulting executable. For instance, if we had:

void func() {     char c;     int i;     short s;     ... } 

We would reorder this to be:

void func() {     int i;     short s;     char c;     ... } 

Because of alignment issues the first one resulted in 12 bytes of stack space being used and the second one resulted in only 8 bytes.

Is this standard behavior for C compilers or just a shortcoming of the compiler we were using?

It seems to me that a compiler should be able to reorder stack variables to favor smaller executable size if it wanted to. It has been suggested to me that some aspect of the C standard prevents this, but I haven’t been able to find a reputable source either way.

As a bonus question, does this also apply to C++ compilers?

Edit

If the answer is yes, C/C++ compilers can rearrange stack variables, can you give an example of a compiler that definitely does this? I’d like to see compiler documentation or something similar that backs this up.

Edit Again

Thanks everybody for your help. For documentation, the best thing I’ve been able to find is the paper Optimal Stack Slot Assignment in GCC(pdf), by Naveen Sharma and Sanjiv Kumar Gupta, which was presented at the GCC summit proceedings in 2003.

The project in question here was using the ADS compiler for ARM development. It is mentioned in the documentation for that compiler that ordering declarations like I’ve shown can improve performance, as well as stack size, because of how the ARM-Thumb architecture calculates addresses in the local stack frame. That compiler didn’t automatically rearrange locals to take advantage of this. The paper linked here says that as of 2003 GCC also didn’t rearrange the stack frame to improve locality of reference for ARM-Thumb processors, but it implies that you could.

I can’t find anything that definitely says this was ever implemented in GCC, but I think this paper counts as proof that you’re all correct. Thanks again.

  • 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. 2026-05-10T18:10:52+00:00Added an answer on May 10, 2026 at 6:10 pm

    As there is nothing in the standard prohibiting that for C or C++ compilers, yes, the compiler can do that.

    It is different for aggregates (i.e. structs), where the relative order must be maintained, but still the compiler may insert pad bytes to achieve preferable alignment.

    IIRC newer MSVC compilers use that freedom in their fight against buffer overflows of locals.

    As a side note, in C++, the order of destruction must be reverse order of declaration, even if the compiler reorders the memory layout.

    (I can’t quote chapter and verse, though, this is from memory.)

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

Sidebar

Ask A Question

Stats

  • Questions 62k
  • Answers 62k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer Does cygwin's binary distribution of rsync recognize the ssh command?… May 11, 2026 at 10:10 am
  • added an answer Try: $('#mylist li').length Just curious: why do you need to… May 11, 2026 at 10:10 am
  • added an answer As a side note: You don't really have 2 cases.… May 11, 2026 at 10:10 am

Related Questions

I have worked on projects for embedded systems in the past where we have
I've worked on several PHP projects and always I have problems with organizing my
I have a few Visual Studio Solutions/Projects that are being worked on in my
A few years ago I have worked on a green field project where we
I believe several of us have already worked on a project where not only
I have worked on iPhone application but not aware of the formalities to submit
I have worked on single threaded business logic/back-end programming for most of my career.
I have now worked on two different teams that use the Agile/Scrum approach in
I might be an exception here but I have never worked on a team
My form receives asynchronous callbacks from another object on random worker threads. I have

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.