Find the answer to your Linux question:
Results 1 to 4 of 4
Hello to all, I am using localtime() funtion to get information of of time. But the program is having segmentation fault. It gives the following type of error from debugger. ...
  1. #1
    Just Joined!
    Join Date
    Nov 2006
    Posts
    13

    Thumbs up localtime() function is having problem

    Hello to all,

    I am using localtime() funtion to get information of of time.

    But the program is having segmentation fault.

    It gives the following type of error from debugger.

    /************************************************** ****************/

    Program received signal SIGSEGV, Segmentation fault.
    0x00a46100 in getenv () from /lib/tls/libc.so.6

    #0 0x00a46100 in getenv () from /lib/tls/libc.so.6
    #1 0x00aafbcf in tzset_internal () from /lib/tls/libc.so.6
    #2 0x00ab07a8 in __tz_convert () from /lib/tls/libc.so.6

    /************************************************** ****************/

    My Program for the reference point is as per Below.

    Same problem is there is I am using either ctime() or gmtime().

    So what should I have to use instead of these function to get time.

    Or What precautions I have to take.

    Please help me if possible.

    Any good reply is apprecialble.

    Thanking You in advance.

    /************************************************** ********************/

    #include <stdio.h>
    #include <string.h>
    #include <utmp.h>
    #include <paths.h>
    #include <time.h>

    struct login
    {
    char username[20];
    char logintime[30];
    char logouttime[30];
    }user[100];

    int main(int argc, char *argv[])
    {
    int i = 0;
    char cTime[64] = { '\0' };
    struct tm *Time;
    struct utmp *entry;
    utmpname(_PATH_WTMP);
    setutent();

    while(entry = getutent())
    {
    if( strcmp(entry->ut_line,":0") == 0)
    {
    Time = localtime(&(entry->ut_time));// with localtime the program is having problem as mentioned above
    strftime(cTime,sizeof cTime,"%F %T",Time);
    if(strcmp(entry->ut_user,"\0") != 0)
    {
    strcpy(user[i].username, "");
    strcpy(user[i].logintime, "");
    strcpy(user[i].username, entry->ut_user);
    strcpy(user[i].logintime, cTime);
    }
    else
    {
    strcpy(user[i].logouttime, "");
    strcpy(user[i].logouttime, cTime);
    i++;
    }
    }
    }
    int p;
    for(p = 0 ; p < i ; p++)
    {
    printf(" %s\t\t %s\t\t %s\n", user[p].username, user[p].logintime, user[p].logouttime);
    }
    endutent();
    return 0;
    }

    /************************************************** ************************/

  2. #2
    Linux Enthusiast likwid's Avatar
    Join Date
    Dec 2006
    Location
    MA
    Posts
    649
    Looks like you haven't allocated any memory for the Time struct.

    you need to do something like

    Code:
    Time = (tm *)malloc(sizeof(tm));
    
    if (Time == NULL) {
       fprintf(stderr, "Error allocating memory for Time, Exiting.");
       exit(-1);
    }
    Then assign values to members of time

    --EDIT--

    Actually I guess Entry is the struct you need memory for and Time is actually just going to be a pointer. IDK what exactly you're doing. You might want to compile it with -ggdb and set a breakpoint for the first line and just type 'step' to go through each line of code until it segfaults. That'll give you a more clear indication of what's happening.

  3. #3
    Just Joined!
    Join Date
    Jan 2011
    Posts
    1

    almost same problem

    I am having the same problem as you, and i tryed the suggestion, but i couldŕ solve it.

    what i get debugging is:


    .gdbinit: No such file or directory.

    Stopped due to shared library event
    Stopped due to shared library event
    Cannot access memory at address 0x4d348b74

    Thanks in advance

  4. #4
    oz
    oz is online now
    forum.guy
    Join Date
    May 2004
    Location
    arch linux
    Posts
    18,095
    Hello and welcome to the forums!

    Please start a new thread of your own if you wish. This thread is over 3 years old, so locking.

    Thank you.
    oz

    new members/users: read this first | new member faq
    no private messages requesting computer support - post them on the forums!
    please use the "report post" button to alert our forum admins to problematic posts rather than responding to them yourself.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...