Re: Proper way to input a dynamically-allocated string
- From: Keith Thompson <kst-u@xxxxxxx>
- Date: Sun, 11 Dec 2005 23:17:52 GMT
"Malcolm" <regniztar@xxxxxxxxxxxxxx> writes:
> "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
I fail to see what's ugly about it. What's wrong with using size_t to
represent sizes? That's what it's for.
--
Keith Thompson (The_Other_Keith) kst-u@xxxxxxx <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
.
- References:
- Proper way to input a dynamically-allocated string
- From: Michel Rouzic
- Re: Proper way to input a dynamically-allocated string
- From: Eric Sosman
- Re: Proper way to input a dynamically-allocated string
- From: Michel Rouzic
- Re: Proper way to input a dynamically-allocated string
- From: Eric Sosman
- Re: Proper way to input a dynamically-allocated string
- From: Michel Rouzic
- Re: Proper way to input a dynamically-allocated string
- From: Flash Gordon
- Re: Proper way to input a dynamically-allocated string
- From: Michel Rouzic
- Re: Proper way to input a dynamically-allocated string
- From: Malcolm
- Re: Proper way to input a dynamically-allocated string
- From: Ben Pfaff
- Re: Proper way to input a dynamically-allocated string
- From: Joe Wright
- Re: Proper way to input a dynamically-allocated string
- From: Malcolm
- Proper way to input a dynamically-allocated string
- Prev by Date: Re: C, really portable?
- Next by Date: Re: C, really portable?
- Previous by thread: Re: Proper way to input a dynamically-allocated string
- Next by thread: Re: Proper way to input a dynamically-allocated string
- Index(es):
Relevant Pages
|