Click here to Skip to main content
15,923,051 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
QuestionLNK2019 Error!!! Pin
arbster13-Feb-07 9:27
arbster13-Feb-07 9:27 
AnswerRe: LNK2019 Error!!! Pin
Mark Salsbery3-Feb-07 10:17
Mark Salsbery3-Feb-07 10:17 
AnswerRe: LNK2019 Error!!! Pin
Hamid_RT3-Feb-07 23:53
Hamid_RT3-Feb-07 23:53 
News64-bit puzzle [modified] Pin
Karpov20073-Feb-07 8:14
Karpov20073-Feb-07 8:14 
GeneralRe: 64-bit puzzle Pin
Waldermort3-Feb-07 8:40
Waldermort3-Feb-07 8:40 
GeneralRe: 64-bit puzzle Pin
Karpov20073-Feb-07 9:35
Karpov20073-Feb-07 9:35 
GeneralRe: 64-bit puzzle Pin
Mark Salsbery3-Feb-07 10:38
Mark Salsbery3-Feb-07 10:38 
GeneralRe: 64-bit puzzle Pin
cmk3-Feb-07 13:08
cmk3-Feb-07 13:08 
Nope, as the OP states their example works fine as 32bit but fails when compiled as 64bit.
The compiler gives no errors or warnings.

As an example, when i run as 64bit:
int  *ptr = array + 3;    // ptr = 0x000000000012feb4
ptr = ptr + (a + b);      // ptr = 0x000000040012feb0 
                                            ^

The disassembly shows ptr = ptr + (a + b):
As posted by OP
0000000000401078  mov         eax,dword ptr [b] 
000000000040107C  mov         ecx,dword ptr [a] 
0000000000401080  add         ecx,eax 
0000000000401082  mov         eax,ecx 
0000000000401084  mov         eax,eax  <--- looks like an effective no-op to me (rax == eax)
0000000000401086  mov         rcx,qword ptr [ptr] 
000000000040108B  lea         rax,[rcx+rax*4]  <--- bad math, rax wrong
000000000040108F  mov         qword ptr [ptr],rax 
 
Change 'unsigned b = 1;' to 'int b = 1;'
0000000000401078  mov         eax,dword ptr [b] 
000000000040107C  mov         ecx,dword ptr [a] 
0000000000401080  add         ecx,eax 
0000000000401082  mov         eax,ecx 
0000000000401084  cdqe        <--- convert DWORD to QWORD  (rax = sign extend of eax)
0000000000401086  mov         rcx,qword ptr [ptr] 
000000000040108B  lea         rax,[rcx+rax*4]  <--- good math, rax correct
000000000040108F  mov         qword ptr [ptr],rax

The problem is with the unsigned b, change it to int b and everything is fine again.

To me this _should_ be a compiler bug.
The OP seems to imply it is correct (though possibly unexpected) behaviour for x64 compilers.

I've been doing x64 coding for the last year and never run into any issues like this.


[OP code - reposted for bug dissection]
int _tmain(int argc, _TCHAR* argv[])
{
	int       a = -2;
	unsigned  b =  1;
	int       array[5] = { 1, 2, 3, 4, 5 };
	int      *ptr = array + 3;

	ptr = ptr + (a + b);   // Invalid pointer value on 64-bit platform
	printf("%i\n", *ptr);  // Access violation on 64-bit platform

	return 0;
}
[/OP code]



...cmk

Save the whales - collect the whole set

GeneralRe: 64-bit puzzle Pin
Karpov20073-Feb-07 19:55
Karpov20073-Feb-07 19:55 
GeneralRe: 64-bit puzzle Pin
cmk3-Feb-07 20:22
cmk3-Feb-07 20:22 
GeneralRe: 64-bit puzzle Pin
Mark Salsbery4-Feb-07 7:59
Mark Salsbery4-Feb-07 7:59 
GeneralRe: 64-bit puzzle Pin
cmk4-Feb-07 13:11
cmk4-Feb-07 13:11 
GeneralRe: 64-bit puzzle Pin
Mike Dimmick5-Feb-07 5:27
Mike Dimmick5-Feb-07 5:27 
GeneralRe: 64-bit puzzle Pin
ricecake5-Feb-07 7:25
ricecake5-Feb-07 7:25 
GeneralRe: 64-bit puzzle Pin
cmk5-Feb-07 11:01
cmk5-Feb-07 11:01 
GeneralRe: 64-bit puzzle Pin
ricecake6-Feb-07 3:35
ricecake6-Feb-07 3:35 
QuestionHow to dock two dialogbars on right by top on bottom Pin
Member 37917193-Feb-07 3:57
Member 37917193-Feb-07 3:57 
AnswerRe: How to dock two dialogbars on right by top on bottom Pin
Mark Salsbery3-Feb-07 7:38
Mark Salsbery3-Feb-07 7:38 
QuestionImage library Pin
ceejeeb3-Feb-07 3:28
ceejeeb3-Feb-07 3:28 
AnswerRe: Image library Pin
Chris Losinger3-Feb-07 9:20
professionalChris Losinger3-Feb-07 9:20 
GeneralRe: Image library Pin
ceejeeb4-Feb-07 2:50
ceejeeb4-Feb-07 2:50 
QuestionHow can i scan all file in Some folder and check if they contain some string ? Pin
Yanshof3-Feb-07 3:22
Yanshof3-Feb-07 3:22 
AnswerRe: How can i scan all file in Some folder and check if they contain some string ? Pin
Waldermort3-Feb-07 3:51
Waldermort3-Feb-07 3:51 
QuestionRe: How can i scan all file in Some folder and check if they contain some string ? Pin
David Crow3-Feb-07 7:48
David Crow3-Feb-07 7:48 
AnswerRe: How can i scan all file in Some folder and check if they contain some string ? Pin
Waldermort3-Feb-07 9:10
Waldermort3-Feb-07 9:10 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.