Re: style question,itoa



Frank Cusack schrieb:
Dont intend to change that. I´m comfortable with that.
That's puzzling.
Not really. I dont like using arrays on the stack unless i can know
for certain they cant overflow. But thats my personal style.

Yes but in this case you do know they can't overflow. An int is at
most 11 chars.
Since i dont know if int can be 64bit i decided to play save there. (18bytes suffice for the number +1 for sign and +1 for the null termination).
But your statement is irrelevant anyway. If i have a pointer to stack space array (or offset into an array) i have to perform additional range checks.

Having the caller pass in a buffer also doesn't REQUIRE that that buffer
be on the stack. But I assume you are optimizing for speed (?) since
you aren't simply using sprintf(). In which case you probably would do
better to use the stack.
Yes, i do optimize for speed, as much as possible, without sacrificing robustness. And as i said i prefer pointers to arrays. Ok, so malloc/free add a larger overhead than range checks, but the worst case scenarios for heap corruption arent nearly as bad as for stack corruption.

Lastly, when the caller has to free() something, it's nicer if the
caller actually has to malloc() it as well.
Call me lazy if you wish, i prefer writing a line less ;)
It helps you read the
code better.
Good to know you know that i will have an easier time reading my code when using functions i wrote that behave as i wanted to. I didnt know that yet.

-frank
.



Relevant Pages

  • Re: ALLOCATABLE arrays
    ... || If try to create a very large array on the stack and you do not have enough ... || Allocating on the heap gives you access to a hell of a lot more memory (well ... and "heap" (and there probably are/were some computers which don't/didn't ... | Automatic arrays are always allocated on the stack. ...
    (comp.lang.fortran)
  • Re: Fortran memory allocation (stack/heap) issues
    ... > rather than Fortran, ... dynamic allocation, and relatively little stack allocation. ... value return and arrays by reference. ...
    (comp.lang.fortran)
  • Re: Stack or Heap
    ... it ran out of stack space. ... combined with a default that puts many arrays on the stack ... which conforms with the C-standard will compile without error ... seg fault due to stack/heap issues. ...
    (comp.lang.fortran)
  • Re: Need some help understanding array definitions
    ... Data structures defined with VARIABLE, CREATE, VALUE, CONSTANT, and related words are, indeed, all global. ... Unlike some languages, Forth doesn't discourage defining global data structures, but it's important to understand their proper use. ... They provide for "persistent" data, as well as space for strings and arrays. ... Strings and arrays should be in defined data structures and referenced by address and length or address on the stack. ...
    (comp.lang.forth)
  • Re: heap allocation of arrays
    ... | to force all arrays to be allocated on the heap. ... | the stack would be replaced with pointers on the stack. ... | heap is easier to detect than failure to allocate space on the ...
    (comp.lang.fortran)