以下代码可以在64位系统上运行吗? [英] Can the following code run on 64bit system?
问题描述
我试图确保我的代码可以在32位和64位系统上运行。请允许以下代码*在64位编译器上运行编译*?如果不是,有人可以向我展示一个可以在64位编译器*上编译*的修改版本吗?代码是:
INT_PTR GetShortPath(THAR * szFullPath,TCHAR * pszShortPath, INT_PTR iStrlen)
{
if (!lstrlen(pszFullPath))
{
返回(INT_PTR)FALSE;
}
TCHAR * p,* q;
p = pszFullPath,q = pszShortPath;
INT_PTR I = 0 ;
while (* p)
{
* q = * p;
p ++,q ++;
}
while (* p!= T(' \\'))
{
if (I< iStrlen )
{
* q = * p;
p ++,q ++,I ++;
}
其他
{
返回(INT_PTR )假;
}
}
* q = T(' \0'跨度>);
return (INT_PTR)TRUE;
}
此代码包含以下错误:
1.返回值类型是一个布尔值。使用类型bool
(以及值true
和false
,而不是TRUE
和FALSE
)。如果返回值传递给系统函数,则可能需要使用BOOL
而不是
2.变量iStrlen
和I
用作数组的下标。它们应定义为std :: size_t
。您可以使用SIZE_T
,这是相同类型的MS版本,但是他们无法正确定义和使用类型已经充分记录了他们需要的事实介绍* _ PTR
类型 - 那么当你可以使用标准化类型时,为什么还要使用不可靠的类型定义呢?
3 。TCHAR
类型的文字需要使用_T()
宏定义,而不是T ()
。你错过了领先的下划线。
为你修正了这些类型和价值:
bool GetShortPath(THAR * szFullPath,TCHAR * pszShortPath,std :: size_t iStrlen) // 已修复
{
如果(!lstrlen (pszFullPath))
{
return false ; // fixed
}
TCHAR * p,* q;
p = pszFullPath,q = pszShortPath;
std :: size_t I = 0 ; // 已修复
而(* p)
{
* q = * p;
p ++,q ++;
}
while (* p!= _T(' \\')) // 已修复
{
if (I< iStrlen)
{
* q = * p;
p ++,q ++,I ++;
}
其他
{
返回 false ; // 已修复
}
}
* q = _T(' \ 0'); < span class =code-comment> // fixed
return < span class =code-keyword> true ; // fixed
}
请注意,您应该始终使用正确的类型 - 否则您可能根本就没有使用任何类型并在汇编程序中编程!
不要使用MS类型,除非你真的,真的必须(即你正在连接到需要它们的系统函数,或使用专有类型定义,如TCHAR)。
I am trying to ensurs that my codes can run on both 32bit and 64bit systems.Please can the following code *run compile on 64bit compiler*? If no, can someon show me a modified version that can * complile on 64bit compiler*?The code is:
INT_PTR GetShortPath( THAR *szFullPath, TCHAR *pszShortPath, INT_PTR iStrlen) { if(!lstrlen(pszFullPath)) { return (INT_PTR)FALSE; } TCHAR *p, *q; p = pszFullPath, q = pszShortPath; INT_PTR I = 0; while(*p) { *q = *p; p++,q++; } while(*p != T('\\')) { if( I < iStrlen) { *q = *p; p++, q++,I++; } else { return (INT_PTR)FALSE; } } *q = T('\0'); return (INT_PTR)TRUE; }解决方案This code contains the following errors:
1. The return value type is a boolean value. Use typebool
(and the valuestrue
andfalse
, rather thanTRUE
andFALSE
). If the return value is passed to a system function, you may need to useBOOL
instead
2. the variablesiStrlen
andI
are used as subscript into an array. They should be defined asstd::size_t
. You can useSIZE_T
instead, which is the MS-Version of the same type, but their inability to define and use types properly has already been sufficiently documented by the fact they needed to introduce the*_PTR
types - so why bother with unreliable type definitions when you can use standardized types?
3. Literals of typeTCHAR
need to be defined using the_T()
macro, notT()
. You are missing the leading underscores.
Fixed those types and values for you:
bool GetShortPath( THAR *szFullPath, TCHAR *pszShortPath, std::size_t iStrlen) //fixed { if(!lstrlen(pszFullPath)) { return false;//fixed } TCHAR *p, *q; p = pszFullPath, q = pszShortPath; std::size_t I = 0;//fixed while(*p) { *q = *p; p++,q++; } while(*p != _T('\\'))//fixed { if( I < iStrlen) { *q = *p; p++, q++,I++; } else { return false;//fixed } } *q = _T('\0');//fixed return true;//fixed }
Please note that you should always use the correct type - else you might just be using no type at all and program in Assembler!
Don't use the MS types unless you really, really must (i. e. you're interfacing to system functions that require them, or using a proprietary type definition such as TCHAR).
这篇关于以下代码可以在64位系统上运行吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!