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? 5 3144
edware wrote: 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?
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.
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?
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.
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. ?
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).
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?
A good question. I pretty much use integer types interchangeably,
though I'm sure I'm missing something.
In article <Gl*******************@newsb.telia.net>,
edware <er***@hotmail.com> wrote: 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?
Right -- for example it might be unsigned long long .
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?
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...
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?
As long as the int values are non-negative, the compiler will take
care of the details in the way you would expect.
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?
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
edware wrote: 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?
Yes.
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?
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.
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 probably would.
Whats the benefit of size_t?
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
pete <pf*****@mindspring.com> writes: edware wrote: 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?
Yes.
*Definitely* not an int, because that's not an unsigned integer
type.
--
"Some people *are* arrogant, and others read the FAQ."
--Chris Dollin
Ben Pfaff wrote: pete <pf*****@mindspring.com> writes:
edware wrote: 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? Yes.
*Definitely* not an int, because that's not an unsigned integer type.
I meant a unsigned int. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: DarkSpy |
last post by:
Question 1:
class A
{
public:
operator int () { return 10; }
operator int () const { return operator int(); }
operator std::string() { return "abcd"; }
operator std::string() const { return...
|
by: cj |
last post by:
Dear friends, I have one more questions for everyone in the newsgroup:
I am preparing for an interview on UNIX/C++. Could you please identify some
of the most important questions which might be...
|
by: Gumby |
last post by:
I want to make a two-d array of unsigned ints that
I can change the size of as I need more memory. To
do this I put in the h file a simple pointer to the
memory and row/col variables to retain the...
|
by: Walter Dnes |
last post by:
I have taken an intro evening C course at a local university, but my
C programming experience is otherwise nill. I've come up with a little
project that I want to cut my teeth on. After reading...
|
by: prashna |
last post by:
Hi Guru's,
Here are my questions...
1)Why does c allows an extra "," in array intialiser?Is there any
advantage of this?
ex: int arr={1,2,3,4,5,};
^^Compiler does not give error for this!
...
|
by: Rano |
last post by:
/*
Hello,
I've got some troubles with a stupid program...
In fact, I just start with the C language and sometime
I don't understand how I really have to use malloc.
I've readden the FAQ...
|
by: Matt |
last post by:
I have 2 questions:
1. strlen returns an unsigned (size_t) quantity. Why is an unsigned
value more approprate than a signed value? Why is unsighned value less
appropriate?
2. Would there...
|
by: Alfonso Morra |
last post by:
Hi,
I have the ff data types :
typedef enum {
VAL_LONG ,
VAL_DOUBLE ,
VAL_STRING ,
VAL_DATASET
}ValueTypeEnum ;
|
by: vansky |
last post by:
Dear all,
im porting a WIN APP to LINUX, and face some problems that r hard for
me to solve, could u dear guys give me some hands?
i'd like to reimplement the following funcs or replace them...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
| |