Thread: Returning strings help

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    17

    Returning strings help

    the code below compiles but when I try to run it, it crashes and says "segmentation fault", please help.

    insert
    Code:
    #include <string.h>
    #include <time.h>
    #include <stdio.h>
    
    
    char* get_time();
    
    main () {
    
    	char *c;
    	
    	c = get_time();
    	printf("%s\n", c);
    
           return 0;
    }
    
    char* get_time() {
    
      char *buffer;
      time_t curtime;
      struct tm *loctime;
     
      curtime = time (NULL);
    
      loctime = localtime (&curtime);
     
      strftime (buffer, 30, " %d/%m/%Y %I:%M:%S\n", loctime);
    	printf("%s\n", buffer);
    return buffer;
    }

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You're returning and then trying to print out, the contents of a variable that has gone out of scope (died).

    Golden Rule: Don't return the address of Zombies!

    (because C points to buffer, and buffer is dead)

  3. #3
    Registered User
    Join Date
    Apr 2010
    Posts
    17
    Ok, so how can I get round this problem? Im obviously trying to display the current time.
    Cheers

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Also: buffer does not have any memory allocated to it. As in, 0 bytes. But you have told strftime() to print upto 30 bytes.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    Registered User
    Join Date
    Apr 2010
    Posts
    17
    Ok, Im really new to this, so can you give me some tips on how to solve my problem?
    Cheers

  6. #6
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    555
    You never allocated any space in buffer to hold the data strftime puts into it. EDIT: As stated by MK27, he beat me to it grrr...

    IE you need to allocate some space in buffer first, then returning buffer will not be an issue, other then the fact that the caller needs to free() it at some point.

  7. #7
    Registered User
    Join Date
    Apr 2010
    Posts
    17
    So should I define buffer as char[30] ?

  8. #8
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by gar35 View Post
    So should I define buffer as char[30] ?
    You could, then you will be back to the problem Adak mentioned -- you cannot return the address of a local stack variable effectively. So you would want to malloc this on the heap:

    Code:
    #include <stdlib.h>
    
    char *example (int bytes) {
    	char *ptr = malloc(bytes);
    	return ptr;
    }
    
    int main(int argc, const char *argv[]) {
    	char *x = example(30);
            free(x);
    	return 0;
    }
    You should google "C heap vs. stack memory" and do a little reading about this issue.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  9. #9
    Registered User
    Join Date
    Apr 2010
    Posts
    17
    Sorry guys but Im still not getting any success, Ive tried declaring buffer as a char[30] but main() just prints out garbage now.

  10. #10
    Registered User
    Join Date
    Apr 2010
    Posts
    17
    Ok Ive tried using malloc() but I now get an error saying "invalid conversion from void* to char* "

  11. #11
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    555
    You didn't include stdlib.h in your program (prototypes free() and malloc()).

  12. #12
    Registered User
    Join Date
    Apr 2010
    Posts
    17
    No I have included that file

  13. #13
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    555
    Then repost your new code.

  14. #14
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by gar35 View Post
    Ok Ive tried using malloc() but I now get an error saying "invalid conversion from void* to char* "
    You are compiling as C++, not C. So chance are you are using MS Visual Studio?
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  15. #15
    Registered User
    Join Date
    Apr 2010
    Posts
    17
    insert
    Code:
    #include <string.h>
    #include <time.h>
    #include <stdio.h>
    #include <stdlib.h>
    
    
    char* get_time();
    
    main (int argc, const char *arcv[]) {
    
    	char *c;
    	
    	c = get_time();
    	printf("%s\n", c);
            free(c);
           return 0;
    }
    
    
    char *get_time() {
    
      char *buffer = malloc(30);
      time_t curtime;
      struct tm *loctime;
     
      curtime = time (NULL);
    
      loctime = localtime (&curtime);
     
      strftime (buffer, 30, " %d/%m/%Y %I:%M:%S\n", loctime);
    
    return buffer;
    }
    Im using the text editor gedit

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Does returning newly-declared strings cause leaks?
    By Anvilsmith in forum C++ Programming
    Replies: 8
    Last Post: 07-29-2008, 11:01 AM
  2. Returning strings?
    By DBProgrammer in forum C++ Programming
    Replies: 4
    Last Post: 09-26-2007, 07:15 PM
  3. Returning Strings
    By Overload in forum C++ Programming
    Replies: 14
    Last Post: 07-29-2004, 07:05 PM
  4. Please help with String class, returning strings
    By skanxalot in forum C++ Programming
    Replies: 4
    Last Post: 09-24-2003, 10:48 AM
  5. correct way of returning strings
    By marsface in forum C++ Programming
    Replies: 5
    Last Post: 06-11-2003, 10:33 AM