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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T16:01:41+00:00 2026-06-10T16:01:41+00:00

Here is a short snippet of a function reading lines. How is that possible

  • 0

Here is a short snippet of a function reading lines.
How is that possible that it compares bufsize with ((size_t)-1)/2 ?

I imagined comparing a variable to eg. int – that is just impossible; to INT_MAX on the contrary it is correct, I think.
So how can that code actually work and give no errors?

int c;
size_t bufsize = 0;
size_t size = 0;

while((c=fgetc(infile)) != EOF) {
    if (size >= bufsize) {
        if (bufsize == 0)
                bufsize = 2;
        else if (bufsize <= ((size_t)-1)/2)
                bufsize = 2*size;
        else {
                free(line);
                exit(3);
        }
        newbuf = realloc(line,bufsize);
        if (!newbuf) {
                free(line);
                abort();
        }
        line = newbuf;
    }
    /* some other operations */
}
  • 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-10T16:01:43+00:00Added an answer on June 10, 2026 at 4:01 pm

    The code relies on some assumptions about bits and then does a well known hack for finding the maximum size_t value (provided that size_t doesn’t accommodate more bits than the register, a safe bet on many machines).

    First it fills a register up with 1 bits, then it casts it into a size_t data type, so the comparison will work. As long as that register is larger in number of bits than the size_t data type, then the (if any) unused 1 bits will be truncated, and you will get the largest unsigned number that can fit in size_t bits.

    After you have that, it divides by two to get half of that number, and does the comparison to see if it seems to be safe to increase size without going over the “maximum” size_t. but by then, it’s dividing a size_t data type, and comparing two size_t data types (a type safe operation).

    If you really wanted to remove this bit-wizardy (ok, it’s not the worst example of bit wizardy I’ve seen). Consider that the following snippet

        else if (bufsize <= ((size_t)-1)/2)
                bufsize = 2*size;
    

    could be replaced with

        else if (bufsize <= (MAX_SIZE/2)
                bufsize = 2*size;
    

    and be type safe without casting and more readable.

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

Sidebar

Related Questions

So here's a snippet of my code: struct dv_nexthop_cost_pair { unsigned short nexthop; unsigned
Here is the short version. I have a Bonus Activity that has 25 image
Okay, here's my short question: I know that === and !== operators will compare
First question here: it is a very short yet fundamental thing in Java that
Here's the following algorithm: int encryption(int a, int b) { short int c, c2;
I understand we need to keep @Transactional boundaries as short as possible. Here is
Here is a short snippet of HTML (it contains all relevant HTML and javascript
Okay so here's my small snippet of code: base.$el.delegate('.mb-panel', 'hover', base.hover = function(){ base.change(
Here's an interesting puzzle. Below is an R snippet that identifies the tangency point
Here is a short XML file with 2 nodes having the same children (but

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.