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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T19:13:03+00:00 2026-06-10T19:13:03+00:00

The situation: I have a piece of code that works when compiled for 32-bits

  • 0

The situation:
I have a piece of code that works when compiled for 32-bits but fails when compiled for 64-bits with gcc 4.6. After identifying the problem and reading up on the standards, I cannot really understand why it is working for 32-bits. I hope someone can explain what is going on.

The code (somewhat simplified and cut down to the interesting parts):

// tbl: unsigned short *, can be indexed with positive and negative values
// v: unsigned int
// p: unsigned char *
tmp = tbl[(v >> 8) - p[0]]; // Gives segfault when not compiled with -m32

When compiled with -m32 the code works. When compiled without -m32 it gives a segfault. The reason for the segfault is that (v >> 8) - p[0] is interpreted as an unsigned int when compiled for 64-bit which for “negative” results will be way off.

According to this question, the C99 standard says the following:
6.2.5c9: A computation involving unsigned operands can never overflow, because a result that cannot be represented by the resulting unsigned integer type is reduced modulo the number that is one greater than the largest value that can be represented by the resulting type.

From this it seems that unsigned minus unsigned will always result in unsigned output which is consistent with what happens in the 64-bit case. This does not seem to happen in the 32-bit case which is what I find extremely strange.

Can anyone explain what is happening in the 32-bit case?

  • 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-10T19:13:04+00:00Added an answer on June 10, 2026 at 7:13 pm

    In both cases you get a very large number because the unsigned int wraps around, but in the 32-bit case, the pointer arithmetic also wraps around, and so it cancels out.

    In order to do the pointer arithmetic, the compiler promotes the array index to the same width as the pointer. So for unsigned with 32-bit pointers, you get the same result as int with 32-bit pointers.

    For example,

    char *p = (char *) 0x1000;
    
    // always points to 0x0c00
    // in 32-bit, the index is 0xfffffc00
    // in 64-bit, the index is 0xfffffffffffffc00
    int r = p[(int) -0x400]; 
    
    // depends on architecture
    // in 32-bit, the index is 0xfffffc00 (same as int)
    // in 64-bit, the index is 0x00000000fffffc00 (different from int)
    int r = p[(unsigned) -0x400];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have seen that the situation is very problematic after reading these: IE6/IE7 css
I have a situation where there is a small piece of Java code that
So here's the problem. I have a piece of code that when execute in
Using Python 2.7 Situation: I have some piece of Python code (that the user
In a new piece of code I have several different classes that refer to
Situation I have a client library that uses the Windows Azure AppFabric Service Bus
I have situation, where running a query that filters by an indexed column in
I've got this nifty little piece of code here that self-propogates ad infinitum every
This is a pretty odd situation I have here. I have used a piece
I would like to have this piece of code in my .aspx file: <input

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.