A Django site.
January 1, 2007

Diego Escalante
dieguito
hack my cow
» Understanding C pointers and memory leaks

Today I was at #gnome-hackers asking about pointers, memory leaks and other C things. As always an enormous amount of positive feedback was the result.
I said that I would publish the log to help any other unfortunate C novice, and then Alex Jones resumed all the IRC conversation:


Alex Jones: hey
Diego: hey
Alex Jones: i dunno whether this has clicked with you
Alex Jones: but you understand how in many scoped languages if you do like
Alex Jones: { foo = "bar"; }
Alex Jones: then outside of the } the foo is deleted
Diego: aha
Alex Jones: well really, when you do something like
Alex Jones: { char *foo; foo = whatever(); }
Alex Jones: you have a variable called "foo", which is defined in memory to be POINTING to a piece of data that is a "char"
Alex Jones: so really all that "foo" actually is
Alex Jones: is a memory address
Diego: aha
Diego: and *foo is the value of that memory
Alex Jones: YES
Alex Jones: now also
Alex Jones: whatever() is a function that returns a char*
Alex Jones: i.e. a POINTER to char data
Alex Jones: so really all that function actually returns is a number (i.e. the memory address)
Alex Jones: and again, this goes into "foo", ok?
Alex Jones: (obviously, before the function returns, it puts its string data in the memory it returns a pointer for)
Diego: aha
Alex Jones: so what happens by the end of that function is that "foo" goes out of scope
Alex Jones: and the pointer gets deleted
Alex Jones: so the number is gone
Alex Jones: but the data still exists there
Diego: aha
Alex Jones: what i find confusing about the way the GNOME guys do C is that they do like "char *foo"
Alex Jones: which makes it look as if "*foo" is the variable
Alex Jones: when it’s not
Alex Jones: "foo" is the variable
Alex Jones: char* is its type
Alex Jones: if you look at it that way, this whole thing about scope etc. makes a lot more sense
foo is just another variable, it’s of type pointer-to-char :)
Alex Jones: i hope that helps you a bit


Hope it helps someone, if I can I’ll try to translate it to Spanish.

December 26, 2006

Diego Escalante
dieguito
hack my cow
» Anewt y PHP 5.2

Estaba probando Anewt (Almost No Effort Web Toolkit) escrito por Wouter Bolsterlee y me encontré un error inocente y gracioso acerca de que la clase DateTime ya estaba definida.

Me pareció raro pues el repositorio de Anewt no había tenido cambios en mucho tiempo y pues no tenía mucho sentido que un error tan evidente y grosero estuviese en el último commit del repositorio (el cual data de hace unas semanas). Entonces le pregunté a Rudy si sabía cómo podía averiguar quién y dónde llamaba/era llamada una función pero antes de poder obtener una idea de cómo hacerlo se me vino a la mente que quizá DateTime ya estaba definido en otro lugar (quizá algún paquete de PEAR que había bajado, a pesar que mi sistema está recién instalado). Así que dije:

(01:07:33 AM) dieg0_rm: espera
(01:07:36 AM) dieg0_rm: creo que ya sé qué pasa
(01:07:47 AM) dieg0_rm: tienes idea si DateTime es una palabra reservada en php5.2?
(01:07:53 AM) stone_head: it is
(01:07:59 AM) stone_head: it breaks it all

Eso quiere decir que si tu aplicación tiene una clase con ese nombre estará inexorablemente rota al momento de actualizar a 5.2 :) .

Entonces, aquí un lo que concluímos Rudy y yo:

(01:17:39 AM) dieg0_rm: a ver blogueemos rápidamente sobre esto
(01:17:51 AM) stone_head: claro
(01:17:56 AM) stone_head: collaborative blogging
(01:18:25 AM) dieg0_rm: claro
(01:18:28 AM) dieg0_rm: pondré rápidamente
(01:18:54 AM) dieg0_rm: putos php devs: por qué carajo implementan una clase DateTime en el core de php jodiendo así miles de aplicaciones, pudieron habernos dado la gracia de los namespaces
(01:18:56 AM) dieg0_rm: end of rant
(01:19:02 AM) stone_head: <o/
(01:19:07 AM) dieg0_rm: firmado: rudy y diego
(01:19:13 AM) stone_head: ello

BTW, Feliz navidad!.