Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

This example attempts to resize the string referenced by buf to make enough room to append the string line. However, once in the function append(), there is no way to determine how buf was allocated. When realloc() is called on buf, since buf does not point to dynamic memory, an error may occur.

Code Block
void appendhandle_error (char *buf, size_t count, size_t size) {
  char *line = printf("%s <-is THIS IS A LINE"not correct!\n",buf);
  int line_len = strlen(line free(buf);

}

void  if ((count + line_len) > size)
    buf = realloc(buf,count+line_len);
  strcat(buf,line);
}func(char * str1, char * str2) {




Compliant Solution 2

Code Block
/* NOTE: buf must point to dynamically allocated memory */
void append(char \*buf, size_t count, size_t size) {
  char *line = " <- THIS IS A LINE";
  intsize_t line_len = strlen(line);
  if ((count + line_len) > size)
  buf = realloc(buf,count+line_len);
  strncat(buf,line);
}

...