Thread: size_t

  1. #1
    Registered User
    Join Date
    May 2004
    Posts
    70

    size_t

    newbie question -- really basic question
    What is size_t

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    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.*

  3. #3
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    In the simplist terms an unsigned long integer

    Drat beaten, thats what I get for looking at my includes directory

  4. #4
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    ^^usually. It is there for portability. but in simple terms they are both right

  5. #5
    Registered User
    Join Date
    May 2004
    Posts
    23
    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.

  6. #6
    Registered User
    Join Date
    May 2004
    Posts
    70
    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"

  7. #7
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    say a function returned a sizeof command ex
    Code:
    int size_of(char *string){
         return (sizeof(string));
    }
    It would be more portable to do this
    Code:
    size_t 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 function
    [edit] man at functions like malloc or qsort. say you want to malloc a variable amount
    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));
    See the difference. And of course error checking has been left out for example's sake.
    Last edited by linuxdude; 05-25-2004 at 11:22 AM.

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.

  9. #9
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    whoops wasn't really paying attention. I just read that thread too. Sorry, I hope he gets my point though with the malloc example.

  10. #10
    Registered User pinko_liberal's Avatar
    Join Date
    Oct 2001
    Posts
    284
    Quote Originally Posted by studentc
    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"
    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 65535

    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.

  11. #11
    Quote Originally Posted by Thantos
    In the simplist terms an unsigned long integer
    size_t is defined as an unsigned integer. Not necessarely 'long'. Actually, on my ol'good Borland C, it's an unsigned int.

  12. #12
    Quote Originally Posted by studentc
    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"
    Of course no. A type is certainely not a value.

    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.

  13. #13
    Quote Originally Posted by C+++C_forever
    whi it would be more portable to do this
    Code:
    size_t size_of(char *string){
    return (sizeof(string));
    }
    instead of returning an int or a long? i am still not convinced to why i should use it. someone to convince me?
    thnaks
    Because the size of an object could be too big for an int.

    For example, on my platform (Borland C++ 3.1), the maximum value for an int is

    <limits.h>
    Code:
    #define INT_MAX             0x7FFF
    and size_t is defined as

    <stddef.h>
    Code:
    typedef unsigned size_t;
    Obviously, on this platform, an unsigned int can be bigger than an int.

    The use of size_t is considered good practice when portability is important.

  14. #14
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    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

  15. #15
    Quote Originally Posted by Thantos
    2) There is no need to reiterate what others have already said
    Can you be more specific ? Please give an example of 'reiteration' of mine.
    3) Learn to use edit.
    Can you be more specific ? Please give an example where I should have used it better.
    4) The words "Borland" and "good" do not belong in the same sentance
    Unless you have more solid words to build your opinion, I stick to mine that is different.
    Emmanuel Delahaye

    "C is a sharp tool"

Popular pages Recent additions subscribe to a feed