questions about size_t | | |
Hello,
I have some questions about the size_t type.
First, what do we know about size_t? From what I have
read I believe that it is an unsigned integer,
but not necessarily an int. Am I correct?
Does this mean that if I need to compare two variables,
one of the size_t type, should the other also be a size_t
variable? There could be problems if I compare it with an
int, if those have different sizes, right?
Most of the time when I make for loops, I use an int variable
as index, like for (int i = 0; ...)
Should I change that to size_t if I need to compare the i
variable with the return value of strlen(), for example?
I think that just adds more things to think about,
wouldn't it be easier to just use an unsigned int, instead
of the size_t variable for strlen, sizeof, etc. ?
Whats the benefit of size_t?
Is there advantage using it for your own stuff, like
struct {
size_t size;
void *stuff;
};
or
make_balloon(size_t balloonsize);
or should it only be used for the standard functions? | | | | re: questions about size_t
edware wrote:[color=blue]
> Hello,
> I have some questions about the size_t type.
> First, what do we know about size_t? From what I have
> read I believe that it is an unsigned integer,
> but not necessarily an int. Am I correct?
>[/color]
size_t is an unsigned integer type large enough to hold the value
returned from sizeof. The actual int types that correspond to size_t
can vary.
[color=blue]
> Does this mean that if I need to compare two variables,
> one of the size_t type, should the other also be a size_t
> variable? There could be problems if I compare it with an
> int, if those have different sizes, right?
>
> Most of the time when I make for loops, I use an int variable
> as index, like for (int i = 0; ...)
> Should I change that to size_t if I need to compare the i
> variable with the return value of strlen(), for example?
>[/color]
Since size_t is defined as being unsigned, as long as you are sure your
int is not negative, the implicit casting (and I'm fuzzy on that) during
the compare should be ok.
[color=blue]
> I think that just adds more things to think about,
> wouldn't it be easier to just use an unsigned int, instead
> of the size_t variable for strlen, sizeof, etc. ?
>[/color]
Well, since size_t will always fit whatever sizeof can return, I'm
thinking that an int type holds an integer value and a size_t type
definitions hold some sort of size representation. Indeed, these are
closely related, but the size of an object is certainly different than
the value of it.
So, those functions that depend on sizes would be more correct in using
one of the type definitions (e.g., wchar_t, size_t).
[color=blue]
> Whats the benefit of size_t?
> Is there advantage using it for your own stuff, like
> struct {
> size_t size;
> void *stuff;
> };
> or
> make_balloon(size_t balloonsize);
>
> or should it only be used for the standard functions?[/color]
A good question. I pretty much use integer types interchangeably,
though I'm sure I'm missing something. | | | | re: questions about size_t
In article <Gl64g.54611$d5.209190@newsb.telia.net>,
edware <erixk@hotmail.com> wrote:[color=blue]
>I have some questions about the size_t type.
>First, what do we know about size_t? From what I have
>read I believe that it is an unsigned integer,
>but not necessarily an int. Am I correct?[/color]
Right -- for example it might be unsigned long long .
[color=blue]
>Does this mean that if I need to compare two variables,
>one of the size_t type, should the other also be a size_t
>variable? There could be problems if I compare it with an
>int, if those have different sizes, right?[/color]
The int would be promoted to the appropriate size and converted to
unsigned before the comparison. If the int happens to be negative,
you had better know what you are doing...
[color=blue]
>Most of the time when I make for loops, I use an int variable
>as index, like for (int i = 0; ...)
>Should I change that to size_t if I need to compare the i
>variable with the return value of strlen(), for example?[/color]
As long as the int values are non-negative, the compiler will take
care of the details in the way you would expect.
[color=blue]
>I think that just adds more things to think about,
>wouldn't it be easier to just use an unsigned int, instead
>of the size_t variable for strlen, sizeof, etc. ?[/color]
[color=blue]
>Whats the benefit of size_t?
>Is there advantage using it for your own stuff, like
>struct {
> size_t size;
> void *stuff;
>};
>or
>make_balloon(size_t balloonsize);[/color]
[color=blue]
>or should it only be used for the standard functions?[/color]
size_t measures the size (in bytes) of an object. You might happen
to be compiling and executing on a system in which the maximum allocatable
memory was (say) 32 terabytes, but for which the standard unsigned int only
goes to (say) 65535 or [more commonly these days] 4294967296 (4 gigabytes).
int is more or less the "natural" computation size for "most" problems,
not too big and not too small -- but there are times when you need
a lot more range, even if the extended range is slower to compute with.
--
I was very young in those days, but I was also rather dim.
-- Christopher Priest | | | | re: questions about size_t
edware wrote:[color=blue]
>
> Hello,
> I have some questions about the size_t type.
> First, what do we know about size_t? From what I have
> read I believe that it is an unsigned integer,
> but not necessarily an int. Am I correct?[/color]
Yes.
[color=blue]
> Does this mean that if I need to compare two variables,
> one of the size_t type, should the other also be a size_t
> variable?
> There could be problems if I compare it with an
> int, if those have different sizes, right?[/color]
Don't worry about comparing different size types.
I do make it a point however,
to compare signed types to signed types
and unsigned types to unsigned types.
[color=blue]
> Most of the time when I make for loops, I use an int variable
> as index, like for (int i = 0; ...)
> Should I change that to size_t if I need to compare the i
> variable with the return value of strlen(), for example?[/color]
I probably would.
[color=blue]
> Whats the benefit of size_t?[/color]
unsigned, might not be large enough to do the job of size_t.
unsigned long, might be slow.
size_t has the ability to be the fastest type,
that is also big enough to do the job.
--
pete | | | | re: questions about size_t
pete <pfiland@mindspring.com> writes:
[color=blue]
> edware wrote:[color=green]
>>
>> Hello,
>> I have some questions about the size_t type.
>> First, what do we know about size_t? From what I have
>> read I believe that it is an unsigned integer,
>> but not necessarily an int. Am I correct?[/color]
>
> Yes.[/color]
*Definitely* not an int, because that's not an unsigned integer
type.
--
"Some people *are* arrogant, and others read the FAQ."
--Chris Dollin | | | | re: questions about size_t
Ben Pfaff wrote:[color=blue]
> pete <pfiland@mindspring.com> writes:
>[color=green]
>> edware wrote:[color=darkred]
>>> Hello,
>>> I have some questions about the size_t type.
>>> First, what do we know about size_t? From what I have
>>> read I believe that it is an unsigned integer,
>>> but not necessarily an int. Am I correct?[/color]
>> Yes.[/color]
>
> *Definitely* not an int, because that's not an unsigned integer
> type.[/color]
I meant a unsigned int. |  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,449 network members.
|