[Tfug] Need help with a C++ algorithm

Stephen Hooper stephen.hooper at gmail.com
Thu Mar 15 03:12:40 MST 2007


On 3/15/07, Brian Murphy <murphy+tfug at email.arizona.edu> wrote:
> Quoting Stephen Hooper <stephen.hooper at gmail.com>:
> > Hate to disagree with you there dude, but yes there is:
>
>
> You're making a really heavy assumption on the implementation of malloc
> to achieve your results.

I thought I said that with "it is horribly system specific" :)

>
> You don't account for the fact that a pointer can point to a memory
> region not created by malloc.
>

True.

> For example, replace your
>
> start = malloc(strtol(argv[1],NULL,16) * sizeof(char));
>
> with
>
> char string[100];
> start = string;
>
> QED. :-)


In which case (and which I didn't really think he was asking about),
you should really know the value anyway (in your case:
sizeof(string)/sizeof(char)).

And before you say it, that it could be fixed length, in a library,
without the source code... true, but then you could always do some
fancy footwork IN A HORRIBLY SYSTEM SPECIFIC WAY (probably involving a
signal handler), and write data like so...:

for(i = 0; ; i++) start[i] = 0xFF;

and then waiting for the bus error (or whatever :) )

Or even easier... if it is initialized in the library I think (and if
I am thinking about it right) it would have to be global so:

for(i = 0;start[i] != 0; i++); /* HORRIBLY
SYSTEM/PROCESSOR/COMPILER/STATE SPECIFIC */
printf("%ld\n",i);

"quod erat demonstrandum": Getting a little cocky now that you are
superhero?  ;)

<mumbles>
I could have said "Q.E.D." to your heavy-handed "No there is not," but
I am much too modest...

It would have made more sense than your using it as "Yes, there is:
no guarantees, but yes there is..."
</mumbles>




More information about the tfug mailing list