what is size_t?

This is a discussion on what is size_t? within the C Programming forums, part of the General Programming Boards category; Hi, I was looking at this old forum thread: http://cboard.cprogramming.com/archi...p/t-63079.html and was a bit confused as to what size_t (size_t ...

  1. #1
    Embedded in C...
    Join Date
    Sep 2008
    Location
    Basingstoke, Hampshire
    Posts
    83

    what is size_t?

    Hi,

    I was looking at this old forum thread:

    http://cboard.cprogramming.com/archi...p/t-63079.html

    and was a bit confused as to what size_t (size_t serialize) is - is it a standard typedef in C (as I have seen it mentioned elsewhere here, or a placeholder for me to place my own type in?

    edit

    just found it - its a replacement for int in string and memory copy functions

    Just trying to understand this code, would someone be able to clarify some lines I don't understand please..


    Code:
    struct dumb by[sizeof member] =
    defining a struct of type dumb with name by - what does the sizeof member do, dynamically size the array?

    Code:
    memcpy(&dst[i], &object->broiled, sizeof object->broiled);
       i += sizeof object->broiled;
    I'm a bit confused as to what the function of i is here - I can see that the memcpy transfers the contents of each member to consecutive array indexes

    Code:
    void showbytes(const void *object, size_t size)
    {
       const unsigned char *byte;
       for ( byte = object; size--; ++byte )
    whats the purpose of size here, and why is it decremented?

    --dave
    Last edited by droseman; 01-27-2009 at 05:48 AM.

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Embedded in C...
    Join Date
    Sep 2008
    Location
    Basingstoke, Hampshire
    Posts
    83
    thanks for the link, it was very descriptive, that was a site I hadn't come accross before.

    --dave

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,033
    Code:
    struct dumb by[sizeof member]
    sizeof returns the actual size of a variable or type. For example,
    Code:
    struct dumb by[sizeof(int)]
    Would define as many structs of type dumb, as the number of bytes an int takes.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,058
    Quote Originally Posted by droseman View Post
    Just trying to understand this code, would someone be able to clarify some lines I don't understand please...
    Code:
    memcpy(&dst[i], &object->broiled, sizeof object->broiled);
       i += sizeof object->broiled;
    I'm a bit confused as to what the function of i is here - I can see that the memcpy transfers the contents of each member to consecutive array indexes
    Hard to say w/o looking at the declaration of the struct object and array dst[] as i is being incremented by sizeof broiled which is an element of the struct object.
    Quote Originally Posted by droseman View Post
    Code:
    void showbytes(const void *object, size_t size)
    {
       const unsigned char *byte;
       for ( byte = object; size--; ++byte )
    whats the purpose of size here, and why is it decremented?

    --dave
    Again hard to say without its proper context. Looks like it is displaying bytes stored in variable object though decrementing size at each step until it becomes zero for a terminal condition may not be a good idea. Perhaps byte <= size maybe a better choice for the terminal condition but then again it is probably best reviewed in context.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21