Thread: Concatenation without string.h - program crashes

  1. #1
    Registered User
    Join Date
    Apr 2013
    Posts
    122

    Concatenation without string.h - program crashes

    Code:
    void my_strcat (char destination[], const char source[])
    {
        int i = 0, j = 0;
        while (destination[i] != '\0') i++;
        destination[i++] = ' ';
        while (source[j - 1] != '\0' || !j) destination[i++] = source[j++];
        puts(destination);
    }
    Why does the above code crash for destination = "abc" and source = "abcjjj"? I'd truly appreciate some advice.

  2. #2
    Registered User
    Join Date
    Jan 2013
    Posts
    55
    Well think about it, if you don't allocate enough space in destination for the source, it will crash. You can't fix a big piece of data in a small container, otherwise it will crash. Just like strcat, you shouldn't try to put bigger amounts of things into a smaller arrays.

  3. #3
    Registered User
    Join Date
    Apr 2013
    Posts
    122
    How may it be emended? How may I prevent it from crashing?

  4. #4
    Registered User
    Join Date
    Mar 2013
    Posts
    31
    The first time through, your program is going to try access source outside its bounds. j is initialised to 0, you call j-1 which will be before the start of your source string, you have no guarantees what will be in this value.

    Also, were you told to use that type of construct for a while loop? The way I read that, your first loop will loop iterate until it encounters a '\0' character then assign ' ' to the space after that one. Is there any reason to expect your destination array will contain any '\0' characters?

  5. #5
    Registered User
    Join Date
    Apr 2013
    Posts
    122
    Quote Originally Posted by AAnderson View Post
    The first time through, your program is going to try access source outside its bounds. j is initialised to 0, you call j-1 which will be before the start of your source string, you have no guarantees what will be in this value.

    Also, were you told to use that type of construct for a while loop? The way I read that, your first loop will loop iterate until it encounters a '\0' character then assign ' ' to the space after that one. Is there any reason to expect your destination array will contain any '\0' characters?
    We were told to presume all strings end with '\0'.
    How would you suggest I fix this?

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    This looks wrong:
    Code:
    while (source[j - 1] != '\0' || !j)
    I would expect you to iterate over the "source" string in the same manner as you first iterate over the "destination" string, except that you will be copying characters.

    Also, it would be good if you posted the code that tests this function to verify that the problem isn't elsewhere.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Apr 2013
    Posts
    122
    Code:
    int main()
    {
        char arr1[] = "abc";
        char arr2[] = "abcffffff";
        my_strcat(arr1, arr2);
        return 0;
    }
    
    
    
    
    void my_strcat (char destination[], const char source[])
    {
        int i = 0, j = 0;
        while (destination[i] != '\0') i++;
        destination[i++] = ' ';
        while (source[j - 1] != '\0' || !j) destination[i++] = source[j++];
        printf("%s\n", destination);
    }
    What do you think, laserlight?

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    It is still wrong: source[j - 1] accesses source out of bounds when j == 0.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Code:
    int main()
    {
        char arr1[] = "abc";
    This allocates memory for 4 characters. Later you are trying to add more characters but that doesn't work. That's like having a 4 litre bucket full of water and then trying to add more water to it. The bucket will overflow and the same happens with the array.
    You need an array which has enough room for all the characters in arr1 and all the characters in arr2 (and don't forget to count '\0').

    Bye, Andreas

  10. #10
    Registered User
    Join Date
    Apr 2013
    Posts
    122
    Quote Originally Posted by AndiPersti View Post
    Code:
    int main()
    {
        char arr1[] = "abc";
    This allocates memory for 4 characters. Later you are trying to add more characters but that doesn't work. That's like having a 4 litre bucket full of water and then trying to add more water to it. The bucket will overflow and the same happens with the array.
    You need an array which has enough room for all the characters in arr1 and all the characters in arr2 (and don't forget to count '\0').

    Bye, Andreas
    But in any case, due to the concatenation itself, destination is going to contain more characters than it was originally set up for, right? I mean, even if source is merely one char long it would still be added to it so that destination becomes longer. This is what puzzles me! Would you care to explain?

    And laserlight, it doesn't really matter whether I access j-1 or not because of the "||!j". Thus, the procedure ignores whatever is in that location for an out-of-bound j. I could easily "correct" this by adding a condition in parenthesis like ((source[j - 1] != '\0' && j != 0)|| !j)
    But which would still not save it from crashing!
    How may I fix the crashing?

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by peripatein
    But in any case, due to the concatenation itself, destination is going to contain more characters than it was originally set up for, right? I mean, even if source is merely one char long it would still be added to it so that destination becomes longer.
    You are mistaken: whatever you defined the array length to be, that is the array length. It does not change just because you attempt to access elements of the array that do not exist. Hence, the solution is to create an array large enough to hold the result, e.g.,
    Code:
    char arr1[13] = "abc";
    So, the length of the string is 3, but the number of elements of the array is 13. Since you will append 9 characters to the string, 3 + 9 + 1 (null character) = 13, hence this is okay.

    Quote Originally Posted by peripatein
    And laserlight, it doesn't really matter whether I access j-1 or not because of the "||!j". Thus, the procedure ignores whatever is in that location for an out-of-bound j.
    You are mistaken: the check for !j comes after you access source[j - 1]. Therefore, you access the array out of bounds.

    Quote Originally Posted by peripatein
    I could easily "correct" this by adding a condition in parenthesis like ((source[j - 1] != '\0' && j != 0)|| !j)
    That would make no difference since you still access source[j - 1] when j == 0. The thing is, why check for source[j - 1] instead of source[j]?

    Quote Originally Posted by peripatein
    How may I fix the crashing?
    Fix both the problems mentioned to you concerning array length and accessing the array out of bounds.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Registered User
    Join Date
    Apr 2013
    Posts
    122
    But, laserlight, how would I then make the procedure add the final '\0' of source to destination? I do not wish to add an extra line just for that one enclosing char but would like it to be done as part of the loop.

  13. #13
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by peripatein View Post
    I do not wish to add an extra line just for that one enclosing char but would like it to be done as part of the loop.
    Concentrate on writing a working function first.

    Bye, Andreas

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by peripatein
    how would I then make the procedure add the final '\0' of source to destination?
    When you are done with the copying, i would contain the index of the character that should be the null character.

    Quote Originally Posted by peripatein
    I do not wish to add an extra line just for that one enclosing char but would like it to be done as part of the loop.
    It can be done, but it requires some code dissection to understand:
    Code:
    while (destination[i++] = source[j++]);
    By the way, I realised that you append a space before appending the "source" string, so you need to account for this space as well.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #15
    Registered User
    Join Date
    Apr 2013
    Posts
    122
    Laserlight, what if I inverted the order of my condition, namely:
    Code:
    while ((j != 0 && source[j - 1] != '\0') || !j) destination[i++] = source[j++];
    Wouldn't that prevent the procedure from accessing a location out of bounds?

    And regardless, suppose I wrote the following:
    Code:
    int main()
    {
        char arr1[60] = "abc";
        char arr2[] = "abcffffff fldsflksfls";
        my_strcat(arr1, arr2);
        return 0;
    }
    void my_strcat (char destination[], const char source[])
    {
        int i = 0, j = 0;
        while (destination[i] != '\0') i++;
        destination[i++] = ' ';
        while (source[j] != '\0') destination[i++] = source[j++];
        printf("%s\n", destination);
    }
    Would you consider that to be a complete solution or is anything missing/unaccounted for?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. String Concatenation Using Pointers No strcat()
    By oviang in forum C Programming
    Replies: 4
    Last Post: 12-07-2007, 10:31 AM
  2. String concatenation
    By Mario F. in forum C++ Programming
    Replies: 5
    Last Post: 06-08-2006, 07:19 PM
  3. K&R problem !! String concatenation :(
    By karanmitra in forum C Programming
    Replies: 9
    Last Post: 08-18-2005, 05:20 AM
  4. string concatenation
    By criticalerror in forum C++ Programming
    Replies: 11
    Last Post: 03-02-2004, 11:37 AM
  5. integer concatenation with string again...
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 03-11-2002, 06:36 PM