Click here to Skip to main content
15,867,568 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
typedef struct _tagStrRec {
	short		nSym;					// string numeric ID
	short		nSec;					// area numeric ID
	short		nLen;					// string len
	long		lSeek;				// seek position within the file
} PSTRTBL, FAR* LPPSTRTBL;



PSTRTBL strTbl;
strTbl.nSym = 20;
strTbl.nSec = 50;

LPPSTRTBL pStrTbl = &strTbl;
pStrTbl->lSeek = 0;
pStrTbl->nLen = 20;
pStrTbl->nSec = 10;
pStrTbl->nSym = 20;

PSTRTBL strTbl1;
strTbl.nSym = 100;
strTbl.nSec = 800;

LPPSTRTBL pStrTbl1 = &strTbl1;
pStrTbl1->lSeek = 30;
pStrTbl1->nLen = 50;
pStrTbl1->nSec = 60;
pStrTbl1->nSym = 80;

long lInd = HandleToLong(pStrTbl) - HandleToLong(pStrTbl1);


What I have tried:

I'm converting my application from 32Bit to 64Bit in 32 bit the value of lInd = 32 but in 64 bit it is coming -80.

long lInd = (long)(pStrTbl) - (long)(pStrTbl1); //32 bit
long lInd = HandleToLong(pStrTbl) - HandleToLong(pStrTbl1); //64bit

Could you please let me know the issue.
Posted
Updated 25-Jun-20 5:02am
Comments
Member 13798855 25-Jun-20 6:10am    
lInd for 32 bit and 64 bit is different , iam not sure why ?

Quote:
Could you please let me know the issue.

May be the issue is in your expectations about how and where structures get allocated in memory.
No doubt that there are differences between 32 bits and 64 bits.
C++
long lInd = (long)(pStrTbl) - (long)(pStrTbl1); //32 bit
long lInd = HandleToLong(pStrTbl) - HandleToLong(pStrTbl1); //64bit

print values of pointers used in your calculation, and see what is what.
 
Share this answer
 
I get a different answer but it is not clear exactly what you are trying to do. You have two pre-allocated structures strTbl and strTbl1. You set some values into those structures. You then create pointers to the two structures and set some different values into them. You then calculate the difference between their addresses.

You need to explain what you mean by "the wrong value". i have checked and all values are correct.
 
Share this answer
 
v2
Comments
Member 13798855 25-Jun-20 6:08am    
that is actually a sample code which i tried to do ..the value of lIndx is different for 32 bit and 64 bi. Could you please let me know the issue.
Richard MacCutchan 25-Jun-20 6:36am    
Well it is sample code that serves no useful purpose, and does not prove anything. When you create a variable whether on the stack or the heap, it is allocated space in whatever part of the memory is free. So the addresses of your structures could be any random values which have no relationship to each other. If you want a true comparison between 32 and 64 bit then do something like this:
PSTRTBL strTbls[2];
long diff = &strTbls[1] - &strTbls[0];

But the result will not be what you think.
Member 13798855 25-Jun-20 6:09am    
long lInd is different for both configurations
HandleToLong takes a Handle and returns a long value the handle references. You are passing a memory address (which may be on the stack, maybe not - depends on your code) and expecting ... I don't know what.

A Handle may be a pointer - it's declared as such in the .h file - but that doesn't mean that a pointer is a handle!

Even if you subtracted two pointers, the result isn't useful, or even consistent between compilers. There is no guaranteed that the memory is allocated "top down" as your app is compiled; that's an internal feature of the compiler and shouldn't be relied upon to give you any indication of the "spacing" between data in physical memory.
Subtracting memory addresses is like adding telephone numbers: you don't get anything particularly useful!

And remember: in 64 bit apps, pointers are 64 bit values. Treating them as a handle and trying to convert them to 32 bit and expecting it to work exactly the same as before is not really going to work...
 
Share this answer
 
Comments
Member 13798855 25-Jun-20 6:26am    
thanks
Computing the difference between two instances (here pointer and addresses) isnt uswful because it depend how and when the are allocated.

Comparing the dats is done in this manner:
C++
long seeked = pStrTbl1->lSeek - pStrTbl->lSeek;
 
Share this answer
 
C++
long
is 32 bit number in both 32 bit and 64 bit architectures. Pointer notation is 64 bit long on 64 bit system. Your answers should be totally random due to
C++
long
overflow as far as your code shows. You truncating 64 bit address by casting it to long. The answer is undefined behavior.

And BTW your code seem to target 16 bit system by using FAR* which is unnecessary on 64 bit
 
Share this answer
 
v2
You should be using a type of INT_PTR for lInd. It is automatically adjusted for the word size you use. The long type is still 32-bits in the 64-bit world so it is not appropriate for dealing with 64-bit pointers.

Also, since Win32 was first released there is no need or value to have anything be FAR. It has meant absolutely nothing since then so do yourself a favor and get rid of it. While you're at it, you should lose the L prefix on pointers also because there is no distinction for long pointers either since they are essentially the same thing as FAR.
 
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