'Segmentation fault with strcat and malloc in C

I'm still pretty new to programming, but I'm trying to write a function that duplicates letters in a given string. It takes a series of integer inputs for how many of each letter to include, then concatenates each letter to the end of the stretched string. I need to dynamically allocate the memory.

This is what I have:

char * stretch (char * aString) {

   int length;
   int i, k, checking;
   int amount[10];
   int aTotal = 0;
   char *stretched;
   char ch;

   length = strlen(aString);

   for (i = 0; i < length; i++) {

      printf("Enter an amount: ");
      scanf("%d", &checking);

      while (checking <= 0) {

         printf("\nPositive integers only\n");

         printf("Enter an amount: ");
         scanf("%d", &checking);
      }
      amount[i] = checking;
   }

   for (i = 0; i < length; i++) {
      aTotal = aTotal + amount[i];
   }

   stretched = malloc(sizeof(char) * total + 1);
   stretched[aTotal] = '\0';

   for (i = 0; i < length; i++) {

      ch = aString[i];
      for (k = 0; k < amount[i]; k++) {
         strncat(stretched, &ch, 1);
      }
      printf("stretched: %s", stretched);
   }
   return stretched;
}

I end up with a segmentation fault, but 'stretching' the string does work, the print statements show that stretched contains the right data (ie. if the string is "Hi!" and the input is 2 1 3, then at the end of the loop stretched contains "HHi!!!"). However I can't print stretched outside of the for loop, but I can print just a basic string to show it leaves the loop.

Running gdb returned

Program received signal SIGSEGV, Segmentation fault.
__strcpy_sse2_unaligned () at ../sysdeps/x86_64/multiarch/strcpy-sse2-unaligned.S:716
716 ../sysdeps/x86_64/multiarch/strcpy-sse2-unaligned.S: No such file or directory.

I'll be honest I have no idea what it means.

I've tried everything I could think of and most of the solutions I could understand online. I've initialized stretched a bunch of ways, and used malloc, calloc, realloc, strcpy, strcat.

Any ideas for what's causing the segmentation fault, and how to fix it? Thank you!! :)

EDIT: I ended up fixing it, I used memset after the malloc and then the next problem was actually in my main(), I was trying to strcpy the returned value into a char*. Thanks for helping!



Solution 1:[1]

In order to use strcat family of functions, you need to have a null terminated string. Your stretched array is filled with garbage and then a null terminator at the end of this garbage array, which could contain anything (including the value 0 elsewhere).

Instead of stretched[total] = '\0'; you need to do stretched[0] = '\0'; .

Also you must obviously ensure that this array is large enough to contain all data, including a null terminator. (Each strcat call will add one at the end of the new string.)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Lundin