0

I tried to run a C program that reads from stdin and stores them in char**, but I keep getting either malloc: Incorrect checksum for freed object or segmentation fault errors whenever realloc is used to increase the size of the pointer. Can anyone point out what I did wrong? The program is example 7_14 from beginning C 5th ed. Below is the code I ran without using C11 optional string functions because my clang complier doesn't seem to support them.

// Program 7.14 Using array notation with pointers to sort strings
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>

#define BUF_LEN 100
#define COUNT 5

int main(void)
{
  char buf[BUF_LEN];                            // Input buffer
  size_t str_count = 0;                         // Current string count
  size_t capacity = COUNT;                      // Current maximum number of strings
  char **pS = calloc(capacity, sizeof(char *)); // Pointers to strings
  char **psTemp = NULL;                         // Temporary pointer to pointer to char
  char *pTemp = NULL;                           // Temporary pointer to char
  size_t str_len = 0;                           // Length of a string
  bool sorted = false;                          // Indicated when strings are sorted

  printf("Enter strings to be sorted, one per line. Press Enter to end:\n");

  // Read in all the strings
  char *ptr = NULL;
  while (*fgets(buf, BUF_LEN, stdin) != '\n')
  {
    if(str_count == capacity)
    {
      capacity += capacity/4;
      if (!(psTemp = realloc(pS, capacity))) return 1;
      pS = psTemp;
    }
    str_len = strnlen(buf, BUF_LEN) + 1;
    if (!(pS[str_count] = malloc(str_len))) return 2;
    strcpy(pS[str_count++], buf);
  }

  // Sort the strings in ascending order
  while(!sorted)
  {
    sorted = true;
    for(size_t i = 0; i < str_count - 1; ++i)
    {
      if(strcmp(pS[i], pS[i + 1]) > 0)
      {
        sorted = false;
        pTemp = pS[i];
        pS[i] = pS[i+1];
        pS[i+1] = pTemp;
      }
    }
  }

  // Output the sorted strings
  printf("Your input sorted in ascending sequence is:\n\n");
  for(size_t i = 0; i < str_count; ++i)
  {
    printf("%s", pS[i]);
    free(pS[i]);
    pS[i] = NULL;
  }
  free(pS);
  pS = NULL;
  return 0;
}

Example execution result:

Enter strings to be sorted, one per line. Press Enter to end:
Many a mickle makes a muckle.
A fool and your money are soon partners.
Every dog has his day.
Do unto others before they do it to you.
A nod is as good as a wink to a blind horse.
The bigger they are, the harder they hit.

[1]    82729 segmentation fault  ./7_14
trainsushi
  • 133
  • 1
  • 3
  • 3
    Does this answer your question? [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) – Marco Bonelli Oct 22 '20 at 03:24
  • 1
    May not be the cause of the issue you are asking about but this is very dangerous code: `*fgets(buf, BUF_LEN, stdin)`. Because `fgets` can return NULL so its return value should not be unconditionally dereferenced. – kaylum Oct 22 '20 at 03:26
  • 3
    `realloc(pS, capacity))` needs to be `realloc(pS, capacity * sizeof(char *)))` – kaylum Oct 22 '20 at 03:28
  • Changing realloc fixed the issue. I'll try using debugger from now on. Thank you! – trainsushi Oct 22 '20 at 03:38

0 Answers0