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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T21:45:57+00:00 2026-05-31T21:45:57+00:00

In a system I am maintaining, users request elements from a collection from a

  • 0

In a system I am maintaining, users request elements from a collection from a 1-based indexing scheme. Values are stored in 0-based arrays in C++ / C.

Is the following hypothetical code portable if 0 is erroneously entered as the input to this function? Is there a better way to validate the user’s input when converting 1-based numbering schemes to 0-based?

const unsigned int arraySize;
SomeType array[arraySize];    

SomeType GetFromArray( unsigned int oneBasedIndex )
{
  unsigned int zeroBasedIndex = oneBasedIndex - 1;

  //Intent is to check for a valid index.
  if( zeroBasedIndex < arraySize )
  {
    return array[zeroBasedIndex];
  }

  //else... handle the error
}

My assumption is that (unsigned int)( 0 - 1 ) is always greater than arraySize; is this true?

The alternative, as some have suggested in their answers below, is to check oneBasedIndex and ensure that it is greater than 0:

const unsigned int arraySize;
SomeType array[arraySize];    

SomeType GetFromArray( unsigned int oneBasedIndex )
{
  if( oneBasedIndex > 0 && oneBasedIndex <= arraySize )
  {
    return array[oneBasedIndex - 1];
  }

  //else... handle the error
}
  • 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-31T21:45:58+00:00Added an answer on May 31, 2026 at 9:45 pm

    For unsigned types, 0-1 is the maximum value for that type, so it’s always >= arraySize. In other words, yes that is absolutely safe.

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

Sidebar

Related Questions

I assist in maintaining an enterprise web-based system (programmed in J2EE, but this is
I'm maintaining some existing pages and came across a bit of code from System.Threading
Looking at some code I'm maintaining in System Verilog I see some signals that
I'm currently maintaining an "old" system written in C# .NET, removing some obsolete features
A programmer on your team is great at maintaining the old legacy system. But
System.IO.BinaryReader reads values in a little-endian format. I have a C# application connecting to
I'm maintaining this Swing app that has a print option. Users need to be
I have a system set up for users to submit their articles into my
Within two months were are going live with a web system coded from scratch.
I am maintaining a custom Linux kernel which is comprised of merged changes from

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.