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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T17:30:28+00:00 2026-05-23T17:30:28+00:00

Can I assume the following invariant? void foo(char *buf, size_t len) { // buf

  • 0

Can I assume the following invariant?

void foo(char *buf, size_t len) {
  // "buf" points to either an array or memory allocated with malloc().
  assert((uintptr_t)(buf + len) < UINTPTR_MAX);
}

In a parser I am writing I want to mark certain offsets using pointers: for example I might have char *end_of_submessage, where end_of_submessage is relative to my current buffer. But if the submessage does not end inside the current buffer, I want to use a value that is larger than any offset in the current buffer could possibly be. So I would do:

void parse(char *buf, size_t len, uintptr_t end_of_submessage) {
  // Some parsing that might increment "buf"
  // ...

  // If end_of_submessage == UINTPTR_MAX, processing will not be
  // triggered even if we have processed our entire current buffer.
  if ((uintptr_t)buf >= end_of_submessage)
    process_submsg_end();
}

But this scheme would be thwarted if malloc() returned memory such that ptr + len == UINTPTR_MAX, or an array had the same property. Is it safe to assume this will never happen? Is it safe according to the standard? If not, is it safe in practice?

  • 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-23T17:30:29+00:00Added an answer on May 23, 2026 at 5:30 pm

    The only guarantee that the C standard provides is follows:

    ISO/IEC 9899:1999(E) §7.18.1.4/1

    The following type designates a signed integer type with the property
    that any valid pointer to void can be converted to this type, then
    converted back to pointer to void, and the result will compare equal
    to the original pointer:

    intptr_t
    

    The following type designates an
    unsigned integer type with the property that any valid pointer to void
    can be converted to this type, then converted back to pointer to void,
    and the result will compare equal to the original pointer:

    uintptr_t
    

    These types are optional.

    No guarantees are given about the precise content of these converted integers. In particular, given a char pointer p, (uintptr_t)(p + 1) == ((uintptr_t)p) + 1 is not guaranteed to be true.

    If you want to mark offsets, you should use a ptrdiff_t offset from another pointer, or you can simply use a pointer to mark the end. For example:

    void parse(char *buf, size_t len, char *end_of_submessage)
    {
    // ...
        if (buf >= end_of_submessage)
            process_submsg_end();
    }
    

    If end_of_submessage may lie in a different buffer, you can use something like:

    char *buf_start = buf;
    // ...
    if (buf_start <= end_of_submessage && buf >= end_of_submessage)
      process_submsg_end();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider the following small example: template<typename T> void foo(T a) { ... } struct
How does MySQL store a varchar field? Can I assume that the following pattern
Hopefully I can explain this well. Assume the following: @interface ClassA : NSObject {
In Java, I can do the following: (assume Subclass extends Base ): ArrayList<? extends
Trying to figure out this pseudo code. The following is assumed.... I can only
Consider these 2 pieces of code (you can assume execeptionObj is of type Object
First, I can assume that all urls that end with jpeg, jpg, bmp, png
This is an Eclipse question, and you can assume the Java package for all
Update - for those of a facetious frame of mind, you can assume that
Okay, I've seen but haven't programmed in C# before. You can assume I'm competent

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.