Thread: Having a fixed number of bytes

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    54

    Having a fixed number of bytes

    Hi, how do I know how many bytes I've initialised to a datatype?

    ie:
    int x; <--Could this mean I've reserved 1 byte?

    How do I initialize a fix length of bytes?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Use sizeof() perhaps?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Hi, how do I know how many bytes I've initialised to a datatype?

    ie:
    int x; <--Could this mean I've reserved 1 byte?
    See Salem's answer.
    Code:
    int x;
    printf("&#37;d\n", sizeof(x));
    BTW, instead of "how many bytes [have I] initialised to a datatype", you might more properly put it as "how many bytes were reserved [on the stack] when I declared this variable".

    How do I initialize a fix length of bytes?
    I have no idea why you would want to do this, but an ordinary local array of characters should require CHAR_BIT/8*elements bytes. CHAR_BIT is usually 8, so
    Code:
    char dummy[16];
    should usually allocate 16 bytes. (This won't work in structures if that's what you're thinking.)
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    In C/C++, a byte is defined as whatever the size of a (signed or unsigned) char is, so your definition always allocates 16 bytes. The catch, of course, is that a byte (in C/C++) isn't always 8 bits, but for many purposes, such as calling malloc(), it doesn't matter.

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by robatino View Post
    In C/C++, a byte is defined as whatever the size of a (signed or unsigned) char is, so your definition always allocates 16 bytes. The catch, of course, is that a byte (in C/C++) isn't always 8 bits, but for many purposes, such as calling malloc(), it doesn't matter.
    sizeof char is 1, not 1 byte, just 1...
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >sizeof char is 1, not 1 byte, just 1...
    sizeof returns the size in bytes.

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    No it doesn't. It returns the size in chars. [edit] <-- this is wrong [/edit]

    [edit]
    Hang on...

    6.5.3.4 The sizeof operator
    Constraints
    1 The sizeof operator shall not be applied to an expression that has function type or an
    incomplete type, to the parenthesized name of such a type, or to an expression that
    designates a bit-field member.
    Semantics
    2 The sizeof operator yields the size (in bytes) of its operand, which may be an
    expression or the parenthesized name of a type. The size is determined from the type of
    the operand. The result is an integer. If the type of the operand is a variable length array
    type, the operand is evaluated; otherwise, the operand is not evaluated and the result is an
    integer constant.
    Give me a second, I'll try to find my point of confusion. I think there's a reference book I have around here some place that says it's "size in chars", which was fresh in my mind, which is why I thought to double check it once more.
    [/edit]

    [edit2]
    I may be confusing another thread. I'm getting too old I guess.
    [/edit2]


    Quzah.
    Last edited by quzah; 03-31-2007 at 11:02 PM.
    Hope is the first step on the road to disappointment.

  8. #8
    Captain - Lover of the C
    Join Date
    May 2005
    Posts
    341
    The MSVC 2005 documentation says that (in C) sizeof returns the number of bytes and (in C++) sizeof returns the size with respect to char.
    Don't quote me on that... ...seriously

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Maybe it's something like that I was thinking of. I'll blame old age and no cup of morning coffee before posting. Here is another FAQ type read, but it relates to C++.

    If I had been awake enough, I wouldn't have edited that, taken advantage of the upcoming date, and tried to pass it off as an elaborate ruse.


    Quzah.
    Last edited by quzah; 03-31-2007 at 11:39 PM.
    Hope is the first step on the road to disappointment.

  10. #10
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Quote Originally Posted by quzah View Post
    If I had been awake enough, I wouldn't have edited that, taken advantage of the upcoming date, and tried to pass it off as an elaborate ruse.
    Well at least you haven't lost your wit.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program giving errors
    By andy bee in forum C Programming
    Replies: 5
    Last Post: 08-11-2010, 10:38 PM
  2. Replies: 6
    Last Post: 02-19-2009, 07:19 PM
  3. Need help getting program print out the digit in words
    By cosmiccomputing in forum C Programming
    Replies: 26
    Last Post: 04-24-2008, 08:28 AM
  4. Random Number problem in number guessing game...
    By -leech- in forum Windows Programming
    Replies: 8
    Last Post: 01-15-2002, 05:00 PM
  5. Reading Certain number of bytes from a file
    By (TNT) in forum Windows Programming
    Replies: 6
    Last Post: 01-14-2002, 08:35 PM