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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T02:54:03+00:00 2026-06-14T02:54:03+00:00

For reasons that I promise exist, I’m reading input character by character, and if

  • 0

For reasons that I promise exist, I’m reading input character by character, and if a character meets certain criteria, I’m writing it into a dynamically allocated buffer. This function adds the specified character to the “end” of the specified string. When reading out of the buffer, I read the first ‘size’ characters.

void append(char c, char *str, int size)
{
 if(size + 1 > strlen(str))
        str = (char*)realloc(str,sizeof(char)*(size + 1));
 str[size] = c;
}

This function, through various iterations of development has produced such errors as “corrupted double-linked list”, “double free or corruption”. Below is a sample of how append is supposed to be used:

// buffer is a string
// bufSize is the number of non-garbage characters at the beginning of buffer
char *buft = buffer;
int bufLoc=0;
while((buft-buffer)/sizeof(char) < bufSize)
    append(*(buft==),destination,bufLoc++);

It generally works for some seemingly arbitrary number of characters, and then aborts with error. If it’s not clear what the second code snippet is doing, it’s just copying from the buffer into some destination string. I know there’s library methods for this, but I need a bit finer control of what exactly gets copied sometimes.

Thanks in advance for any insight. I’m stumped.

  • 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-14T02:54:04+00:00Added an answer on June 14, 2026 at 2:54 am

    This function does not append a character to a buffer.

    void append(char c, char *str, int size)
    {
        if(size + 1 > strlen(str))
            str = realloc(str, size + 1);
        str[size] = c;
    }
    

    First, what is strlen(str)? You can say “it’s the length of str“, but that’s omitting some very important details. How does it compute the length? Easy — str must be NUL-terminated, and strlen finds the offset of the first NUL byte in it. If your buffer doesn’t have a NUL byte at the end, then you can’t use strlen to find its length.

    Typically, you will want to keep track of the buffer’s length. In order to reduce the number of reallocations, keep track of the buffer size and the amount of data in it separately.

    struct buf {
        char *buf;
        size_t buflen;
        size_t bufalloc;
    };
    
    void buf_init(struct buf *b)
    {
        buf->buf = NULL;
        buf->buflen = 0;
        buf->bufalloc = 0;
    }
    
    void buf_append(struct buf *b, int c)
    {
        if (buf->buflen >= buf->bufalloc) {
            size_t newalloc = buf->bufalloc ? buf->bufalloc * 2 : 16;
            char *newbuf = realloc(buf->buf, newalloc);
            if (!newbuf)
                abort();
            buf->buf = newbuf;
            buf->bufalloc = newalloc;
        }
        buf->buf[buf->buflen++] = c;
    }
    

    Another problem

    This code:

    str = realloc(str, size + 1);
    

    It only changes the value of str in append — it doesn’t change the value of str in the calling function. Function arguments are local to the function, and changing them doesn’t affect anything outside of the function.

    Minor quibbles

    This is a bit strange:

    // Weird
    x = (char*)realloc(str,sizeof(char)*(size + 1));
    

    The (char *) cast is not only unnecessary, but it can actually mask an error — if you forget to include <stdlib.h>, the cast will allow the code to compile anyway. Bummer.

    And sizeof(char) is 1, by definition. So don’t bother.

    // Fixed
    x = realloc(str, size + 1);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For various reasons that I won't go into (I promise it's necessary to do
I'm writing a program that for performance reasons uses shared memory (sockets and pipes
For reasons that I won't get into here, I will be doing some new
For reasons that I'd rather not go into, I have a bunch of parallel
One of the reasons that I tend to dread writing Javascript for anything other
For reasons that are irrelevant to this question I'll need to run several SQLite
For reasons that only the developers can understand, Firefox will create and open .url
For reasons that I would not like to discuss, our master database schema is
I would like to know the reasons that we do refactoring and justify it.
(Let's assume that I have good reasons not to remove my NSAssert() checks in

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.