0

I have a function append_string, which appends str2 to str1:

void append_string(char* str1, char* str2) {
    int    new_length = strlen(str1)+strlen(str2);
    size_t new_size = sizeof(char)*(new_length);
    str1 = (char*) realloc(str1, new_size);
    strcat(str1, str2);
}

As shown in the function, I'm trying to increase the size using a the combined size of the strings.

Whenever I call append_str("", "adc");, I get "realloc(): Invalid pointer"

What did I do wrong?

0

1 Answer 1

4

At least these problems:

Attempting to reallocate something not allocated.

realloc(str1, new_size) attempts to reallocate the string literal "" leading to "realloc(): Invalid pointer".

Size off by 1

New size did not account for the null character.

// size_t new_size = sizeof(char)*(new_length);
size_t new_size = sizeof(char)*(new_length + 1);

Code loses the allocated pointer

Calling code lost the value of the new pointer str.

Weak type for sizing

Use size_t.


Instead, pass in an allocated pointer or NULL by its address.

void append_string(char** str, const char* appendage) {
  size_t new_length = strlen(*str) + strlen(appendage);
  size_t new_size = sizeof(char)*(new_length + 1);
  *str = realloc(*str, new_size);
  strcat(*str, appendage);
}

// Usage
char *s = malloc(1);
strcpy(s, "");
append_str(&s, "adc");
puts(s);

Advanced issues include:

  • What to do if realloc() returns NULL?

  • How to handle appendage overlapping str?

  • Do not use strcat(). Avoid slow code. Better to retain the original string length and copy from there.

      void append_string(char** str, const char* appendage) {
        size_t str_len = *str ? strlen(*str) : 0;
        size_t app_len = strlen(appendage);
        void *p = realloc(*str, str_len + app_len + 1);
        if (p == NULL) {    
          // Handle Error - various approaches
          free(*str);
          *str = NULL;
        } else {        
          strcpy(*str + str_len, appendage);
        }
      }
    

Still need to handle case when appendage overlaps *str.

    void append_string_when_overlap_possible(char** str, const char* appendage) {
      size_t str_len = *str ? strlen(*str) : 0;
      size_t app_len = strlen(appendage);
      char *p = malloc(str_len + app_len + 1);
      if (p == NULL) {    
        // Handle Error - various approaches
        free(*str);
        *str = NULL;
      } else {        
        if (*str) {
          strcpy(p, *str);
        }
        strcpy(p + str_len, appendage);
        free(*str);
        *str = p;
      }
    }
Sign up to request clarification or add additional context in comments.

2 Comments

Wow! This is so descriptive and easy to understand! Thank you! Although, I don't understand what you meant in the section "Code loses the allocated pointer". Could you please elaborate a bit more on that?
@TahminAhmed After str1 = (char*) realloc(str1, new_size);, str is used in the next line strcat(str1, str2); and then that is it. The allocated data's pointer is then lost after the function ends - memory still allocated, but no place in user code saved the pointer value.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.