Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:41944 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 22123 invoked from network); 16 Nov 2008 13:20:39 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 16 Nov 2008 13:20:39 -0000 Authentication-Results: pb1.pair.com smtp.mail=karpeles@ookoo.org; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=karpeles@ookoo.org; sender-id=pass Received-SPF: pass (pb1.pair.com: domain ookoo.org designates 88.191.88.38 as permitted sender) X-PHP-List-Original-Sender: karpeles@ookoo.org X-Host-Fingerprint: 88.191.88.38 lamune.ookoo.org Linux 2.6 Received: from [88.191.88.38] ([88.191.88.38:47483] helo=Lamune.ookoo.org) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 0C/81-13764-62E10294 for ; Sun, 16 Nov 2008 08:20:39 -0500 Received: (pmaild 936620 invoked for user karpeles@ookoo.org); Sun, 16 Nov 2008 14:20:31 +0100 Received: from [192.168.0.25] (30.3.207-77.rev.gaoland.net [77.207.3.30]) by Lamune.ookoo.org (phpinetd by MagicalTux ) with SMTP version 1.0; Sun, 16 Nov 2008 14:20:31 +0100 To: Chris Jiang Cc: internals@lists.php.net In-Reply-To: <56.41.13764.40D10294@pb1.pair.com> References: <56.41.13764.40D10294@pb1.pair.com> Content-Type: text/plain; charset=UTF-8 Organization: ooKoo.org Date: Sun, 16 Nov 2008 14:20:30 +0100 Message-ID: <1226841630.14940.26.camel@localhost> Mime-Version: 1.0 X-Mailer: Evolution 2.22.3.1 Content-Transfer-Encoding: 8bit Subject: Re: [PHP-DEV] Can someone explain me why this happens please? From: karpeles@ookoo.org ("M." =?ISO-8859-1?Q?Karpel=E8s?=) Hi, Try initializing your "res" variable first. either: memset(&res, 0, sizeof(res)); Or: res[0] = 0; Both will work, it all depends if you want to write "clean" code, of "fast" code (and your definition of both). I believe there are cleaner way to do what you are doing, but I'll let that to your curiosity. Mark Le dimanche 16 novembre 2008 à 21:15 +0800, Chris Jiang a écrit : > Hi all, since I started playing around with Zend API, I thought it would > be better to start writing some small functions as practice. Then my > entire weekend became nightmare. > > I've tried to add a function activated in dynamic module, and it gives a > very strange result. This function is really simple: taking an integer > as argument, returns a 'hhhh:mm:ss' format string. If additional > argument (BOOL) is set to true, then the 'hhhh' will turn to 'DD hh'. > > Now, it works fine while compiled with GCC alone in standard C style, > but while working as a PHP function, the result is like this: > > (!靠备h2339:44:05 > (!靠备窵▒97D 11:44:05 > > What are the characters in front of them? Where did they come? It's > really confusing...... > > The original Zend style code is as follow: > > -------------------------------------------------------------------- > > ZEND_FUNCTION(cj_format_clock) > { > char res[50], myb[10]; > long mys = 0; > double myd, myh, mym; > zend_bool useDay = 0; > > if ( zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS() > TSRMLS_CC, "l|b", &mys, &useDay) == FAILURE ) > { > php_error(E_ERROR, "Expecting cj_format_clock([(INT)seconds])", > get_active_function_name(TSRMLS_C)); > return; > } > > if ( mys < 0 ) > { > php_error(E_ERROR, "Number of second must be a possitive integer", > get_active_function_name(TSRMLS_C)); > return; > } > > if ( useDay ) > { > myd = mys / 86400; > mys %= 86400; > sprintf(myb, "%.0f", myd); > strcat(res, myb); > strcat(res, "D "); > } > > myh = mys / 3600; > mys %= 3600; > if ( myh < 10 ) > strcat(res, "0"); > sprintf(myb, "%.0f", myh); > strcat(res, myb); > > strcat(res, ":"); > > mym = mys / 60; > mys %= 60; > if ( mym < 10 ) > strcat(res, "0"); > sprintf(myb, "%.0f", mym); > strcat(res, myb); > > strcat(res, ":"); > > if ( mys < 10 ) > strcat(res, "0"); > sprintf(myb, "%d", mys); > strcat(res, myb); > > RETURN_STRING(res, 1); > } >