• Welcome to Valhalla Legends Archive.
 

Very strange. (C++)

Started by Win32, June 23, 2007, 03:07 AM

Previous topic - Next topic

Win32

Unless I'm blind, can someone point out the problem with the following code?


static Client* pTargetClient = Clients;
static Dword   dwNewQueueSize;



OutputDebugStringA("\n\nQueuing Client message...\n");


//
// Find the Client.
//
while(pTargetClient != (Clients + (MAX_CLIENTS * sizeof(Clients))))
{
//
// Have we found the Client?
//
if(pTargetClient->dwAddress == dwAddress && pTargetClient->wPort == wPort)
{
OutputDebugStringA("[ Found Client ]\n");


//
// Continue to queuing the message.
//
goto QUEUE_MESSAGE;
}

pTargetClient += sizeof(Client);
}


The real problem is 'pTargetClient' is not being added the correct value (40d), it's being added 1,600. The size of the Client struct is 40 bytes, no doubt about it.

The disassembly is:


pTargetClient += sizeof(Client);
00411A1D  mov         eax,dword ptr [pTargetClient (417004h)]
00411A22  add         eax,640h
00411A27  mov         dword ptr [pTargetClient (417004h)],eax


This doesn't make much sense, sizeof(Client) returns 40 in all other instances.


Much appreciated if anyone could shed some light on this.


Thanks,

Matt.


EDIT:
Well it appears I've figured it out. The operand for Struct*+= is multiplied by the weight of the structure. This never used to happen before, might be because I'm not using VC++ anymore. Still, don't understand why such a blatently stupid assumption is made.

K

Quote from: Win32 on June 23, 2007, 03:07 AM
EDIT:
Well it appears I've figured it out. The operand for Struct*+= is multiplied by the weight of the structure. This never used to happen before, might be because I'm not using VC++ anymore. Still, don't understand why such a blatently stupid assumption is made.

In that case, I would blame whatever version of Visual C++ you were using, because this is pretty well-known feature of the both the C and C++ standards. 

MyndFyre

Quote from: K on June 23, 2007, 11:20 AM
In that case, I would blame whatever version of Visual C++ you were using, because this is pretty well-known feature of the both the C and C++ standards. 
I don't think that it's a problem....  It's working according to the standard.

Quote from: Win32 on June 23, 2007, 03:07 AM
EDIT:
Well it appears I've figured it out. The operand for Struct*+= is multiplied by the weight of the structure. This never used to happen before, might be because I'm not using VC++ anymore. Still, don't understand why such a blatently stupid assumption is made.
I think you don't understand how pointer arithmetic works.  Incrementing a pointer by one means that you should be pointing to the next object following a pointer.  It's so that you can do something like so:

int numbers[10];
int* current = &numbers[0];
for (int i = 0; i < 10; i++)
{
  *(current++) = i;
}

This code is equivalent to:

for (int i = 0; i < 10; i++)
{
  numbers[i] = i;
}

If incrementing a pointer by one meant incrementing it by a byte, then you'd really fuck up your number list doing this, wouldn't you?
QuoteEvery generation of humans believed it had all the answers it needed, except for a few mysteries they assumed would be solved at any moment. And they all believed their ancestors were simplistic and deluded. What are the odds that you are the first generation of humans who will understand reality?

After 3 years, it's on the horizon.  The new JinxBot, and BN#, the managed Battle.net Client library.

Quote from: chyea on January 16, 2009, 05:05 PM
You've just located global warming.

Win32

Guess I've been working with assembly too long, forgot that C++ is a little more -logical-. My bad, bit of a pointless post.

iago

Yeah, you'll run into this a lot when converting assembly to C++. It's something that's bitten me in the ass many, many times.
This'll make an interesting test for broken AV:
QuoteX5O!P%@AP[4\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*