Valhalla Legends Forums Archive | C/C++ Programming | Very strange. (C++)

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

[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);
}
[/code]

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:

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

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.
June 23, 2007, 8:07 AM
K
[quote author=Win32 link=topic=16814.msg170346#msg170346 date=1182586023]
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.
[/quote]

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. 
June 23, 2007, 4:20 PM
Myndfyr
[quote author=K link=topic=16814.msg170352#msg170352 date=1182615622]
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. 
[/quote]
I don't think that it's a problem....  It's working according to the standard.

[quote author=Win32 link=topic=16814.msg170346#msg170346 date=1182586023]
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.
[/quote]
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:
[code]
int numbers[10];
int* current = &numbers[0];
for (int i = 0; i < 10; i++)
{
  *(current++) = i;
}
[/code]
This code is equivalent to:
[code]
for (int i = 0; i < 10; i++)
{
  numbers[i] = i;
}
[/code]
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?
June 23, 2007, 5:47 PM
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.
June 23, 2007, 6:20 PM
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.
June 23, 2007, 6:31 PM

Search