Re: Proper way to input a dynamically-allocated string




"Joe Wright" <jwright@xxxxxxxxxxx> wrote
> #define SSIZE_MAX 2147483647
> #define INT_MAX 2147483647
> #define LONG_MAX 2147483647L
>
> ..and so see no compelling reason to type anything size_t rather than int.
>
> It is interesting to have functions prototyped with size_t parameters to
> indicate positive values. Otherwise, int works perfectly well for me.
>
Take this function

/*
trivial function that counts number of occurrences of ch in str
*/
mystrcount(const char *str, int ch)

Now basically this function is alwaysgoing to return small integers.
However, technically, someone could pass it a massive string, all set to one
character. Then an int would overflow, if size_t were bigger than an int.

Thus the function must return a size_t.

That means that the higher-level logic which calls it must also be written
with size_t, and the ugliness propagates


.



Relevant Pages

  • Re: Reading a string of unknown size
    ... replace above with int main ... '2 * sizeof *str'. ... failed to check the return value of mallocabove for failure. ... You've overwritten your earlier nul character. ...
    (comp.lang.c)
  • Re: parser needed
    ... static int tokenlen(const char *str, ... str - string containing expression ... ignores white space between tokens ...
    (comp.programming)
  • Re: Is anything easier to do in java than in lisp?
    ... In Linj you write: ... > argumentList in java. ... > int String.indexOf ... (position c str:from-end t) ...
    (comp.lang.lisp)
  • Re: Help beginner with simple string function
    ... int CharSpaces(char *Blabla); ... str ||!*str)? ... !*str applies the * operator to the operand string and then applies ...
    (comp.lang.c)
  • Re: Reading a string of unknown size
    ... replace above with int main ... '2 * sizeof *str'. ... failed to check the return value of mallocabove for failure. ... You've overwritten your earlier nul character. ...
    (comp.lang.c)