x86 program on Windows is OK but x64 program crashes

Source: Internet
Author: User

Let's look at the following code:

#include <stdio.h>#include<windows.h>#include<memory>classtest{ Public: Test (inti): A (i) {}~Test () {} Public:    voidPrint () {printf ("a=%d\n", a); }Private:    intA;};intMain () {std::shared_ptr<Test> AA (NewTest (6)); AA-Print (); wprintf (L"test* Size:%d\n",sizeof(test*)); wprintf (L"ULONG Size:%d\n",sizeof(ULONG)); ULONG Data= (ulonglong) AA.Get(); (Test*) data)Print ();}

x86 environment, the operation is normal (the output is as follows):

A=6
test* Size:4
ULONG Size:4
A=6

But if you compile the x64 program and run it, there's a problem here:

A=6

test* Size:8
ULONG Size:4

----------------------------> Crash.

In fact, from the printed size, it is obvious that these two size inconsistencies resulted in the pointer being 8 bytes in the x64 bit environment and only 4 bytes in the x86 environment (usually).

So if the program wants to run in both x86 and x64 environments, you need to consider this.

x86 program on Windows is OK but x64 program crashes

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.