Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Need some Bits/Bytes and Types information

Need some Bits/Bytes and Types information

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestiondatabasedata-structuresperformance
7 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    gabbana
    wrote on last edited by
    #1

    Hy, I am still having problem handling some data types and the data itself in memory and so on. For example: For getting the text of an edit control (winapi) requires to create a buffer with enaugh space. I did (unicode) for example: length is from the type of WORD in the example, but i used length as an integer. TCHAR *buf = new TCHAR[length+1]; So now it says the FIRST WORD needs to have the length of the string in the edit field, which I retrieved before. The example shows: *((LPWORD)buf) = length; However, when I use my way: *(buf) = length it ends in the same result. So, my question is whats so special in the way how msdn do it? ----- And then, did i understand the line right ? Here is my explanation: In memory we have allocated space for the variable buf. The length is the length of TCHAR * mylength(the variable) But in memory it is handled as "we need xx bytes" right? Then *(buf) points to the first byte (in array it is index 0) But because we need the first word, we convert the normal pointer (i think normal is a void pointer) to an pointer of a WORD. So now the index 0 for examples is exactly 1 WORD. Then we write the length variable which is also a WORD to the location. Is this right or do I misunderstand something? ---- And why is my way *(buf) = length also working? I hope everybody understand whats my problem. Thank you for help. bye, gabbana

    CPalliniC C G 3 Replies Last reply
    0
    • G gabbana

      Hy, I am still having problem handling some data types and the data itself in memory and so on. For example: For getting the text of an edit control (winapi) requires to create a buffer with enaugh space. I did (unicode) for example: length is from the type of WORD in the example, but i used length as an integer. TCHAR *buf = new TCHAR[length+1]; So now it says the FIRST WORD needs to have the length of the string in the edit field, which I retrieved before. The example shows: *((LPWORD)buf) = length; However, when I use my way: *(buf) = length it ends in the same result. So, my question is whats so special in the way how msdn do it? ----- And then, did i understand the line right ? Here is my explanation: In memory we have allocated space for the variable buf. The length is the length of TCHAR * mylength(the variable) But in memory it is handled as "we need xx bytes" right? Then *(buf) points to the first byte (in array it is index 0) But because we need the first word, we convert the normal pointer (i think normal is a void pointer) to an pointer of a WORD. So now the index 0 for examples is exactly 1 WORD. Then we write the length variable which is also a WORD to the location. Is this right or do I misunderstand something? ---- And why is my way *(buf) = length also working? I hope everybody understand whats my problem. Thank you for help. bye, gabbana

      CPalliniC Offline
      CPalliniC Offline
      CPallini
      wrote on last edited by
      #2

      gabbana wrote:

      I hope everybody understand whats my problem. Thank you for help.

      For instance I cannot understand your problem. To get the text of an edit box, using Windows API, you may to call GetWindowText this way

      TCHAR szBuf[0x100];
      if (! GetWindowText(hWnd, szBuf, sizeof(szBuf)/sizeof(szBuf[0])))
      {// handle error
      }

      :)

      If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
      This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
      [My articles]

      In testa che avete, signor di Ceprano?

      1 Reply Last reply
      0
      • G gabbana

        Hy, I am still having problem handling some data types and the data itself in memory and so on. For example: For getting the text of an edit control (winapi) requires to create a buffer with enaugh space. I did (unicode) for example: length is from the type of WORD in the example, but i used length as an integer. TCHAR *buf = new TCHAR[length+1]; So now it says the FIRST WORD needs to have the length of the string in the edit field, which I retrieved before. The example shows: *((LPWORD)buf) = length; However, when I use my way: *(buf) = length it ends in the same result. So, my question is whats so special in the way how msdn do it? ----- And then, did i understand the line right ? Here is my explanation: In memory we have allocated space for the variable buf. The length is the length of TCHAR * mylength(the variable) But in memory it is handled as "we need xx bytes" right? Then *(buf) points to the first byte (in array it is index 0) But because we need the first word, we convert the normal pointer (i think normal is a void pointer) to an pointer of a WORD. So now the index 0 for examples is exactly 1 WORD. Then we write the length variable which is also a WORD to the location. Is this right or do I misunderstand something? ---- And why is my way *(buf) = length also working? I hope everybody understand whats my problem. Thank you for help. bye, gabbana

        C Offline
        C Offline
        Code o mat
        wrote on last edited by
        #3

        *((LPWORD)buf) = length; - this will copy sizeof(WORD) bytes into the first sizeof(WORD) bytes of your buffer *(buf) = length - this will convert your integer (length) to a TCHAR and put that into the first TCHAR pointed at by buf. So let's assume WORD is 2 bytes and TCHAR is also 2 bytes (unicode), in this case the 2 versions should produce the same result, but if you compile wihout unicode, TCHAR being 1 byte then in the second case, only 1 bytes will be changed in your buffer while the 2nd byte is left unchanged, you would probably get a warning from your compiler too ('conversion from int to char, possible loss of data' or similar). On a sidenote, could you show me where you read this:

        gabbana wrote:

        So now it says the FIRST WORD needs to have the length of the string in the edit field, which I retrieved before.

        please?

        > The problem with computers is that they do what you tell them to do and not what you want them to do. <

        1 Reply Last reply
        0
        • G gabbana

          Hy, I am still having problem handling some data types and the data itself in memory and so on. For example: For getting the text of an edit control (winapi) requires to create a buffer with enaugh space. I did (unicode) for example: length is from the type of WORD in the example, but i used length as an integer. TCHAR *buf = new TCHAR[length+1]; So now it says the FIRST WORD needs to have the length of the string in the edit field, which I retrieved before. The example shows: *((LPWORD)buf) = length; However, when I use my way: *(buf) = length it ends in the same result. So, my question is whats so special in the way how msdn do it? ----- And then, did i understand the line right ? Here is my explanation: In memory we have allocated space for the variable buf. The length is the length of TCHAR * mylength(the variable) But in memory it is handled as "we need xx bytes" right? Then *(buf) points to the first byte (in array it is index 0) But because we need the first word, we convert the normal pointer (i think normal is a void pointer) to an pointer of a WORD. So now the index 0 for examples is exactly 1 WORD. Then we write the length variable which is also a WORD to the location. Is this right or do I misunderstand something? ---- And why is my way *(buf) = length also working? I hope everybody understand whats my problem. Thank you for help. bye, gabbana

          G Offline
          G Offline
          gabbana
          wrote on last edited by
          #4

          Okay, now I think i found some information. Lets start with Bits and Bytes: A BIT can be 1 or 0. So we have 2 options A BYTE contains 8 Bits. So we have 2^8 possible option (ASCII/ANSI) So we have all the 256 chars. A WORD contains 16 Bits (2BYTE) A WCHAR (wide char) contains also 16bits (2BYTE) -> chinese languag and so on need 2 Bytes for one char. A Pointer (I said its normally a void point but that was wrong) is normally a pointer to a specific datatype, for example WCHAR *buf -> means we have a Pointer which points to ONE WCHAR somewhere in memory (as long as we allocate space) So now we allocate space: *buf = new WCHAR[16]; This means we allocate 16 bits per char, so we have allocated 2*16 bytes = 32 bytes which means 2^32 bits. So if we use the example with edit field, we get an integer (the length) and convert it to a WORD. BECAUSE the edit field must return something >0 we CAN convert it to an unsigned integer, a WORD right? -> Now why do we need the following? *((LPWORD)buf) = length And why does *(buf) = (WORD)length also works ? So we know that WCHAR contains 2Bytes. A WORD also contains 2Bytes. So the point normaly shows to the firt wchar which is also a WORD in this example. So we can easily move the length to this location. Right ? But why the first way ? So it could be that if we does not using unicode, the pinte would only show to the first ONE byte in the memory. And then we need to convert it it first to an LPWORD to be sure the pointer shows to the FIRST 2Bytes, a WORD in memory. And so it works. I hope its not too long and someone can confirm my explanation. Thank you very much. bye, gabbana

          G 1 Reply Last reply
          0
          • G gabbana

            Okay, now I think i found some information. Lets start with Bits and Bytes: A BIT can be 1 or 0. So we have 2 options A BYTE contains 8 Bits. So we have 2^8 possible option (ASCII/ANSI) So we have all the 256 chars. A WORD contains 16 Bits (2BYTE) A WCHAR (wide char) contains also 16bits (2BYTE) -> chinese languag and so on need 2 Bytes for one char. A Pointer (I said its normally a void point but that was wrong) is normally a pointer to a specific datatype, for example WCHAR *buf -> means we have a Pointer which points to ONE WCHAR somewhere in memory (as long as we allocate space) So now we allocate space: *buf = new WCHAR[16]; This means we allocate 16 bits per char, so we have allocated 2*16 bytes = 32 bytes which means 2^32 bits. So if we use the example with edit field, we get an integer (the length) and convert it to a WORD. BECAUSE the edit field must return something >0 we CAN convert it to an unsigned integer, a WORD right? -> Now why do we need the following? *((LPWORD)buf) = length And why does *(buf) = (WORD)length also works ? So we know that WCHAR contains 2Bytes. A WORD also contains 2Bytes. So the point normaly shows to the firt wchar which is also a WORD in this example. So we can easily move the length to this location. Right ? But why the first way ? So it could be that if we does not using unicode, the pinte would only show to the first ONE byte in the memory. And then we need to convert it it first to an LPWORD to be sure the pointer shows to the FIRST 2Bytes, a WORD in memory. And so it works. I hope its not too long and someone can confirm my explanation. Thank you very much. bye, gabbana

            G Offline
            G Offline
            gabbana
            wrote on last edited by
            #5

            Oh I see the first two answers now. Thank you very much but it would be great if you also confirm what I say in the last post. I think the unicode stuff I mentioned is right, juhuu.

            C 1 Reply Last reply
            0
            • G gabbana

              Oh I see the first two answers now. Thank you very much but it would be great if you also confirm what I say in the last post. I think the unicode stuff I mentioned is right, juhuu.

              C Offline
              C Offline
              Code o mat
              wrote on last edited by
              #6

              As far as i can tell, you understood it quite well. Just to clear things up, a pointer alone always points at 1 byte in memory, giving it a type is, how to say, just a more clear, logical way of handling things. Hmm, hard to explain what i mean, i try to give an example: WORD *WordPointer = (WORD *)malloc(sizeof(char)); So, althorough WordPointer is assumed to be pointing at a WORD being 2 bytes, it still points at the 1 allocated byte. So if then you do *WordPointer = 3; It will write 2 bytes in memory but we only allocated 1 byte, this can result in 2 (or maybe more) things: -You get an access violation because you are trying to write a memory location (the second byte) that does not belong to your process. This is a better result because it is much easier to track down and correct. -The second byte of the word actually belongs to your process but is part of something else, for example an integer's first byte you use somewhere else, then you owerwrite this changing your integer, this problem is much harder to track down and correct because it doesn't result in an error right away but maybe later when you try to use your integer and see that it has some bogous value and you have no idea where it came. </smartass_mode> :) I hope this is clear, don't want to lecture you, just thought this info might be useful for you. :)

              > The problem with computers is that they do what you tell them to do and not what you want them to do. <

              G 1 Reply Last reply
              0
              • C Code o mat

                As far as i can tell, you understood it quite well. Just to clear things up, a pointer alone always points at 1 byte in memory, giving it a type is, how to say, just a more clear, logical way of handling things. Hmm, hard to explain what i mean, i try to give an example: WORD *WordPointer = (WORD *)malloc(sizeof(char)); So, althorough WordPointer is assumed to be pointing at a WORD being 2 bytes, it still points at the 1 allocated byte. So if then you do *WordPointer = 3; It will write 2 bytes in memory but we only allocated 1 byte, this can result in 2 (or maybe more) things: -You get an access violation because you are trying to write a memory location (the second byte) that does not belong to your process. This is a better result because it is much easier to track down and correct. -The second byte of the word actually belongs to your process but is part of something else, for example an integer's first byte you use somewhere else, then you owerwrite this changing your integer, this problem is much harder to track down and correct because it doesn't result in an error right away but maybe later when you try to use your integer and see that it has some bogous value and you have no idea where it came. </smartass_mode> :) I hope this is clear, don't want to lecture you, just thought this info might be useful for you. :)

                > The problem with computers is that they do what you tell them to do and not what you want them to do. <

                G Offline
                G Offline
                gabbana
                wrote on last edited by
                #7

                Ahh okay great. Thank you very much. I think I will try a bit and see what the debugger shows to become a better "feeling" of how something of that happens so that I know "Aahh, I have an error with the memory allocation".

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups