[M3devel] efficient "safe" Modula-3?
Jay
jayk123 at hotmail.com
Tue Jan 1 22:33:18 CET 2008
How can I write this in idiomatic Modula-3:
date : Date.T Fmt.FN( (* heap alloc *) "%04s-%02s-%02s %02s:%02s:%02s", ARRAY OF TEXT{ (* heap alloc? easily avoided, but just over the limit *) Fmt.Int(date.year), (* heap alloc *) Fmt.Int(ORD(date.month) + 1), (* heap alloc *) Fmt.Int(date.day), (* heap alloc *) Fmt.Int(date.hour), (* heap alloc *) Fmt.Int(date.minute), (* heap alloc *) Fmt.Int(date.second) (* heap alloc *) })));
to be more like in performance to:
Date_t date; /* Win32 SYSTEMTIME essentially */ char Buffer[sizeof("2008-01-01 20:29:58")]; sprintf( Buffer, "%04u-%02u-%02u %02u:%02u:%02u", date.year, ORD(date.month) + 1, date.day, date.hour, date.minute, date.second);
or, ok:
Date_t date; /* Win32 SYSTEMTIME essentially */ char* Buffer = (char*) malloc(sizeof("2008-01-01 20:29:58")); /* deliberate heap alloc for long lived string */ if (Buffer== NULL)
... sprintf( Buffer, "%04u-%02u-%02u %02u:%02u:%02u", date.year, ORD(date.month) + 1, date.day, date.hour, date.minute, date.second);
Unnecessary heap allocations bug me.
I seem to see a fair amount of tedious Modula-3 code around to avoid heap allocs,folks manually managing growing arrays instead of using something generic and efficient.
Can "safe" code be efficient and convenient to write?
(and yes, I know about snprintf, sprintf_s, etc.)
- Jay
_________________________________________________________________
The best games are on Xbox 360. Click here for a special offer on an Xbox 360 Console.
http://www.xbox.com/en-US/hardware/wheretobuy/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://m3lists.elegosoft.com/pipermail/m3devel/attachments/20080101/6b1ce111/attachment-0001.html>
More information about the M3devel
mailing list