[OT] Re: Why Is Escaping Data Considered So Magical?

Michael Torrie torriem at gmail.com
Wed Jun 30 00:28:25 EDT 2010


On 06/29/2010 10:17 PM, Michael Torrie wrote:
> On 06/29/2010 10:05 PM, Michael Torrie wrote:
>> #include <stdio.h>
>>
>> int main(int argc, char ** argv)
>> {
>> 	char *buf = malloc(512 * sizeof(char));
>> 	const int a = 2, b = 3;
>> 	snprintf(&buf, sizeof buf, "%d + %d = %d\n", a, b, a + b);
>                        ^^^^^^^^^^
> Make that 512*sizeof(buf)

Sigh.  Try again.  How about "512 * sizeof(char)" ?  Still doesn't make
a different.  The code still crashes because the &buf is incorrect.

Another reason python programming is just so much funner and easier!

This little diversion is fun though.  C is pretty powerful and I enjoy
it, but it sure keeps one on one's toes.  I made a similar mistake to
the &buf thing years ago when I thought I could return strings (char *)
from functions on the stack the way Pascal and BASIC could.  It was only
by pure luck that my code worked as the part of the stack being accessed
was invalid and could have been overwritten.

>> 	fprintf(stdout, buf);
>> 	free(buf);
>> 	return 0;
>> } /*main*/




More information about the Python-list mailing list