首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

这道面试题的答案一直是错的吗?该如何处理

2013-04-02 
这道面试题的答案一直是错的吗?今天突然想起来一道面试题:15.用C++写个程序,如何判断一个操作系统是16位还

这道面试题的答案一直是错的吗?
今天突然想起来一道面试题:
15.用C++写个程序,如何判断一个操作系统是16位还是32位的?不能用sizeof()函数
网上标准答案:
A1:16位的系统下,
int i = 65536;
cout << i; // 输出0;//装不下,最高位溢出,剩下16位的当然是0;
int i = 65535;
cout << i; // 输出-1;//-1的补码是65535
32位的系统下,
int i = 65536;
cout << i; // 输出65536;
int i = 65535;
cout << i; // 输出65535;
A2:
int a = ~0;
if( a>65536 )  
{  
cout<<"32 bit"<<endl;
}
else
{  
cout<<"16 bit"<<endl;
}
发现这两种解法都是一种思想,就是求int所占的位数。但是int所占的位数和操作系统有关吗?还是和编译器的设置有关?
[解决办法]

引用
发现这两种解法都是一种思想,就是求int所占的位数。但是int所占的位数和操作系统有关吗?还是和编译器的设置有关? 

给你一个连接
http://发现这两种解法都是一种思想,就是求int所占的位数。但是int所占的位数和操作系统有关吗?还是和编译器的设置有关?
简单的结论就是--compiler才是重点
谁管你是什么os或cpu, compiler都可以加上hardware abstraction layer

c++的standard只规定了int最小需要有几个byte
但是没有限定他的最大bytes
这道题目是那个天才面试官出的?
你就行行好指导一下这位面试官吧
要是他爱面子死不认错,就别进这家公司了
[解决办法]
引用:
引用:引用:C/C++ code?123456789101112int main(){    void**p=0;    p++;    switch( (int)p )    {        case 2:puts("16");break;        case 4:puts("32");……



懂了, 很多dll就是采用类似windows句柄的方式提供给用户使用。

但是释放的时候,dll内部需要再次转换为类指针。否则内存泄漏

举例:

typedef MYHANDLE{}* PMYHANDLE;

class Test
{
char* p;
int data;
.....
};

//创建函数
PMYHANDLE Create()
{
Test* pObj=new Test();
return (PMYHANDLE)pObj;
}
//释放函数

void Release(PMYHANDLE handle)
{
Test* pObj=(Test*)handle;
delete pObj;
}

如果这里, 不转换,这对handle进行delete,就会泄露




[解决办法]
#include <windows.h>
#include <tchar.h>
#include <stdio.h>

#define BUFSIZE 80

typedef void (WINAPI *PGNSI)(LPSYSTEM_INFO);

int __cdecl _tmain()
{
   OSVERSIONINFOEX osvi;
   SYSTEM_INFO si;
   PGNSI pGNSI;
   BOOL bOsVersionInfoEx;

   ZeroMemory(&si, sizeof(SYSTEM_INFO));
   ZeroMemory(&osvi, sizeof(OSVERSIONINFOEX));

   // Try calling GetVersionEx using the OSVERSIONINFOEX structure.
   // If that fails, try using the OSVERSIONINFO structure.

   osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);

   if( !(bOsVersionInfoEx = GetVersionEx ((OSVERSIONINFO *) &osvi)) )
   {
      osvi.dwOSVersionInfoSize = sizeof (OSVERSIONINFO);
      if (! GetVersionEx ( (OSVERSIONINFO *) &osvi) ) 


         return FALSE;
   }

   // Call GetNativeSystemInfo if supported
   // or GetSystemInfo otherwise.

   pGNSI = (PGNSI) GetProcAddress(
      GetModuleHandle(TEXT("kernel32.dll")), 
      "GetNativeSystemInfo");
   if(NULL != pGNSI)
      pGNSI(&si);
   else GetSystemInfo(&si);

   switch (osvi.dwPlatformId)
   {
      // Test for the Windows NT product family.

      case VER_PLATFORM_WIN32_NT:

      // Test for the specific product.

      if ( osvi.dwMajorVersion == 6 && osvi.dwMinorVersion == 0 )
      {
         if( osvi.wProductType == VER_NT_WORKSTATION )
             printf ("Windows Vista ");
         else printf ("Windows Server "Longhorn" " );
      }

      if ( osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 2 )
      {
         if( GetSystemMetrics(SM_SERVERR2) )
            printf( "Microsoft Windows Server 2003 "R2" ");
         else if( osvi.wProductType == VER_NT_WORKSTATION &&
            si.wProcessorArchitecture==PROCESSOR_ARCHITECTURE_AMD64)
         {
            printf( "Microsoft Windows XP Professional x64 Edition ");
         }
         else printf ("Microsoft Windows Server 2003, ");
      }

      if ( osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 1 )
         printf ("Microsoft Windows XP ");

      if ( osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 0 )
         printf ("Microsoft Windows 2000 ");

      if ( osvi.dwMajorVersion <= 4 )
         printf ("Microsoft Windows NT ");

      // Test for specific product on Windows NT 4.0 SP6 and later.
      if( bOsVersionInfoEx )
      {
         // Test for the workstation type.


         if ( osvi.wProductType == VER_NT_WORKSTATION &&
              si.wProcessorArchitecture!=PROCESSOR_ARCHITECTURE_AMD64)
         {
            if( osvi.dwMajorVersion == 4 )
               printf ( "Workstation 4.0 " );
            else if( osvi.wSuiteMask & VER_SUITE_PERSONAL )
               printf ( "Home Edition " );
            else printf ( "Professional " );
         }
            
         // Test for the server type.
         else if ( osvi.wProductType == VER_NT_SERVER 
[解决办法]
 
                   osvi.wProductType == VER_NT_DOMAIN_CONTROLLER )
         {
            if(osvi.dwMajorVersion==5 && osvi.dwMinorVersion==2)
            {
               if ( si.wProcessorArchitecture ==
                    PROCESSOR_ARCHITECTURE_IA64 )
               {
                   if( osvi.wSuiteMask & VER_SUITE_DATACENTER )
                      printf ( "Datacenter Edition "
                               "for Itanium-based Systems" );
                   else if( osvi.wSuiteMask & VER_SUITE_ENTERPRISE )
                      printf ( "Enterprise Edition "
                               "for Itanium-based Systems" );
               }

               else if ( si.wProcessorArchitecture ==
                         PROCESSOR_ARCHITECTURE_AMD64 )


               {
                   if( osvi.wSuiteMask & VER_SUITE_DATACENTER )
                      printf ( "Datacenter x64 Edition " );
                   else if( osvi.wSuiteMask & VER_SUITE_ENTERPRISE )
                      printf ( "Enterprise x64 Edition " );
                   else printf( "Standard x64 Edition " );
               }

               else
               {
                   if( osvi.wSuiteMask & VER_SUITE_DATACENTER )
                      printf ( "Datacenter Edition " );
                   else if( osvi.wSuiteMask & VER_SUITE_ENTERPRISE )
                      printf ( "Enterprise Edition " );
                   else if ( osvi.wSuiteMask & VER_SUITE_BLADE )
                      printf ( "Web Edition " );
                   else printf ( "Standard Edition " );
               }
            }
            else if(osvi.dwMajorVersion==5 && osvi.dwMinorVersion==0)
            {
               if( osvi.wSuiteMask & VER_SUITE_DATACENTER )
                  printf ( "Datacenter Server " );
               else if( osvi.wSuiteMask & VER_SUITE_ENTERPRISE )
                  printf ( "Advanced Server " );
               else printf ( "Server " );
            }
            else  // Windows NT 4.0 


            {
               if( osvi.wSuiteMask & VER_SUITE_ENTERPRISE )
                  printf ("Server 4.0, Enterprise Edition " );
               else printf ( "Server 4.0 " );
            }
         }
      }
      // Test for specific product on Windows NT 4.0 SP5 and earlier
      else  
      {
         HKEY hKey;
         TCHAR szProductType[BUFSIZE];
         DWORD dwBufLen=BUFSIZE*sizeof(TCHAR);
         LONG lRet;

         lRet = RegOpenKeyEx( HKEY_LOCAL_MACHINE,
            TEXT("SYSTEM\\CurrentControlSet\\Control\"
                 "ProductOptions"), 0, KEY_QUERY_VALUE, &hKey );
         if( lRet != ERROR_SUCCESS )
            return FALSE;

         lRet = RegQueryValueEx( hKey, TEXT("ProductType"),
            NULL, NULL, (LPBYTE) szProductType, &dwBufLen);
         RegCloseKey( hKey );

         if( (lRet != ERROR_SUCCESS) 
[解决办法]

             (dwBufLen > BUFSIZE*sizeof(TCHAR)) )
            return FALSE;

         if ( lstrcmpi( TEXT("WINNT"), szProductType) == 0 )
            printf( "Workstation " );
         if ( lstrcmpi( TEXT("LANMANNT"), szProductType) == 0 )
            printf( "Server " );
         if ( lstrcmpi( TEXT("SERVERNT"), szProductType) == 0 )
            printf( "Advanced Server " );
         printf( "%d.%d ", osvi.dwMajorVersion, osvi.dwMinorVersion );
      }

      // Display service pack (if any) and build number.

      if( osvi.dwMajorVersion == 4 && 


          lstrcmpi( osvi.szCSDVersion, TEXT("Service Pack 6") ) == 0 )
      { 
         HKEY hKey;
         LONG lRet;

         // Test for SP6 versus SP6a.
         lRet = RegOpenKeyEx( HKEY_LOCAL_MACHINE,
            TEXT("SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\"
                 "Hotfix\\Q246009"), 0, KEY_QUERY_VALUE, &hKey );
         if( lRet == ERROR_SUCCESS )
            printf( "Service Pack 6a (Build %d)\n", 
            osvi.dwBuildNumber & 0xFFFF );         
         else // Windows NT 4.0 prior to SP6a
         {
            _tprintf( TEXT("%s (Build %d)\n"),
               osvi.szCSDVersion,
               osvi.dwBuildNumber & 0xFFFF);
         }

         RegCloseKey( hKey );
      }
      else // not Windows NT 4.0 
      {
         _tprintf( TEXT("%s (Build %d)\n"),
            osvi.szCSDVersion,
            osvi.dwBuildNumber & 0xFFFF);
      }

      break;

      // Test for the Windows Me/98/95.
      case VER_PLATFORM_WIN32_WINDOWS:

      if (osvi.dwMajorVersion == 4 && osvi.dwMinorVersion == 0)
      {
          printf ("Microsoft Windows 95 ");
          if (osvi.szCSDVersion[1]=='C' 
[解决办法]
 osvi.szCSDVersion[1]=='B')
             printf("OSR2 " );
      } 

      if (osvi.dwMajorVersion == 4 && osvi.dwMinorVersion == 10)
      {
          printf ("Microsoft Windows 98 ");
          if ( osvi.szCSDVersion[1]=='A' 
------解决方案--------------------


 osvi.szCSDVersion[1]=='B')
             printf("SE " );
      } 

      if (osvi.dwMajorVersion == 4 && osvi.dwMinorVersion == 90)
      {
          printf ("Microsoft Windows Millennium Edition\n");
      } 
      break;

      case VER_PLATFORM_WIN32s:

      printf ("Microsoft Win32s\n");
      break;
   }
   return TRUE; 
}

热点排行