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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T12:13:35+00:00 2026-05-11T12:13:35+00:00

So I have a couple of functions that work with a string type I

  • 0

So I have a couple of functions that work with a string type I have created. One of them creates a dynamically allocated sting. The other one takes said string, and extends it. And the last one frees the string. Note: The function names are changed, but all are custom-defined by me.

string new = make('Hello, '); adds(new, 'everyone'); free(new); 

The code above works – it compiles and runs fine. The code below does not work – it compiles, runs, and then

string new = make('Hello, '); adds(new, 'everyone!'); free(new); 

The difference between the code is that the adds() function is adding 1 more character (a !). The character it adds makes no difference – just the length. Just for completeness, the following code does not work:

string new = make('Hello, '); adds(new, 'everyone'); adds(new, '!'); free(new); 

Oddly, the following code, which uses a different function, addc() (which adds 1 character instead of a string) works:

string new = make('Hello, '); adds(new, 'everyone'); addc(new, '!'); free(new); 

The following, which also does the same thing, works:

string new = make('Hello, everyone!'); free(new); 

The error that all the ones that don’t work give is this:

test(526) malloc: *** error for object 0x100130: double free *** set a breakpoint in malloc_error_break to debug 

(test is the extremely descriptive name of the program I have this in.)

As far as the function internals, my make() is a call to strlen() and two calls to malloc() and a call to memcpy(), my adds() is a call to strlen(), a call to realloc(), and a call to memcpy(), and my free() is two calls to the standard library free().

So are there any ideas why I’m getting this, or do I need to break down and use a debugger? I’m only getting it with adds()es of over a certain length, and not with addc()s.

Breaking down and posting code for the functions:

typedef struct _str {   int _len;   char *_str; } *string;  string make(char *c) {     string s = malloc(sizeof(string));     if(s == NULL) return NULL;     s->_len = strlen(c);     s->_str = malloc(s->_len + 1);     if(s->_str == NULL)        {              free(s);         return NULL;       }     memcpy(s->_str, c, s->_len);     return s; }  int adds(string s, char *c) {     int l = strlen(c);     char *tmp;     if(l <= 0) return -1;     tmp = realloc(s->_str, s->_len + l + 1);     if(!tmp) return 0;     memcpy(s->_str + s->_len, c, l);     s->_len += l;     s->_str[s->_len] = 0;     return s->_len; }  void myfree(string s) {     if(s->_str) free(s->_str);     free(s);     s = NULL;     return; } 
  • 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. 2026-05-11T12:13:36+00:00Added an answer on May 11, 2026 at 12:13 pm

    A number of potential problems I would fix:

    1/ Your make() is dangerous since it’s not copying across the null-terminator for the string.

    2/ It also makes little sense to set s to NULL in myfree() since it’s a passed parameter and will have no effect on the actual parameter passed in.

    3/ I’m not sure why you return -1 from adds() if the added string length is 0 or less. First, it can’t be negative. Second, it seems quite plausible that you could add an empty string, which should result in not changing the string and returning the current string length. I would only return a length of -1 if it failed (i.e. realloc() didn’t work) and make sure the old string is preserved if that happens.

    4/ You’re not storing the tmp variable into s->_str even though it can change – it rarely re-allocates memory in-place if you’re increasing the size although it is possible if the increase is small enough to fit within any extra space allocated by malloc(). Reduction of size would almost certainly re-allocate in-place unless your implementation of malloc() uses different buffer pools for different-sized memory blocks. But that’s just an aside, since you’re not ever reducing the memory usage with this code.

    5/ I think your specific problem here is that you’re only allocating space for string which is a pointer to the structure, not the structure itself. This means when you put the string in, you’re corrupting the memory arena.

    This is the code I would have written (including more descriptive variable names, but that’s just my preference).

    I’ve changed:

    • the return values from adds() to better reflect the length and error conditions. Now it only returns -1 if it couldn’t expand (and the original string is untouched) – any other return value is the new string length.
    • the return from myfree() if you want to really do want to set the string to NULL with something like ‘s = myfree (s)‘.
    • the checks in myfree() for NULL string since you can now never have an allocated string without an allocated string->strChars.

    Here it is, use (or don’t 🙂 as you see fit:

    /*================================*/ /* Structure for storing strings. */  typedef struct _string {     int  strLen;     /* Length of string */     char *strChars;  /* Pointer to null-terminated chars */ } *string;  /*=========================================*/ /* Make a string, based on a char pointer. */  string make (char *srcChars) {     /* Get the structure memory. */      string newStr = malloc (sizeof (struct _string));     if (newStr == NULL)         return NULL;      /* Get the character array memory based on length, free the        structure if this cannot be done. */      newStr->strLen = strlen (srcChars);     newStr->strChars = malloc (newStr->strLen + 1);     if(newStr->strChars == NULL) {              free(newStr);         return NULL;     }      /* Copy in string and return the address. */      strcpy (newStr->strChars, srcChars);     return newStr; }  /*======================================================*/ /* Add a char pointer to the end of an existing string. */  int adds (string curStr, char *addChars) {     char *tmpChars;      /* If adding nothing, leave it alone and return current length. */      int addLen = strlen (addChars);     if (addLen == 0)         return curStr->strLen;      /* Allocate space for new string, return error if cannot be done,        but leave current string alone in that case. */      tmpChars = malloc (curStr->strLen + addLen + 1);     if (tmpChars == NULL)         return -1;      /* Copy in old string, append new string. */      strcpy (tmpChars, curStr->strChars);     strcat (tmpChars, addChars);      /* Free old string, use new string, adjust length. */      free (curStr->strChars);     curStr->strLen = strlen (tmpChars);     curStr->strChars = tmpChars;      /* Return new length. */      return curStr->strLen; }  /*================*/ /* Free a string. */  string myfree (string curStr) {     /* Don't mess up if string is already NULL. */      if (curStr != NULL) {         /* Free chars and the string structure. */          free (curStr->strChars);         free (curStr);     }      /* Return NULL so user can store that in string, such as        <s = myfree (s);> */      return NULL; } 

    The only other possible improvement I could see would be to maintain a buffer of space and the end of the strChars to allow a level of expansion without calling malloc().

    That would require both a buffer length and a string length and changing the code to only allocate more space if the combined string length and new chars length is greater than the buffer length.

    This would all be encapsulated in the function so the API wouldn’t change at all. And, if you ever get around to providing functions to reduce the size of a string, they wouldn’t have to re-allocate memory either, they’d just reduce their usage of the buffer. You’d probably need a compress() function in that case to reduce strings that have a large buffer and small string.

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

Sidebar

Ask A Question

Stats

  • Questions 123k
  • Answers 123k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer If you're using the URL Loading System (NSURLRequest, NSURLConnection, etc.),… May 12, 2026 at 12:58 am
  • Editorial Team
    Editorial Team added an answer Create a script to launch Tomcat. In the launch script,… May 12, 2026 at 12:58 am
  • Editorial Team
    Editorial Team added an answer Here's an example of getting the stack via the traceback… May 12, 2026 at 12:58 am

Related Questions

I was thinking earlier today about an idea for a small game and stumbled
I've got a couple of really old MSDos based paint programs. They work on
I edited the question so it would make more sense. I have a function
What's a better way to start a thread, _beginthread , _beginthreadx or CreateThread ?

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.