Click here to Skip to main content
15,918,007 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
This line of code in the program (the client's code):

MIDL
BYTE* bp = (BYTE*)(&dataLength) + sizeof(dataLength) - cbLeftToReceive;


should it not be:

MIDL
BYTE* bp = (BYTE*)(&dataLength + sizeof(dataLength) - cbLeftToReceive);


?? i.e. i have moved a ')'.

my email address is 'A SECRET'
Thanks!!
Posted
Updated 19-Aug-10 4:55am
v2
Comments
Dalek Dave 19-Aug-10 10:56am    
Removed Address : Not required, and leaves you open to bot attacks.

Depends on what type dataLength is. The original code will work whatever the type of dataLength is - moving the closing parenthesis will give you an incorrect result if the size of *&dataLength is not 1.
 
Share this answer
 
No!

The first one is correct, because it is adding an offset (in bytes) to the starting address. This is the type of thing you would do when receiving the data from a byte stream.

The second one is adding an offset in dataLength type sizes, which would be an error in this case.

Example:
int dataLength; // assume 32-bits (4-bytes)
size_t cbLeftToReceive = 2; // count bytes left

// OK: Equivalent to (BYTE*)(&dataLength)[2]
BYTE* bp = (BYTE*)(&dataLength) + sizeof(dataLength) - cbLeftToReceive;

// ERROR: Equivalent to (BYTE*)(&dataLength)[8]<br>
BYTE* bp = (BYTE*)(&dataLength + sizeof(dataLength) - cbLeftToReceive);</br>
 
Share this answer
 
v2
Comments
Peter_in_2780 22-Aug-10 19:46pm    
Reason for my vote of 5
Nailed the sizeof() issue in pointer arithmetic.
er...Yes.

Keep it all in one chunk.
 
Share this answer
 
Comments
Niklas L 20-Aug-10 4:54am    
er...No
The both forms are intuitive incorrect,
your origin lies at the address of a variable, not at a data position... :)

Just post the whole function here :-D
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900