Thread: Bus Error Appending Char to String (char array)

  1. #1
    Registered User
    Join Date
    Jan 2016
    Posts
    45

    Bus Error Appending Char to String (char array)

    Hello,
    I am trying to append a space ' ', onto the end of a char array.
    In my main function I must pass a string to another function.
    eg) passingFunction("sentence...");
    Appending a space must not be done in the main function.
    However I am encountering a bus error problem.

    Here is code that generates the error:
    Code:
    #include <stdio.h>
    #include <string.h>
    
    
    void printString(char *);
    void passingFunction(char *);
    
    
    int main()
    {
    	passingFunction("I hate wonder bread.");
    }
    
    
    void passingFunction(char line[])
    {
    	int length = strlen(line);
    	line[length] = ' ';
    	line[length+1] = '\0';
    	printString(line);
    }
    
    
    void printString(char line[])
    {
    	int counter = 0;
    	char ch = line[counter];
    	while (ch != '\0')
    	{
    		printf("%c|", ch);
    		counter++;
    		ch = line[counter];
    	}
    	printf("\n");
    }
    I noticed that I do not encounter this problem if I did this:

    Code:
    #include <stdio.h>
    #include <string.h>
    
    
    void printString(char *);
    void passingFunction(char *);
    
    
    int main()
    {
            char line[] = "I hate wonder brea.");
    	passingFunction(line);
            printString(line)
    }
    
    
    void passingFunction(char line[])
    {
    	int length = strlen(line);
    	line[length] = ' ';
    	line[length+1] = '\0';
    	printString(line);
    }
    
    
    void printString(char line[])
    {
    	int counter = 0;
    	char ch = line[counter];
    	while (ch != '\0')
    	{
    		printf("%c|", ch);
    		counter++;
    		ch = line[counter];
    	}
    	printf("\n");
    }
    What is causing this bus error and what would be a possible solution without declaring the array like I did in the second method I have?

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    The first program passes a string literal to your function, and then tries to modify it, which is a no-no.

    The second program succeeds since you're passing a (modifiable) array.

    ... what would be a possible solution without declaring the array like I did in the second method I have?
    The array is actually the best possible choice in this case.

    You could allocate memory to a pointer instead, but you'd still be declaring a variable.
    You could copy the string to an allocated pointer within the function, and return the pointer, but then you'd have to free that pointer afterwards.

    Why do you want to avoid declaring an array?

  3. #3
    Registered User
    Join Date
    Jan 2016
    Posts
    45
    On second thought, maybe I can do that. I read the question wrong, which makes it easier
    We haven't learned about allocating memory or freeing a pointer in class.
    So I think my solution is actually the only thing I can do.
    Thank you!
    Also what is the reason why it is a no-no? Why can't I do that?
    Last edited by seal308; 02-02-2016 at 11:35 AM.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Your second solution is also wrong in that you need to declare an array that is large enough to store the final result.
    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

  5. #5
    Registered User
    Join Date
    Jan 2016
    Posts
    45
    That's odd, I ran it and it worked.
    Is the worry that this is dangerous or not the proper way?

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by seal308
    Also what is the reason why it is a no-no? Why can't I do that?
    In practice, string literals are not necessarily stored in a place in memory such that they can be modified. Hence, the C standard states that attempting to modify a string literal results in undefined behaviour. This means that it could appear to work, or it might result in something strange, or in an error such as the one that you observed.

    Quote Originally Posted by seal308
    That's odd, I ran it and it worked.
    Is the worry that this is dangerous or not the proper way?
    Accessing an array out of bounds, e.g., by writing beyond its bounds, also results in undefined behaviour. You can sometimes get away with writing just a little beyond the end of an array because the allocation of memory may have alignment requirements, or you might be overwriting memory for a variable that you do not access afterwards and hence it looks like nothing went wrong.
    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
    Jan 2016
    Posts
    45
    Thx for the explanation.
    What I did was this:
    char line[1000];
    strcpy(line, "I hate wonder bread.");

    Is this ok, or am I still trying to access outside the arrays bounds?

    Quote Originally Posted by laserlight View Post
    In practice, string literals are not necessarily stored in a place in memory such that they can be modified. Hence, the C standard states that attempting to modify a string literal results in undefined behaviour. This means that it could appear to work, or it might result in something strange, or in an error such as the one that you observed.


    Accessing an array out of bounds, e.g., by writing beyond its bounds, also results in undefined behaviour. You can sometimes get away with writing just a little beyond the end of an array because the allocation of memory may have alignment requirements, or you might be overwriting memory for a variable that you do not access afterwards and hence it looks like nothing went wrong.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    That is probably fine. It would have been better to declare:
    Code:
    void printString(const char *line);
    void appendSpaceAndPrint(char *line);
    The parameter of printString should be a const char* instead of just a char* because the function does not modify the string. passingFunction would be better named appendSpaceAndPrint. Notice that in both cases I include the parameter names in the forward declaration. If you were doing more than just appending a single space character, then it would be wise to have another parameter specify the maximum length of the string or the number of elements of the array.

    Actually, functions should do one thing and do it well. Hence, I would argue that passingFunction should be renamed appendSpace, and then it will not print anything. Rather, the main function would be modified:
    Code:
    int main(void)
    {
        char line[100] = "I hate wonder bread.";
        appendSpace(line);
        printString(line);
        return 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
    Jan 2016
    Posts
    45
    Understood.
    Honestly my problem is bigger than the question asked. I just quickly made a simplified program to show the problem I was having so I didn't pay much attention to names etc.
    However I didn't know I could use names in prototypes, thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 09-25-2014, 06:12 AM
  2. Convert string to char or use 2d array of char?
    By simpleblue in forum C++ Programming
    Replies: 6
    Last Post: 09-25-2011, 05:00 PM
  3. Appending char to C-Style string
    By Yinyang107 in forum C++ Programming
    Replies: 5
    Last Post: 02-07-2011, 05:22 PM
  4. Appending char to string (char array)
    By sniper83 in forum C Programming
    Replies: 14
    Last Post: 04-15-2008, 06:48 AM
  5. error converting string to char array
    By COBOL2C++ in forum C++ Programming
    Replies: 6
    Last Post: 07-11-2003, 10:59 AM

Tags for this Thread