> 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.
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
> > 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
...
> 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
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
------------------------------------------------------------
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 >
<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>
>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 --
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
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
> 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
...
>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.
> 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