Ask a Question related to UNIX Programming, Design and Development.

  1. #1

    Default getting time string

    I have pieced together some functions to get the current time as a string.
    This is used in a logging function and works a few times, but after being
    called
    more than about 6 times, it causes a core dump.
    Is there a better way of getting the time as a string?

    my code
    --------------------------------------------
    time_t *etime; // pointer to unix time
    char *stime; // time string

    time(etime); // get current unix time
    stime = ctime(etime); // convert to string
    stime[24] = 0; // remove \n
    ---------------------------------------------

    --

    Thanks,
    Matt Churchyard


    Matt Churchyard Guest

  2. Similar Questions and Discussions

    1. SQL generates 'access violation or syntax' error whencompiled into string var ahead of time
      I have stumbled across something perplexing and frustrating, and I'm hoping somebody can shed some light here. I have a site with some listings....
    2. Maintain query string and somehow auto refresh a pagewith that string intact
      I have a drill down where on page one the user selects criteria to narrow down the search for a speicific group of employees(like all hired between...
    3. Best way to process a string 1 char at a time
      John Heim wrote: for ($i = 0; $i < strlen($str); $i++) { strFunction($str{$i}); } is one way.
    4. Cannot create an object of type 'System.String[]' from its representation 'String[] Array'
      Hello, I am designing a .net custom control in VS.net 7.1 and my control exposes an array of strings which are supposed to be the items to show. To...
  3. #2

    Default Re: getting time string

    Matt Churchyard <matt@userve.net> wrote:
    > I have pieced together some functions to get the current time as a string.
    > This is used in a logging function and works a few times, but after being
    > called
    > more than about 6 times, it causes a core dump.
    > Is there a better way of getting the time as a string?
    > my code
    > --------------------------------------------
    > time_t *etime; // pointer to unix time
    > char *stime; // time string
    > time(etime); // get current unix time
    From the man page of time(2):

    time returns the time since the Epoch (00:00:00 UTC, Jan-
    uary 1, 1970), measured in seconds.

    If t is non-NULL, the return value is also stored in the
    memory pointed to by t.

    In your case etime is an unitialized pointer, which is probably *not*
    NULL, but pointing at some random place in memory. Thus time() writes
    over this memory and if you're lucky, you get a crash because you
    destroyed someting you needed immediately. Either change your code so
    that you have

    time_t etime;
    time( &etime );

    or

    time_t etime;
    etime = time( NULL );
    Regards, Jens
    --
    _ _____ _____
    | ||_ _||_ _| [email]Jens.Toerring@physik.fu-berlin.de[/email]
    _ | | | | | |
    | |_| | | | | | [url]http://www.physik.fu-berlin.de/~toerring[/url]
    \___/ens|_|homs|_|oerring
    Jens.Toerring@physik.fu-berlin.de Guest

Posting Permissions

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139