Need some Bits/Bytes and Types information
-
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
-
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
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 callGetWindowText
this wayTCHAR 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] -
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
*((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. <
-
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
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
-
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
-
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.
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. <
-
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. <