BYTE, char data corruption

This is a discussion on BYTE, char data corruption within the C++ Programming forums, part of the General Programming Boards category; Originally Posted by dwks It does work? Oh, you mean doesn't. It does work, because all I want copied is ...

  1. #31
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    Quote Originally Posted by dwks
    It does work? Oh, you mean doesn't. It does work, because all I want copied is the address.
    That was a typo that I later corrected. If all you want is the address, then great, it will work for you.

  2. #32
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    You are one of a very select few who do. I've never see that before in over 20 years.
    It's the only way to initialize (as Thantos says) a struct. It's the same as with arrays. Do you never do that either?

    This code sets all elements to zero:
    Code:
    int x[10] = {0};
    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.

  3. #33
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    If all you want is the address, then great, it will work for you.
    The address is what I would expect, since it's a pointer.
    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. #34
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    Quote Originally Posted by dwks
    It's the only way to initialize (as Thantos says) a struct. It's the same as with arrays. Do you never do that either?
    No. Never like that, except maybe in an array of structures when initializers contain no function calls.

    Quote Originally Posted by dwks
    This code sets all elements to zero:
    Code:
    int x[10] = {0};
    Do that all the time, but that's not the same as initializing a structure.

  5. #35
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    but that's not the same as initializing a structure.
    It's pretty much the same. They both initialize things. The only difference is that with structures, the type you're initializing can change.
    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.

  6. #36
    Registered User
    Join Date
    Dec 2001
    Posts
    206
    Quote Originally Posted by Thantos
    A BYTE is nothing more then a fancy unsigned char.
    In C++ language, yes. I use BYTE because it's what I'm actually dealing with: bytes. Less confusing to me, and whoever I share my code with.


    how does it corrupt it?
    Changing it to a char makes 50% of my bytes turn from 0x0000008E to 0xFFFFFF8E.

  7. #37
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Use & then to elininate the extra Fs.

    [edit]
    Changing it to a char
    How? With a cast?[/edit]
    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.

  8. #38
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    Quote Originally Posted by Denethor2000
    Changing it to a char makes 50% of my bytes turn from 0x0000008E to 0xFFFFFF8E.
    Impossible -- one byte cannot contain that big a number. The max falue of a byte is 0xff. you are probably seeing the output of a debugger, which is not necessarily the same as what is actually stored in the memory location.

  9. #39
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    In C++ language, yes. I use BYTE because it's what I'm actually dealing with: bytes. Less confusing to me, and whoever I share my code with
    Obviously its causing more problems then its helping.

    However you still haven't given up all the information. Or hell even a full code example. if all you need to do is write it out to a file use write() on a binary file

  10. #40
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Perhaps he should be "changing" it to an unsigned char.
    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.

  11. #41
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Impossible -- one byte cannot contain that big a number. The max falue of a byte is 0xff.
    What if you were dealing with 16 bit bytes? 32 bit bytes? They'd have a high value then that.

    BTW I'm guessing we all are assuming you are using the BYTE specified in the windows header. Can someone tell me if its a #define or typedef?

  12. #42
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    Quote Originally Posted by Thantos
    What if you were dealing with 16 bit bytes? 32 bit bytes? They'd have a high value then that.

    BTW I'm guessing we all are assuming you are using the BYTE specified in the windows header. Can someone tell me if its a #define or typedef?
    You have a point -- but have you ever heard of such an animal? Not to say there isn't one, but I doubt the OP is using it.

    Quote Originally Posted by windef.h
    typedef unsigned char BYTE;

  13. #43
    Registered User
    Join Date
    Dec 2001
    Posts
    206
    Bytes are 2 bits. Words are 4 bits. Dwords are 8 bits. Qwords are 16 bits.

    All bytes are 2bit, 0xFF. Stop asking if my bytes are 32bit or 16bit... They're neither, obviously.

    Anyway, using memcpy lets me copy my array into the struct, and it works correctly. Thanks for the help. <3

  14. #44
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    Quote Originally Posted by Denethor2000
    Bytes are 2 bits. Words are 4 bits. Dwords are 8 bits. Qwords are 16 bits.

    All bytes are 2bit, 0xFF. Stop asking if my bytes are 32bit or 16bit... They're neither, obviously.

    Anyway, using memcpy lets me copy my array into the struct, and it works correctly. Thanks for the help. <3
    No. on MS-Windows and *nix a byte is 8 bits. What you probably mean is a nibble, which is half the size of a byte.

    -- If you don't know what a byte is, don't bother reading. --

    I guess you should never have started this thread, since you don't know what a byte is either

  15. #45
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Ok you are seriously in need of some education then. Bytes are the lowest addressible memory element. By standard they are required to contain at least 8 bits. So whereever you got your size definations, burn it.

Page 3 of 4 FirstFirst 1234 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Obtaining source & destination IP,details of ICMP Header & each of field of it ???
    By cromologic in forum Networking/Device Communication
    Replies: 1
    Last Post: 04-29-2006, 02:49 PM
  2. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  3. Need help fixing bugs in data parsing program
    By daluu in forum C Programming
    Replies: 8
    Last Post: 03-27-2003, 05:02 PM
  4. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 04:26 PM

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