Вы находитесь на странице: 1из 22

64: : 23.07.

2007

++ 6464, " ", . / ++", " 64Viva64. 646464. .

, .

32++, ( ." , . . , ++ ,

64. ) !", ,

, , MSDN, : 64Windows; 64Windows.

,
y y

int long size_t, time_t, , . , Windows, .

32ptrdiff_t

64,

64. memsize32 64 . . . ++ 64 " , : size_t, .

ptrdiff_t, DWORD_PTR, LONG_PTR ,

"20 http://www.viva64.com/art-1-2-599168895.html

, . . 64. Viva64 Viva64 (www.viva64.com/download.php), Viva64. PortSample. ,

PortSample
bool IsX64Platform() {

return sizeof(size_t) == 8; } template <typename A, typename B> inline size_t SafeMul(A a, B b) { return static_cast<size_t> (a) * static_cast<size_t>(b); } template <typename A, typename B, typename C> inline size_t SafeMul(A a, B b, C c) { return static_cast<size_t>(a) * static_cast<size_t>(b) * static_cast<size_t>(c); } template <typename A, typename B, typename C, typ ename D> inline size_t SafeMul(A a, B b, C c, D d) { return static_cast<size_t>(a) * static_cast<size_t>(b) * static_cast<size_t>(c) * static_cast<size_t>(d); } void V101() { unsigned imageWidth = 1000; unsigned imageHeght = 1000; unsigned bytePerPixel = 3; unsigned maxFrameCountInBuffer;

if (IsX64Platform()) { maxFrameCountInBuffer = 2000; } else { maxFrameCountInBuffer = 100; } size_t bufferSize = imageWidth * imageHeght * bytePerPixel * max FrameCountInBuffer; BYTE *buffer = static_cast<BYTE *>(malloc(bufferSize)); BYTE *ptr = buffer; for (unsigned frame = 0; frame != maxFrameCountInBuffer; ++frame) for (unsigned width = 0; width != imageWidth; ++width) for (unsigned height = 0; height != imageHeght; ++height) { *ptr++ = 0xFF; *ptr++ = 0xFF; *ptr++ = 0x00; } free (buffer); } void V102() { int domainWidth; int domainHeght; int domainDepth; if (IsX64Platform()) { domainWidth = 2000; domainHeght = 2000; domainDepth = 2000; } else { domainWidth = 500; domainHeght = 500;

domainDepth = 500; }

char *buffer = new char [size_t(domainWidth) * size_t(domainHeght) * size_t(domainDepth)];

char *current = buffer; char *end = buffer; end += domainWidth * domainHeght * domainDepth; while (current != end) *current++ = 1; delete [] buffer; } void V103() { size_t Megabyte = 1048576; size_t Gigabyte = 1073741824; size_t n = IsX64Platform() ? Gigabyte : Megabyte; unsigned arraySize = n * sizeof(INT_PTR); INT_PTR *buffer = (INT_PTR *)malloc(size_t(arraySize)); for (size_t i = 0; i != n; ++i) buffer[i] = 0; free(buffer); } void V104() { volatile size_t n; if (IsX64Platform()) { n = SafeMul(5, 1024, 1024, 1024);

} else { n = SafeMul(5, 1024, 1024); }

char *buffer = new char [n];

volatile size_t index = 0; volatile unsigned i; for (i = 0; i != n; ++i) buffer[index++] = 1; delete [] buffer; } void V105() { bool flag = true; unsigned a = unsigned( -1); if ((flag ? a : sizeof(float)) != size_t( -1)) { throw CString("x64 portability issues"); } } void V106() { void *buffer; const unsigned Megabyte = 1024 * 1024; const unsigned Gigabyte = 1024 * 1024 * 1024; unsigned unit; if (IsX64Platform()) unit = Gigabyte; else unit = Megabyte;

buffer = malloc(5 * unit); if (IsX64Platform()) memset(buffer, 0, SafeMul(5, 1024, 1024, 1024)); else memset(buffer, 0, SafeMul(5, 1024, 1024)); free(buffer); } void V107_FillFunction(char *array, unsigned arraySize) { for (unsigned i = 0; i != arraySize; ++i) array[i] = 1; } void V107() { size_t n; if (IsX64Platform()) { n = SafeMul(5, 1024, 1024, 1024); } else { n = SafeMul(5, 1024, 1024); } char *array = (char *)malloc(n * sizeof(char)); memset(array, 0, n * sizeof(char)); V107_FillFunction(array, n); for (size_t i = 0; i != n; ++i) if (array[i] != 1) throw CString("x64 p ortability issues"); free(array); } void V108() { size_t n;

if (IsX64Platform()) { n = SafeMul(5, 1024, 1024, 1024); } else { n = SafeMul(5, 1024, 1024); } char *array = (char *)malloc(n * sizeof(char)); memset(array, 0, n * sizeof (char)); volatile int index = 0; for (size_t i = 0; i != n; ++i) { array[index++] = 1; if (array[i] != 1) throw CString("x64 portability issues"); } free(array); } ptrdiff_t UnsafeCalcIndex(int x, int y, int width) { volatile int result = x + y * width; return result; } void V109() { int domainWidth; int domainHeght; if (IsX64Platform()) { domainWidth = 50000; domainHeght = 50000; } else { domainWidth = 5000; domainHeght = 5000; }

char *array = (char *)m alloc(SafeMul(domainWidth, domainHeght)); for (int x = 0; x != domainWidth; ++x) for (int y = 0; y != domainHeght; ++y) { array[UnsafeCalcIndex(x, y, domainWidth)] = 55; } free(array); } int UnsafeStrLen(const char *text) { const char *ptr = text; while (*ptr != 0) ++ptr; return ptr - text; } void V110() { size_t n; CString trueSize; if (IsX64Platform()) { n = SafeMul(3, 1024, 1024, 1024); trueSize = _T("3221225472"); } else { n = SafeMul(3, 1024, 1024); trueSize = _T("3145728"); } char *str = (char *)malloc(n * sizeof(char)); memset(str, 'V', n * sizeof(char)); str[n - 1] = 0; int len = UnsafeStrLen(str); CString falseSize; falseSize.Format(_T("%i"), len + 1);

free(str); if (falseSize != trueSize) throw CString(_T("x64 portability issues")); } void V111() { char invalidStr[100], validStr[100]; const char *invalidFormat = "%u"; const char *validFormat = "%Iu"; size_t a = SIZE_MAX; sprintf_s(invalidStr, sizeof(invalidStr),invalidFormat, a); sprintf_s(validStr, sizeof(validStr), validFormat, a); if (strcmp(invalidStr, validStr) != 0) throw CString(_T("x64 portability issues")); } void V113() { size_t a = size_t( -1); double b = a; --a; --b; size_t c = b; if (a != c) throw CString(_T("x64 portability issues")); } void V114() { unsigned intPtr[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; size_t *sizetPtr = (size_t *)(intPtr); size_t sum = 0;

for (size_t i = 0; i != 10; ++i) sum += sizetPtr[i]; if (sum != 45) throw CString(_T("x64 portability issues")); } void V301() { class CWinAppTest { public: virtual void WinHelp(DWORD_PTR, UINT) { ::AfxMessageBox(_T("Cannot activate WinHelp")); } }; class CPortSampleApp : public CWinAppTest { public: virtual void WinHelp(DWORD, UINT) { ::AfxMessageBox(_T("WinHelp activated")); } }; CWinAppTest *Application = new CPo rtSampleApp(); Application->WinHelp(NULL, 0); delete Application; } int _tmain(int argc, TCHAR* argv[]) { V101(); V102(); V103(); V104(); V105();

V106(); V107(); V108(); V109(); V110(); V111(); V112(); V113(); V114(); V201(); V202(); V203(); V301(); return 0; }

, ,

, , 32, . , ,

, .

. 64.

memsize
void V101() { unsigned imageWidth = 1000; unsigned imageHeght = 1000; unsigned bytePerPixel = 3; unsigned maxFrameCountInBuffer; if (IsX64Platform()) { maxFrameCountInBuffer = 2000; } else { maxFrameCountInBuffer = 100; }

size_t bufferSize = imageWidth * imageHeght * bytePerPixel * maxFrame CountInBuffer; BYTE *buffer = static_cast<BYTE *>(malloc(bufferSize)); BYTE *ptr = buffer; for (unsigned frame = 0; frame != maxFrameCountInBuffer; ++frame) for (unsigned width = 0; width != imageWidth; ++width) for (unsigned height = 0; height != imageHeght; ++height) { *ptr++ = 0xFF; *ptr++ = 0xFF; *ptr++ = 0x00; } free (buffer); }

:
size_t bufferSize = imageWidth * imageHeght * bytePerPixel * maxFrameCountInBuffer;

, 32 size_t, 6432unsigned. 4 , . , .

, , ? !

unsigned,

32-

64unsigned,

memsizevoid V102() { int domainWidth; int domainHeght; int domainDepth; if (IsX64Platform()) { domainWidth = 2000; domainHeght = 2000;

domainDepth = 2000; } else { domainWidth = 500; domainHeght = 500; domainDepth = 500; }

char *buffer = new char [size_t(domainWidth) * size_t(domainHeght) * size_t(domainDepth)];

char *current = buffer; char *end = buffer; end += domainWidth * domainHeght * domainDepth; while (current != end) *current++ = 1; delete [] buffer; }

, memsize:

end += domainWidth * domainHeght * domainDepth;

, .

64-

end

memsizevoid V103() { size_t Megabyte = 1048 576; size_t Gigabyte = 1073741824; size_t n = IsX64Platform() ? Gigabyte : Megabyte; unsigned arraySize = n * sizeof(INT_PTR); INT_PTR *buffer = (INT_PTR *)malloc(size_t(arraySize));

for (size_t i = 0; i != n; ++i) buffer[i] = 0; free(buffer); }

:
unsigned arraySize = n * sizeof(INT_PTR);

unsigned).

64-

memsizevoid V104() { volatile size_t n; if (IsX64Platform()) { n = SafeMul(5, 1024, 1024, 1024); } else { n = SafeMul(5, 1024, 1024); }

char *buffer = new char [n];

volatile size_t index = 0; volatile unsigned i; for (i = 0; i != n; ++i) buffer[index++] = 1; delete [] buffer; }

, . :
for (i = 0; i != n; ++i)

, . i ,

unsigned , i != n

n unsigned . .

size_t, 4 ?

memsize
void V105() { bool flag = true; unsigned a = unsigned( -1); if ((flag ? a : sizeof(float)) != size_t( -1)) { throw CString("x64 portability issues"); } }

?:

.
if ((flag ? a : sizeof(float)) != size_t( -1)) {

a ?

unsigned, unsigned(-1) memsize.

size_t , size_t (-1).

void V106() { void *buffer; const unsigned Megabyte = 1024 * 1024; const unsigned Gigabyte = 1024 * 1024 * 1024; unsigned unit; if (IsX64Platf orm()) unit = Gigabyte; else unit = Megabyte; buffer = malloc(5 * unit); if (IsX64Platform()) memset(buffer, 0, SafeMul(5, 1024, 1024, 1024));

else memset(buffer, 0, SafeMul(5, 1024, 1024)); free(buffer); }

:
buffer = malloc(5 * unit);

. . unit

? unsigned.

64malloc() (5 * unit)

5 . memsize5 .

5 , . .

memsizevoid V107_FillFunction(char *array, unsigned arraySize) { for (unsigned i = 0; i != arraySize; ++i) array[i] = 1; } void V107() { size_t n; if (IsX64Platform()) { n = SafeMul(5, 1024, 1024, 1024); } else { n = SafeMul(5, 1024, 1024); } char *array = (char *)malloc(n * sizeof(char)); memset(array, 0, n * sizeof(char)); V107_FillFunction(array, n); for (size_t i = 0; i != n; ++i) if (array[i] != 1) throw CString("x64 portability issues"); free(array);

32-

,
V107_FillFunction(array, n);

n unsigned. , .

void V108() { size_t n; if (IsX64Platform()) { n = SafeMul(5, 1024, 1024, 1024); } else { n = SafeMul(5, 1024, 1024); } char *array = (char *)malloc(n * sizeof(char)); memset(array, 0, n * sizeof(char)); volatile int index = 0; for (size_t i = 0; i != n; ++i) { array[index++] = 1; if (array[i] != 1) throw CString("x64 portability issues"); } free(array); }

memsizearray[index++] = 1;

, unsigned

4 .

memsizeptrdiff_t UnsafeCalcIndex(int x, int y, int width) { volatile int result = x + y * width; return result; } void V109() { int domainWidth; int domainHeght; if (IsX64Platform()) { domainWidth = 50000; domainHeght = 50000; } else { domainWidth = 5000; domainHeght = 5000; } char *array = (char *)malloc(SafeMul(domainWidth, domainHeght)); for (int x = 0; x != domainWidth; ++x) for (int y = 0; y != domainHeght; ++y) { array[UnsafeCalcIndex(x, y, domainWidth)] = 55; } free(array); }

,
return result;

result UnsafeCalcIndex() ,

int, , , ptrdiff_t.

ptrdiff_t. 2 result. .

memsizeint UnsafeStrLen(const char *text) { const char *ptr = text; while (*ptr != 0) ++ptr; return ptr - text; } void V110() { size_t n; CString trueSize; if (IsX64Platform()) { n = SafeMul(3, 1024, 1024, 1024); trueSize = _T("3221225472"); } else { n = SafeMul(3, 1024, 1024); trueSize = _T("3145728"); } char *str = (char *)malloc(n * sizeof(char)); memset(str, 'V', n * sizeof(char)); str[n - 1] = 0; int len = UnsafeStrLen(str); CString falseSize; falseSize.Format(_T("%i"), len + 1); if (falseSize != trueSize) throw CString(_T("x64 portability issues")); }

,
return ptr - text;

, ( . )

memsize ,

int. 2

memsizevoid V111() { char invalidStr[100], validStr[100]; const char *invalidForma t = "%u"; const char *validFormat = "%Iu"; size_t a = SIZE_MAX; sprintf_s(invalidStr, sizeof(invalidStr),invalidFormat, a); sprintf_s(validStr, sizeof(validStr), validFormat, a); if (strcmp(invalidStr, validStr) != 0) throw CString(_T("x64 po rtability issues")); }

/ :

const char *invalidFormat = "%u"; sprintf_s(invalidStr, sizeof(invalidStr),invalidFormat, a);

32.

64-

memsizevoid V113() { size_t a = size_t( -1); double b = a; --a; --b; size_t c = b;

double

if (a != c) throw CString(_T("x64 portability issues")); }

:
double b = a;

size_t c = b;

64-

void V114() { unsigned intPtr[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; size_t *sizetPtr = (size_t *)(intPtr); size_t sum = 0; for (size_t i = 0; i != 10; ++i) sum += sizetPtr[i]; if (sum != 45) throw CString(_T("x64 portability issues")); }

++, memsize

, , , :

size_t *sizetPtr = (size_t *)(intPtr);

size_t unsigned.

void V301() { class CWinAppTest { public: virtual void WinHelp(DWORD_PTR, UINT) {

::AfxMessageBox(_T("Cannot activate WinHelp")); } }; class CPortSampleApp : public CWinAppTest { public: virtual void WinHelp(DWORD, UINT) { ::AfxMessageBox(_T("WinHelp activated")); } }; CWinAppTest *Application = new CPortSampleApp(); Application->WinHelp(NULL, 0); delete Application; }

++ , . WinHelp() 32, 64.

64 -

DWORD_PTR DWORD , ! "Cannot activate WinHelp".

, 64, .

, ? 5 , , . , Windows? ? , , 2007 , , . ,

, 643264-

Viva64 (www.Viva64.com), . .

Оценить