Thread: Uncertain why program hangs; Simple string copy with pointers

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    178

    Uncertain why program hangs; Simple string copy with pointers

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <stdbool.h>	/* Include the stand boolean header */
    #include <math.h>
    #include "share.h"        /* Custom header of no affect to problem */
    
    void cpy(char *original, char *copy)
    {
    	while (*original != '\0')
    		*copy++ = *original++;
    }
    
    int
    main(VOID)
    {
    	char phrase[] = "Blessed be the Lord!";
    
    	char *copy = "";
    
    	cpy(phrase, copy);
    	printf ("%s", copy);
    	return 0;
    }
    I have not debugged this programing using gdb though I have ascertained the problem lay with the pointer assignment in the function.
    Last edited by Imanuel; 08-04-2011 at 08:57 PM. Reason: Including headers

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Hints: modifying a string literal results in undefined behaviour; watch out for your null character.

    By the way, unless you have special reason to use a macro, just use void instead of VOID. Showing the headers included would also be a good idea.
    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

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    178
    I did edit my post to include headers. I am not sure what you mean by modifying a string literal resulting in undefined behavior. I am using the second edition of the "C Programming Language" to learn C. All I am doing is passing a pointer into the functions and copying each character to another pointer.

  4. #4
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    Another hint- make sure you've allocated enough memory in "copy" to store what you want to put there.
    Code:
    while(!asleep) {
       sheep++;
    }

  5. #5
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by Imanuel View Post
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <stdbool.h>	/* Include the stand boolean header */
    #include <math.h>
    #include "share.h"        /* Custom header of no affect to problem */
    
    void cpy(char *original, char *copy)
    {
    	while (*original != '\0')
    		*copy++ = *original++;
    }
    
    int
    main(VOID)
    {
    	char phrase[] = "Blessed be the Lord!";
    
    	char *copy = "";
    
    	cpy(phrase, copy);
    	printf ("%s", copy);
    	return 0;
    }
    I have not debugged this programing using gdb though I have ascertained the problem lay with the pointer assignment in the function.
    1.VOID is not a default C keyword, void is. There is a difference.
    2. char* copy = ""; makes a string literal, just like char phrase[]=".." does. In order to do what you want you need to declare a char* and then get memory for it with malloc. Note: You could just define a char[] with some default size in it.
    3. Your while loop as written does not place the '\0' char at the end of your copied string (assuming that would work). Note: C style strings need to be terminated with '\0'. Look carefully at your loop logic.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    178
    Quote Originally Posted by TheBigH View Post
    Another hint- make sure you've allocated enough memory in "copy" to store what you want to put there.
    I used char *copy = malloc(100) and it worked fine. I never though I actually needed to preallocate the memory for the pointer. Thank you.

  7. #7
    Registered User
    Join Date
    May 2010
    Posts
    178
    Quote Originally Posted by AndrewHunter View Post
    1.VOID is not a default C keyword, void is. There is a difference.
    2. char* copy = ""; makes a string literal, just like char phrase[]=".." does. In order to do what you want you need to declare a char* and then get memory for it with malloc. Note: You could just define a char[] with some default size in it.
    3. Your while loop as written does not place the '\0' char at the end of your copied string (assuming that would work). Note: C style strings need to be terminated with '\0'. Look carefully at your loop logic.
    In closing, I did everything you suggested and the previous replyer did. All is well in Kansas as I continue to learn C.

  8. #8
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    Quote Originally Posted by Imanuel View Post
    I used char *copy = malloc(100) and it worked fine. I never though I actually needed to preallocate the memory for the pointer. Thank you.
    *copy = malloc(100) is fine and correct, since a char always has size 1, but in the interests of clarity you might consider *copy = malloc( 100 * sizeof(char) ) since this will make it clear to anyone reading your code what kind of thing is supposed to go into copy. Writing code that is self-commenting as possible is good programming practice.
    Code:
    while(!asleep) {
       sheep++;
    }

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by TheBigH
    *copy = malloc(100) is fine and correct, since a char always has size 1, but in the interests of clarity you might consider *copy = malloc( 100 * sizeof(char) ) since this will make it clear to anyone reading your code what kind of thing is supposed to go into copy.
    That is quite clear since the type name is already on the same line. If you really do want to have the sizeof, then I suggest:
    Code:
    char *copy = malloc(100 * sizeof(*copy));
    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

  10. #10
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    You're right, I overlooked the type name. Disregard.
    Code:
    while(!asleep) {
       sheep++;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple String Copy Question
    By Canadian0469 in forum C++ Programming
    Replies: 11
    Last Post: 09-18-2008, 02:58 AM
  2. Program hangs
    By abh!shek in forum C Programming
    Replies: 4
    Last Post: 05-25-2008, 08:02 PM
  3. Prog Hangs - Something To Do With Pointers
    By mike_g in forum C++ Programming
    Replies: 8
    Last Post: 09-19-2007, 09:34 AM
  4. I'm back! need help with a simple copy program
    By Rune Hunter in forum C++ Programming
    Replies: 11
    Last Post: 10-16-2004, 07:52 PM
  5. Simple copy program hangs?
    By curlious in forum Linux Programming
    Replies: 3
    Last Post: 07-24-2004, 05:00 PM