Thread: pointer passing segmentation fault

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    134

    Question pointer passing segmentation fault

    Code:
    #include <stdio.h>
    #include <time.h>
    
    char *tm2str(struct tm *timeInfo)    {
            char *buf;
            sprintf (buf, "%d %d %d", timeInfo->tm_mday, timeInfo->tm_mon, (timeInfo->tm_year)+1900);
            return(buf);
    }
    
    int main ()
    {
      time_t rawtime;
      struct tm * timeinfo;
      char *out;
    
      time ( &rawtime );
      timeinfo = localtime ( &rawtime );
      out = tm2str(timeinfo);
      printf("\n%s\n", out);
      return 0;
    }
    why when it is run gives:
    Code:
    Segmentation fault (core dumped)

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well you didn't allocate any space for buf
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    May 2008
    Posts
    134

    how to do it?

    Thanks Salem for instant reply.
    How to do that without affecting my objective?

    If I change that declaration of buf i.e.

    Code:
    char *buf;
    to
    Code:
    char buf[20];
    am getting the warning:
    Code:
    test.c: In function ‘tm2str’:
    test.c:9: warning: function returns address of local variable
    What could be the reason for this
    Last edited by kapil1089thekin; 01-04-2011 at 11:57 AM.

  4. #4
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    Well, without knowing your objective, it is hard to say.

    Try using malloc. Here is an example from the wiki for malloc. Pay attention to the "useful idiom" example as well. Also, don't forget to free your allocated memory when you are done with it.


    Quote Originally Posted by kapil1089thekin View Post
    Thanks Salem for instant reply.
    How to do that without affecting my objective?

    If I change that declaration of buf i.e.

    Code:
    char *buf;
    to
    Code:
    char buf[20];
    am getting the warning:
    Code:
    test.c: In function ‘tm2str’:
    test.c:9: warning: function returns address of local variable
    What could be the reason for this
    You are allocating that space on the stack. When your function returns, the stack frame is gone, and so is your memory allocation. In other words, the data is trashed. Better to allocate some memory with malloc, and return the pointer to it.
    Last edited by kermit; 01-04-2011 at 11:59 AM.

  5. #5
    Registered User
    Join Date
    May 2008
    Posts
    134

    edited my previous post

    kindly look into it!

  6. #6
    Registered User
    Join Date
    May 2008
    Posts
    134
    which stack? Could you elaborate more? And when does something is stored in stack?

  7. #7
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    Quote Originally Posted by kapil1089thekin View Post
    which stack? Could you elaborate more? And when does something is stored in stack?
    Give this a read.

  8. #8
    Registered User
    Join Date
    May 2008
    Posts
    134
    I got that u are talking about stack frame?

    then why this doesnt happens in other cases like when it is dealing with integers?

  9. #9
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    Because with integers, as per your example question, you are passing back a value which can be used immediately, or stored, or ignored. With an array, you are passing back a value as well, but that value is an address. By the time you get that adress, i.e., after the function returns, it is useless because the stack frame (where the address existed) has already been popped off.
    Last edited by kermit; 01-04-2011 at 01:03 PM.

  10. #10
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    How about
    Code:
    void tm2str(struct tm *timeInfo, char *buf)
    Get rid of the return.
    And in your main you call where you declared buf[20]:
    Code:
    tm2str(timeinfo, buf);

  11. #11
    Registered User
    Join Date
    Jan 2011
    Posts
    3
    am getting the warning:
    Code:
    test.c: In function ‘tm2str’:
    test.c:9: warning: function returns address of local variable
    buf is a automatic variable for tm2str, when func is finished it is deallocated.

    instead of char *buf; put char *buf = malloc(20 * sizeof(char)); and in main after printf put free(out);.
    Last edited by littletics; 01-04-2011 at 02:22 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. Problems passing a file pointer to functions
    By smitchell in forum C Programming
    Replies: 4
    Last Post: 09-30-2008, 02:29 PM
  3. segmentation fault... first time with unix...
    By theMethod in forum C Programming
    Replies: 16
    Last Post: 09-30-2008, 02:01 AM
  4. Segmentation Fault?
    By magda3227 in forum C Programming
    Replies: 10
    Last Post: 07-10-2008, 07:26 AM
  5. Segmentation fault (pointer to an array)
    By Lang in forum C Programming
    Replies: 4
    Last Post: 04-08-2008, 12:22 AM

Tags for this Thread