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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T15:12:05+00:00 2026-05-30T15:12:05+00:00

Consider the following code: #include <stdlib.h> #ifndef TRY #define TRY struct #endif TRY testme

  • 0

Consider the following code:

#include <stdlib.h>

#ifndef TRY
#define TRY struct
#endif

TRY testme
{
  int one;
  int two;
  char three;
  int four;
};

int
main (void)
{
  {
    volatile TRY testme one;

    one.one = 2;
    one.three = 7;
  }

  {
    volatile TRY testme twos;

    twos.one = 3;
  }

  {
    volatile TRY testme one;

    one.one = 4;
  }

  {
    volatile TRY testme twos;

    twos.one = 5;
  }

  {
    volatile TRY testme twos;

    twos.one = 6;
  }

  {
    volatile TRY testme twos;

    twos.one = 6;
  }

  return EXIT_SUCCESS;
}

Compiled as is for x86 is (meaning testme is a struct), the stack size the compiler allocates for main is 16 bytes.

$ gcc -g -O2 test.c -o test 
$ objdump -d ./test | ./checkstack.pl i386 | grep main
16 main

However, compiled with TRY defined to union (meaning testme is a union), the stack size the compiler allocates for main is 32 bytes:

$ gcc -DTRY=union -g -O2 test.c -o test 
$ objdump -d ./test | ./checkstack.pl i386 | grep main

Moreover, any additional instances of the struct/union defined in additional scopes, will produce bigger stack allocations when using a union, but will not enlarge the stack allocation when used as a struct.

Now, this does not make sense – the union should take less stack space, if at all, not more, then a struct with the same fields!

It seems as if GCC treats unions as used concurrently even when in different scopes, but does not do the same for structs.

Some more clarifications:

  1. volatile is used to stop the compiler from optimizing away the assignments. Loosing the volatile and compiling with no optimization produces the same results.

  2. Even if testme is a struct that has a union as one of the members, the same behavior is observed. In other words – it is enough that one of the members of a struct is a union for GCC to for separate stack allocations.

  3. Compiler is gcc version 4.4.3 (Ubuntu 4.4.3-4ubuntu5) but other GCC versions for other architectures showed the same behavior.

  4. checkstack.pl simply searches the objdump output for the instructions used to allocate stack (sub to the stack pointer).

My question:

  1. Why does GCC do this? is this a bug or is there a reason for this behavior?
  2. Assuming this is not a bug, is a there a way to work around this and force GCC to allocate stack for stucts same as unions.

Clarification: My question is not why the struct or union appears to be bigger in size from the size of its part. I understand the reason is padding for alignment. My problem is that the compiler allocates multiple stack frames for different instances of the union even though they are defined in different scopes while it shouldn’t and indeed doesn’t do the same for a struct with the same fields.

Thanks!

  • 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-30T15:12:07+00:00Added an answer on May 30, 2026 at 3:12 pm

    Apparently at least an attempt has been made to relax gcc’s strict aliasing paranoia regarding unions.

    You may wish to make sure that gcc source you compile from has this or equivalent patch applied:
    http://codereview.appspot.com/4444051/

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

Sidebar

Related Questions

Consider the following code. #include <stdio.h> #include <vector> #include <iostream> struct XYZ { int
Consider the following code: #include <stdio.h> #include <ctype.h> char* Mstrupr(char* szCad); int main() {
Consider the following code: #include<iostream> #include<utility> struct Base { int baseint; }; struct Der1
Consider the following code: #include <stdio.h> int main (void) { char str1[128], str2[128], str3[128];
Consider the following code: #include <vector> struct S { int a; double b; };
Consider the following code: #include <algorithm> #include <iostream> #include <vector> struct A { int
Consider the following code: #include <vector> struct A { explicit A(int i_) : i(i_)
Consider the following code: #include <iostream> struct A { const char *name; A() :
Consider the following code: #include <iostream> using namespace std; int main() { int x,
Consider the following code: #include <iostream> struct foo { // (a): void bar() {

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.