Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:41950 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 30008 invoked from network); 16 Nov 2008 13:47:11 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 16 Nov 2008 13:47:11 -0000 Authentication-Results: pb1.pair.com smtp.mail=thetaphi@php.net; spf=unknown; sender-id=unknown Authentication-Results: pb1.pair.com header.from=thetaphi@php.net; sender-id=unknown Received-SPF: unknown (pb1.pair.com: domain php.net does not designate 80.190.230.99 as permitted sender) X-PHP-List-Original-Sender: thetaphi@php.net X-Host-Fingerprint: 80.190.230.99 www.troja.net Linux 2.5 (sometimes 2.4) (4) Received: from [80.190.230.99] ([80.190.230.99:51532] helo=mail.troja.net) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 2F/23-13764-D5420294 for ; Sun, 16 Nov 2008 08:47:11 -0500 Received: from localhost (localhost [127.0.0.1]) by mail.troja.net (Postfix) with ESMTP id 392154D22F; Sun, 16 Nov 2008 14:47:07 +0100 (CET) Received: from mail.troja.net ([127.0.0.1]) by localhost (cyca.troja.net [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 26205-09; Sun, 16 Nov 2008 14:47:03 +0100 (CET) Received: from VEGA (port-83-236-62-68.dynamic.qsc.de [83.236.62.68]) (using SSLv3 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by mail.troja.net (Postfix) with ESMTP id 31E3E4D1FB; Sun, 16 Nov 2008 14:47:02 +0100 (CET) To: "'Chris Jiang'" , , =?utf-8?Q?'=22M._Karpel=C3=A8s=22'?= References: <56.41.13764.40D10294@pb1.pair.com> <1226841630.14940.26.camel@localhost> <492021AB.9040106@gmail.com> Date: Sun, 16 Nov 2008 14:47:02 +0100 Message-ID: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-Mailer: Microsoft Office Outlook 11 In-Reply-To: <492021AB.9040106@gmail.com> Thread-Index: AclH8FDL77CChI5BSz6kVJlk+GUswgAAEQeA X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.5579 Subject: RE: [PHP-DEV] Can someone explain me why this happens please? From: thetaphi@php.net ("Uwe Schindler") This is per definition: In C local variables are not initialized with = anything! The weird characters you see are content from prior memory = usage leftover from calls to other functions. Its just garbage. In C, = local variables must always be initialized. ----- Uwe Schindler thetaphi@php.net - http://www.php.net NSAPI SAPI developer Bremen, Germany > -----Original Message----- > From: Chris Jiang [mailto:jiangcat@gmail.com] > Sent: Sunday, November 16, 2008 2:36 PM > To: internals@lists.php.net; "M. Karpel=C3=A8s" > Cc: internals@lists.php.net > Subject: Re: [PHP-DEV] Can someone explain me why this happens please? >=20 > Ah, greet appreciation! The memset() way works perfectly, but the = res[0] > way didn't work. >=20 > Beside, I think you are right. Although I'm just a beginner in C > language, but from the experience of JS and PHP, also according to the > document I've been searching all the time, there should be something > related to time.h functions which might be more suitable for what I'm > doing. However, at the mean time, I'm just trying to get 'strings' to > work in C and Zend, hehe! >=20 > Still, this is a really strange experience. Where are those characters > come from? Shouldn't it be a clean array when I first created them? > Anything related to the zval structure? >=20 > Thank you again! >=20 >=20 > M. Karpel=C3=A8s wrote: > > Hi, > > > > Try initializing your "res" variable first. > > > > either: > > > > memset(&res, 0, sizeof(res)); > > > > Or: > > > > res[0] =3D 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 =C3=A0 21:15 +0800, Chris Jiang a = =C3=A9crit : > >> 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: > >> > >> (!=E9=9D=A0=E5=A4=87h2339:44:05 > >> (!=E9=9D=A0=E5=A4=87=E7=AA=B5=E2=96=9297D 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 =3D 0; > >> double myd, myh, mym; > >> zend_bool useDay =3D 0; > >> > >> if ( zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, > ZEND_NUM_ARGS() > >> TSRMLS_CC, "l|b", &mys, &useDay) =3D=3D 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 =3D mys / 86400; > >> mys %=3D 86400; > >> sprintf(myb, "%.0f", myd); > >> strcat(res, myb); > >> strcat(res, "D "); > >> } > >> > >> myh =3D mys / 3600; > >> mys %=3D 3600; > >> if ( myh < 10 ) > >> strcat(res, "0"); > >> sprintf(myb, "%.0f", myh); > >> strcat(res, myb); > >> > >> strcat(res, ":"); > >> > >> mym =3D mys / 60; > >> mys %=3D 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); > >> } > >> > > >=20 > > Hi, > > > > Try initializing your "res" variable first. > > > > either: > > > > memset(&res, 0, sizeof(res)); > > > > Or: > > > > res[0] =3D 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 =C3=A0 21:15 +0800, Chris Jiang a = =C3=A9crit : > >> 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: > >> > >> (!=E9=9D=A0=E5=A4=87h2339:44:05 > >> (!=E9=9D=A0=E5=A4=87=E7=AA=B5=E2=96=9297D 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 =3D 0; > >> double myd, myh, mym; > >> zend_bool useDay =3D 0; > >> > >> if ( zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, > ZEND_NUM_ARGS() > >> TSRMLS_CC, "l|b", &mys, &useDay) =3D=3D 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 =3D mys / 86400; > >> mys %=3D 86400; > >> sprintf(myb, "%.0f", myd); > >> strcat(res, myb); > >> strcat(res, "D "); > >> } > >> > >> myh =3D mys / 3600; > >> mys %=3D 3600; > >> if ( myh < 10 ) > >> strcat(res, "0"); > >> sprintf(myb, "%.0f", myh); > >> strcat(res, myb); > >> > >> strcat(res, ":"); > >> > >> mym =3D mys / 60; > >> mys %=3D 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); > >> } > >> > > >=20 > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: http://www.php.net/unsub.php