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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T10:07:02+00:00 2026-06-10T10:07:02+00:00

char *buf = malloc(bufsize) char *ptr = buf; … while(condition) { ptrdiff_t offset =

  • 0
char *buf = malloc(bufsize)
char *ptr = buf;
…
while(condition) {
    ptrdiff_t offset = ptr - buf;    // <========== THIS LINE

    // offset will never be negative because we only ever *increase* ptr
    if ((size_t)offset > bufsize) {
        // we need more room
        bufsize += 128;
        buf = realloc(buf, bufsize);
        ptr = buf + offset;  // buf might be in a completely new location
    }
    *ptr++ = …  // write this byte
}

Is this valid or undefined?

I would have assumed that it’s valid, but I read something about it being undefined, so I googled it. These links seem to inescapably claim it’s undefined:

  • Secure coding
  • Is subtraction of pointers not pointing to different elements of same array valid in C?

However, no mention of it is made in these SO questions:

  • Pointer subtraction confusion
  • size_t return pointer subtraction
  • Pointer Arithmetic In C

These all talk about not two pointers being in the same “array”. Does that actually mean a plain old C array on the stack?

If it is undefined, it seems very odd to me… Why force me to carry along a counter variable when I have access to one constant pointer and one moving pointer?

  • 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-10T10:07:04+00:00Added an answer on June 10, 2026 at 10:07 am

    Pointers into a block of memory returned by malloc count as being into the same array:

    c11

    7.22.3 Memory management functions

    1 – The
    pointer returned [from malloc] if the allocation succeeds […] may be assigned to
    a pointer to any type of object […] and then used
    to access such an object or an array of such objects in the space allocated (until the space
    is explicitly deallocated).

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

Sidebar

Related Questions

I have a large buffer: char *buf = malloc(1000000000); // 1GB If I forked
if I read file by calling read() like this: unsigned char buf[512]; memset(buf, 0,
I'm reading a C code that do char * buf = malloc(sizeof (char *)
Suppose I have a char buf[12]; which I know will always have a right
In the following code,what is the meaning of buf = malloc(n * sizeof(char)); is
I have this code: string get_md5sum(unsigned char* md) { char buf[MD5_DIGEST_LENGTH + MD5_DIGEST_LENGTH]; char
//child process char buf[20]; read(fd[0][0], buf, 20); printf(%s, buf); //parent process write(fd[0][1], 12, 20);
I have two variables: char charTime[] = TIME; char buf[] = SOMETHINGELSE; I want
I have the following C code: typedef void (*mycallback) (char buf[128]); void take_callback(mycallback cb)
I' ve got such a function static ssize_t read_mydevice(struct file *filp, char* buf, size_t

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.