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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T10:31:04+00:00 2026-05-29T10:31:04+00:00

I’m writing a wrapper that forks an execv() process. Output from the child is

  • 0

I’m writing a wrapper that forks an execv() process. Output from the child is captured in stderr. When waitpid() releases, I can read the contents of stderr and report it back.

In my case, I want to dynamically allocate a buffer and write stderr into this buffer.

To size the buffer I can realloc(), but this is not very efficient and I have found it tends to bloat memory pool use. Rather, I’d like to know the size without altering its pointer.

Consider the following, and note that MyStderr is just a placeholder for stderr:

int size=0;
int ch=0;
// int MyStderr is the stderr fd
FILE *nCountFD = fdopen(MyStderr, "r");
while ((ch = getc(nCountFD)!=EOF)
{
    ++size;
}
printf("Size is %ld\n", size);

Here I get the size. However, now the file pointer for MyStderr is at the end of its buffer.

I tried using lseek.

lseek() fails against stderr, so I can’t use it here. At least, this is what my testing and stackoverflow searching indicates.

So …

  1. Is there a way to get the size without moving MyStderr to eof ?

or

  1. Is there an lseek method that will work with MyStderr ?

Note. Here is the only solution I can think of, using realloc..

char *buf=(char*)malloc(80);
char *NewBuf=NULL;
int n=80;
while ((ch = getc(nCountFD)!=EOF)
{
    buf[i]=ch;
    ++size;
    if (size>n)
    {
       n=n+80;
       NewBuf= realloc(buf, n);
       // some code to make sure it works here //
       buf=NewBuf;
    }
}
printf("Size is %ld\n", size);

And now the update

Rather than build the functionality to work around the fact that stderr is unbuffered, I determined to make the initial malloc of my result buffer sufficiently large so that realloc() would be unlikely in the majority of cases. And if realloc() happens, the original allocation is doubled in size for each realloc() as suggested.

In testing (100000 iterations) this works very well with no leaking or discernible bloating.

I owe a great deal to the Stack Overflow community. Thank you all.

The code below won’t run stand-alone. It’s placed here to illustrate what I did.

..
after all the code that parses the commandline, forks, does execv, and cleans up…

while (waitpid(nPID, &status, 0) != nPID)
    ;
i = 0;
nFD = fdopen(nErrFD, "r");
if (!nFD) {
    snprintf(cErrMsg, 80, "Cannot open fd[%i].  Failed to spaw process",
            nErrFD);
    cbuf = strcpy(cbuf, cErrMsg);
    goto NECerror;
}

close(nErrFD);


cbuf = calloc(nBufSz, sizeof(char));
memset(cbuf, 0x00, nBufSz);
i = 0;
while ((ch = getc(nFD)) != EOF) {
    cbuf[i] = (char) ch;
    ++size;
    ++i;
    if (size > nBufSz) {
        nBufSz = nBufSz + nBaseBufSz;
        NewBuf = realloc(cbuf, nBufSz);
        if (NewBuf == NULL) {
            snprintf(cErrMsg, 80,
                    "Internal error:cannot allocate [%i] bytes", nBufSz);
            cbuf = strcpy(cbuf, cErrMsg);
            fclose(nFD);
            goto NECerror;
        }
        cbuf = NewBuf;
        free(NewBuf);
    }

}
fclose(nFD);
  • 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-29T10:31:05+00:00Added an answer on May 29, 2026 at 10:31 am

    Pipes are not seekable — if you’re reading data from a pipe that’s being provided by the other process, then you cannot seek its data stream. You have to store the data as its read.

    The reason you’re probably seeing lots of memory thrashing is due to the way your expanding your buffer — you only expand it by a constant amount every time it’s exceeded. You’ll have much better success if you double its size when it’s exceeded: this is a well-known technique for dynamic arrays.

    For example:

    // Error checking omitted for expository purposes
    size_t size = 4096;
    char *buf = malloc(size);
    char *bufPtr = buf;
    ssize_t n;
    while((n = read(MyStderr, bufPtr, 4096) > 0)
    {
        bufPtr += n;
    
        // Make sure we always have at least 4096 bytes of free space in the
        // buffer for the next read
        if(size - (bufPtr - buf) < 4096)
        {
            size *= 2;  // Watch out for overflow!
            buf = realloc(buf, size);
        }
    }
    
    // The (bufPtr - buf) bytes of buf now hold the entirety of the process's stderr
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
Does anyone know how can I replace this 2 symbol below from the string
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I am currently running into a problem where an element is coming back 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.