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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T17:48:33+00:00 2026-06-05T17:48:33+00:00

I have the following program. I wonder why it outputs -4 on the following

  • 0

I have the following program. I wonder why it outputs -4 on the following 64 bit machine? Which of my assumptions went wrong ?

[Linux ubuntu 3.2.0-23-generic #36-Ubuntu SMP Tue Apr 10 20:39:51 UTC
2012 x86_64 x86_64 x86_64 GNU/Linux]

  1. In the above machine and gcc compiler, by default b should be pushed first and a second.
    The stack grows downwards. So b should have higher address and a have lower address. So result should be positive. But I got -4. Can anybody explain this ?

  2. The arguments are two chars occupying 2 bytes in the stack frame. But I saw the difference as 4 where as I am expecting 1. Even if somebody says it is because of alignment, then I am wondering a structure with 2 chars is not aligned at 4 bytes.

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

void CompareAddress(char a, char b)
{
    printf("Differs=%ld\n", (intptr_t )&b - (intptr_t )&a);
}

int main()
{
    CompareAddress('a','b');
    return 0; 
}

/* Differs= -4 */
  • 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-05T17:48:35+00:00Added an answer on June 5, 2026 at 5:48 pm

    The best way to answer these sort of questions (about behaviour of a specific compiler on a specific platform) is to look at the assembler. You can get gcc to dump its assembler by passing the -S flag (and the -fverbose-asm flag is nice too). Running

    gcc -S -fverbose-asm file.c
    

    gives a file.s that looks a little like (I’ve removed all the irrelevant bits, and the bits in parenthesis are my notes):

    CompareAddress:
            # ("allocate" memory on the stack for local variables)
            subq    $16, %rsp       
            # (put a and b onto the stack)
            movl    %edi, %edx      # a, tmp62
            movl    %esi, %eax      # b, tmp63
            movb    %dl, -4(%rbp)   # tmp62, a
            movb    %al, -8(%rbp)   # tmp63, b 
            # (get their addresses)
            leaq    -8(%rbp), %rdx  #, b.0
            leaq    -4(%rbp), %rax  #, a.1
            subq    %rax, %rdx      # a.1, D.4597 (&b - &a)
            # (set up the parameters for the printf call)
            movl    $.LC0, %eax     #, D.4598
            movq    %rdx, %rsi      # D.4597,
            movq    %rax, %rdi      # D.4598,
            movl    $0, %eax        #,
            call    printf  #
    
    main:
            # (put 'a' and 'b' into the registers for the function call)
            movl    $98, %esi       #,
            movl    $97, %edi       #,
            call    CompareAddress
    

    (This question explains nicely what [re]bp and [re]sp are.)

    The reason the difference is negative is the stack grows downward: i.e. if you push two things onto the stack, the one you push first will have a larger address, and a is pushed before b.

    The reason it is -4 rather than -1 is the compiler has decided that aligning the arguments to 4 byte boundaries is “better”, probably because a 32 bit/64 bit CPU deals with 4 bytes at time better than it handles single bytes.

    (Also, looking at the assembler shows the effect that -mpreferred-stack-boundary has: it essentially means that memory on the stack is allocated in different sized chunks.)

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

Sidebar

Related Questions

I have the following c program which launches a Gtk Program on ubuntu: #include
I am cross compiling linux kernel. I have the following program which compiles perfectly
I have the following program which compiles fine and with no errors or warnings:
I have this following program which gets the number of files from user, then
I have following program in which I am adding few number to set and
I have the following program in which I'm trying to understand the functioning of
I have following simple program: import std.stdio; int main(string[] argv) { writeln(Hello, world!); return
I have the following program to open lot's of sockets, and hold them open
I have the following program to browse all virtual directories and their sub directories
Hi I have the following program. When I compile on the terminal gcc main.c

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.