0
votes
int length = strlen(input_str);
output_s = malloc((sizeof(char*) * totalSentences) + 1);
for (int i = 0; i < totalSentences; i++) {
  output_s[i] = malloc((sizeof(char) * sentences[i]) + 1);
}

free(sentences);

currentSentence = 0;
int currentCharacter = 0;
int firstChar = 1;
for (int i = 0; i < length; i++) {

  if (isalpha(*input_str) && (firstChar == 1)) {
    output_s[currentSentence][currentCharacter] = (char)toupper(*input_str);
    currentCharacter++;
    firstChar = 0;
  } else if (isalpha(*input_str)) {
    output_s[currentSentence][currentCharacter] = (char)tolower(*input_str);
    currentCharacter++;
  } else if (!isspace(*input_str) && !ispunct(*input_str)) {
    output_s[currentSentence][currentCharacter] = *input_str;
    currentCharacter++;
  }

  if (isspace(*input_str)) {
    firstChar = 1;
  }

  if (ispunct(*input_str)) {
    firstChar = 1;
    currentCharacter = 0;
    currentSentence++;
    input_str++;
    if (currentSentence == totalSentences) {
      break;
    }
    continue;
  }

  input_str++;
}

output_s[totalSentences] = NULL;

So I am creating an array of strings in c, and using a for loop and printf I know the strings are being created valid and it prints (null) at the end so I am assuming I am correctly setting the NULL byte at the end with output_s[totalSentences] = NULL; however when running this through valgrind it shows

==18908== Invalid write of size 8
==18908==    at 0x400BB1: camel_caser (camelCaser.c:99)
==18908==    by 0x400D45: test_camelCaser (camelCaser_tests.c:34)
==18908==    by 0x400E17: main (camelCaser_main.c:13)
==18908==  Address 0x5204538 is 24 bytes inside a block of size 25 alloc'd
==18908==    at 0x4C2DB8F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==18908==    by 0x40090C: camel_caser (camelCaser.c:57)
==18908==    by 0x400D45: test_camelCaser (camelCaser_tests.c:34)
==18908==    by 0x400E17: main (camelCaser_main.c:13)

with line 57 being the line where I malloc the malloc(sizeof(char*) * totalSentences + 1) and line 99 being the line where I actually set the last byte in the array to be NULL, so I don't know if this is somehow messing up the memory earlier in the array that has caused the null to be written in a bad spot or am I not allocating enough memory to fit NULL in?

1
The multiplication and addition in the first malloc() is probably wrongly parenthesized. The odd bytes is of no use in an array of pointers. You probably want ‘(length + 1) * sizeof(char *)`.Jonathan Leffler
What is sentences[i]?alk
After determining the total number of sentences I made an array sentences that kept track of number of characters per sentence so that I could make a properly sized array to contain themuser3645925

1 Answers

0
votes

You definitely need that to be output_s = malloc(sizeof(char*) * (totalSentences + 1)); instead of output_s = malloc((sizeof(char*) * totalSentences) + 1);.

The code - as you have it - allocates space for totalSentences char *s, plus 1 byte. Your code wants space for totalSentences + 1 char *s. That's why valgrind gives the error that you're writing 8 bytes to a space 24 bytes into 25 bytes allocated.