newbie question -- really basic question
What is size_t
newbie question -- really basic question
What is size_t
size_t "is the unsigned integer type of the result of the sizeof operator"
7. It is easier to write an incorrect program than understand a correct one.
40. There are two ways to write error-free programs; only the third one works.*
In the simplist terms an unsigned long integer
Drat beaten, thats what I get for looking at my includes directory
^^usually. It is there for portability. but in simple terms they are both right
It is there because (I assume) it is different sizes on different platforms but if you're just using size_t rather than int or long or short or whatever then it won't really matter how it was chosen to be implemented there.
can i then replace my int with size_t
is
size_t "is the unsigned integer type of the result of the sizeof operator"
the same as
size_t "is the unsigned integer value of the result of the sizeof operator"
say a function returned a sizeof command exIt would be more portable to do thisCode:int size_of(char *string){ return (sizeof(string)); }It doesn't have to be a function it could be a variable like the one that would recieve the return of the latter functionCode:size_t size_of(char *string){ return (sizeof(string)); }
[edit] man at functions like malloc or qsort. say you want to malloc a variable amountSee the difference. And of course error checking has been left out for example's sake.Code:int var; int *ptr; get var from user ptr=malloc(var*sizeof(int)); or size_t var; int *ptr; get var from user ptr=malloc(var*sizeof(int));
Last edited by linuxdude; 05-25-2004 at 11:22 AM.
Bad example. Your function will return the size of the pointer. You can't use sizeof on a pointer and expect it to give you the size of the string, unless the pointer is the name of an array created in that same scope. This was just beatend to death recently in another thread.
Quzah.
Hope is the first step on the road to disappointment.
whoops wasn't really paying attention. I just read that thread too. Sorry, I hope he gets my point though with the malloc example.
First all you can safely assume for all compilers is that size_t is an unsigned integer type (it might be unsigned int, unsigned long, ....). If you have a c99 compiler, the constant SIZE_MAX defined in stdint.h should give you the maximum possible value that size_t variable can have. For a proper C99 compiler SIZE_MAX should be at least 65535Originally Posted by studentc
Secondly, no you cannot replace int with size_t in general because int can take both negative and positive values whereas size_t cannot.
Last edited by pinko_liberal; 05-28-2004 at 07:59 PM. Reason: typo
The one who says it cannot be done should never interrupt the one who is doing it.
size_t is defined as an unsigned integer. Not necessarely 'long'. Actually, on my ol'good Borland C, it's an unsigned int.Originally Posted by Thantos
Of course no. A type is certainely not a value.Originally Posted by studentc
To answer your first question, size_t is useful to define a variable holding a size, an length, a number of elements in an array or an index.
I personnally use it massively in these occasions.
Because the size of an object could be too big for an int.Originally Posted by C+++C_forever
For example, on my platform (Borland C++ 3.1), the maximum value for an int is
and size_t is defined as<limits.h>
Code:#define INT_MAX 0x7FFF
Obviously, on this platform, an unsigned int can be bigger than an int.<stddef.h>
Code:typedef unsigned size_t;
The use of size_t is considered good practice when portability is important.
1) Welcome to the boards Emmanuel Delaha
2) There is no need to reiterate what others have already said
3) Learn to use edit.
4) The words "Borland" and "good" do not belong in the same sentance
Can you be more specific ? Please give an example of 'reiteration' of mine.Originally Posted by Thantos
Can you be more specific ? Please give an example where I should have used it better.3) Learn to use edit.
Unless you have more solid words to build your opinion, I stick to mine that is different.4) The words "Borland" and "good" do not belong in the same sentance
Emmanuel Delahaye
"C is a sharp tool"