Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

ctime causes segmentation fault.

285 views
Skip to first unread message

Castelnuovo

unread,
Dec 29, 1998, 3:00:00 AM12/29/98
to
Castelnuovo wrote:

> I' m trying to write a piece of a procedure that logs connection from a
> socket. The part that should get the time is as follow.
>
> time_t *date=(time_t *)malloc(sizeof(date));
> char *the_date;
> ........
> date=(time_t*)time(date);
> the_date=ctime(date);
>
> when the program execute the ctime instruction it give a segmentation
> fault .
> I know that arg1 of ctime should be (const time_t *) but making the cast
> doesn't help .
> I've had the similar problem with char and char * , but I' ve made a
> translation routine that make it go. With dates I' can't figure out the
> way.
> Any suggestion is welcome especially by e-mail.
> Thanks.


paolo...@my-dejanews.com

unread,
Dec 29, 1998, 3:00:00 AM12/29/98
to
In article <36888ACF...@tin.it>,
There are several errors in your program due to confusion between pointers and
object pointed to. Write instead (after inclusion of time.h):

time_t date;
char *the_date;

date=time(NULL);
if(date == -1) {
puts("Problem with time");
return 0;
}
the_date=ctime(&date);

printf("%s\n", the_date);

Dynamic allocation is useless. Casting the output of time to time_t* is
a major bug. Defining date as a pointer gives many more trouble than it saves.
Ciao Paolo

-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/ Search, Read, Discuss, or Start Your Own

Alex...@scitex.com

unread,
Dec 29, 1998, 3:00:00 AM12/29/98
to xas...@tin.it
In article <36888ACF...@tin.it>,
Castelnuovo <xas...@tin.it> wrote:
> Castelnuovo wrote:
>
> > I' m trying to write a piece of a procedure that logs connection from a
> > socket. The part that should get the time is as follow.
> >
> > time_t *date=(time_t *)malloc(sizeof(date));
> > char *the_date;
> > ........
> > date=(time_t*)time(date);
^
|
That's your problem.
Superfluous casts are plain dangerous, as you may see here:
time_t time(time_t *tod);
You are forcefully casting time_t to time_t* and assigning it
to a pointer variable. In absence of this cast you'd get compilation
error instead of run-time failure.

> > the_date=ctime(date);
> >
> > when the program execute the ctime instruction it give a segmentation
> > fault .

--
Regards,
Alex Krol
Disclaimer: I'm not speaking for Scitex Corporation Ltd

Lawrence Kirby

unread,
Dec 29, 1998, 3:00:00 AM12/29/98
to
In article <76agtd$f63$1...@nnrp1.dejanews.com> paolo...@my-dejanews.com writes:

...

> date=time(NULL);
> if(date == -1) {

Note that this should be

> if(date == (time_t)-1) {

They don't necessarily do the same thing (even though the conditions where
they differ are fairly obscure).

--
-----------------------------------------
Lawrence Kirby | fr...@genesis.demon.co.uk
Wilts, England | 7073...@compuserve.com
-----------------------------------------


Francois Gagnon

unread,
Dec 30, 1998, 3:00:00 AM12/30/98
to
Try this:
time_t *date=(time_t *)malloc(sizeof(time_t));
instead of
time_t *date=(time_t *)malloc(sizeof(date));

Francois Gagnon

Castelnuovo wrote:
>
> Castelnuovo wrote:
>
> > I' m trying to write a piece of a procedure that logs connection from a
> > socket. The part that should get the time is as follow.
> >
> > time_t *date=(time_t *)malloc(sizeof(date));
> > char *the_date;
> > ........
> > date=(time_t*)time(date);

> > the_date=ctime(date);
> >
> > when the program execute the ctime instruction it give a segmentation
> > fault .

> > I know that arg1 of ctime should be (const time_t *) but making the cast
> > doesn't help .
> > I've had the similar problem with char and char * , but I' ve made a
> > translation routine that make it go. With dates I' can't figure out the
> > way.
> > Any suggestion is welcome especially by e-mail.
> > Thanks.

--
------------------------------------------------------------
"You can make more friends in two months by becoming genuinely
interseted
in other people than you can in two years by trying to get other people
interested in you."
-Dale
Carnegie

------------------------------------------------------------
Francois Gagnon
email:
mailto:025...@dragon.acadiau.ca mailto:fga...@whitehouse.acl.ca
mailto:fga...@hfx.andara.com mailto:lev...@hotmail.com
Web Page:
http://dragon.acadiau.ca/~025260g http://users.andara.com/~fgagnon
ICQ: 13298594
Phone: (902)452-3946
Adress: 1338 Hollis St. app: 401
Halifax, Nova Scotia, Canada, B3J 1T8
------------------------------------------------------------

Michael J. Tobler

unread,
Dec 30, 1998, 3:00:00 AM12/30/98
to
In article <368AAF29...@hfx.andara.com>, fga...@hfx.andara.com
says...

> Try this:
> time_t *date=(time_t *)malloc(sizeof(time_t));
> instead of
> time_t *date=(time_t *)malloc(sizeof(date));

Geez, what's the big bugaloo? Why not just:

time_t currTime = time( NULL ) ;

????????????

What's with all the memory management stuff?

>
> Francois Gagnon
>
[snip]


--
<<<<<<<<<< Blue Skies >>>>>>>>>>>
< Michael J. Tobler >
< mto...@no-spam-ibm.net >
< remove "no-spam-" when replying >
<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>

Phlip

unread,
Dec 31, 1998, 3:00:00 AM12/31/98
to
Francois Gagnon escribió:

>Try this:
> time_t *date=(time_t *)malloc(sizeof(time_t));
>instead of
> time_t *date=(time_t *)malloc(sizeof(date));

I always felt the following was just one scratch better:

time_t *date=(time_t *)malloc(sizeof *date);

Of course details like this are much easier to get right than the real
maintenance hazards in my code are!

-- Phlip at politizen dot com (address munged)
======= http://users.deltanet.com/~tegan/home.html =======
-- Now collecting votes for a new group:
news:alt.flame.moderated --

Ben Pfaff

unread,
Dec 31, 1998, 3:00:00 AM12/31/98
to
"Phlip" <add...@web.page> writes:

Francois Gagnon escribió:

>Try this:
> time_t *date=(time_t *)malloc(sizeof(time_t));
>instead of
> time_t *date=(time_t *)malloc(sizeof(date));

I always felt the following was just one scratch better:

time_t *date=(time_t *)malloc(sizeof *date);

Better yet:

time_t *date = malloc(sizeof *date);

Don't use casts when they're not needed.
--
(supporter of the campaign for grumpiness where grumpiness is due in c.l.c)

Please: do not email me copies of your posts to comp.lang.c
do not ask me C questions via email; post them instead

Al Bowers

unread,
Jan 1, 1999, 3:00:00 AM1/1/99
to

Castelnuovo wrote:

> Castelnuovo wrote:
>
> > I' m trying to write a piece of a procedure that logs connection from a
> > socket. The part that should get the time is as follow.
> >
> > time_t *date=(time_t *)malloc(sizeof(date));

time_t *date = malloc(sizeof(*date));

> > char *the_date;
> > ........
> > date=(time_t*)time(date);

time() returns type time_t. casting totime_t* is an error. You can use:
*date = time(NULL);

> > the_date=ctime(date);

You never dynamically allocated memorystorage for the_date.

> >
> > when the program execute the ctime instruction it give a segmentation
> > fault .
> > I know that arg1 of ctime should be (const time_t *) but making the cast
> > doesn't help .

That is not a problem.

> > I've had the similar problem with char and char * , but I' ve made a
> > translation routine that make it go. With dates I' can't figure out the
> > way.

With the corrections mentioned :#include <stdio.h>
#include <time.h>
#include <stdlib.h>

int main(void) {
time_t *date;
char *the_date;

if((date = malloc(sizeof(*date))) == NULL) {
puts("Memory allocation error");
exit(EXIT_FAILURE);
}
if((*date = time(NULL)) == (time_t)-1) {
puts("Time is unavailable");
free(date);
exit(EXIT_FAILURE);
}
if((the_date = malloc(32)) == NULL) {
puts("Memory allocation error");
free(date);
exit(EXIT_FAILURE);
}
the_date = ctime(date);
printf("The date: %s",the_date);
free(date);
free(the_date);
return 0;
}

--
Al Bowers
Tampa, FL
mailto:abo...@combase.com
http:www.gate.net/~abowers/index.html


Brian B. McGuinness

unread,
Jan 5, 1999, 3:00:00 AM1/5/99
to
Al Bowers wrote:

> With the corrections mentioned :

...

> if((the_date = malloc(32)) == NULL) {
> puts("Memory allocation error");
> free(date);
> exit(EXIT_FAILURE);
> }

You don't want to do this.

> the_date = ctime(date);

At this point, the pointer to the memory youallocated is replaced with a
pointer to ctime()'s
internal buffer. So the 32 bytes you got from
malloc() are lost.

> printf("The date: %s",the_date);
> free(date);
> free(the_date);

Now you are attempting to free ctime()'sinternal static buffer.

> return 0;
> }

Instead, you can simply do:

printf ("The date: %s", ctime (date));

or, since this is C++,

cout << "The date: " << ctime (date);

By the way, you may want to append a
newline to the output.

--- Brian

Lawrence Kirby

unread,
Jan 6, 1999, 3:00:00 AM1/6/99
to
In article <3692597E...@lmco.com>
brian.b.m...@lmco.com "Brian B. McGuinness" writes:

...

>Instead, you can simply do:
>
>printf ("The date: %s", ctime (date));
>
>or, since this is C++,
>
>cout << "The date: " << ctime (date);
>
>By the way, you may want to append a
>newline to the output.

Or possibly not since the string generated by asctime() and ctime()
already has a new-line at the end. Sadly however this tends to be more
often a hindrance than a help.

Brian B. McGuinness

unread,
Jan 6, 1999, 3:00:00 AM1/6/99
to
Lawrence Kirby wrote:

> In article <3692597E...@lmco.com>
> brian.b.m...@lmco.com "Brian B. McGuinness" writes:
>
> ...

> >cout << "The date: " << ctime (date);
> >
> >By the way, you may want to append a
> >newline to the output.
>
> Or possibly not since the string generated by asctime() and ctime()
> already has a new-line at the end. Sadly however this tends to be more
> often a hindrance than a help.

Oops -- I forgot about that as I haven't used these functions much.
I prefer strftime().

--- Brian


0 new messages