随笔 - 505  文章 - 1034  trackbacks - 0
<2006年12月>
262728293012
3456789
10111213141516
17181920212223
24252627282930
31123456


子曾经曰过:编程无他,唯手熟尔!

常用链接

留言簿(94)

随笔分类(649)

随笔档案(505)

相册

BCB

Crytek

  • crymod
  • Crytek's Offical Modding Portal

Game Industry

OGRE

other

Programmers

Qt

WOW Stuff

搜索

  •  

积分与排名

  • 积分 - 894889
  • 排名 - 14

最新随笔

最新评论

阅读排行榜

评论排行榜

atoi使用时要注意了

这样子写有问题,可能导致down掉,因为ch没有结束符.但是我没看出怎么当的

2007-10-09  下面这个程序不会当,但是 number 的值可能不是预期的。

int _tmain(int argc, _TCHAR* argv[])
{
    
char ch[1= {0};
    ch[
0= '9';

    
int number = ::atoi(ch);

    std::cout 
<< number << std::endl;

    
return 0;
}

以下是atox.c的源码:
/***
*atox.c - atoi and atol conversion
*
*       Copyright (c) Microsoft Corporation. All rights reserved.
*
*Purpose:
*       Converts a character string into an int or long.
*
******************************************************************************
*/

#include 
<cruntime.h>
#include 
<stdlib.h>
#include 
<ctype.h>
#include 
<mtdll.h>
#include 
<tchar.h>
#ifdef _MBCS
#undef _MBCS
#endif  /* _MBCS */

#ifndef _UNICODE
#define _tchartodigit(c)    ((c) >= '0' && (c) <= '9' ? (c) - '0' : -1)
#else  /* _UNICODE */
int _wchartodigit(wchar_t);
#define _tchartodigit(c)    _wchartodigit((wchar_t)(c))
#endif  /* _UNICODE */

/***
*long atol(char *nptr) - Convert string to long
*
*Purpose:
*       Converts ASCII string pointed to by nptr to binary.
*       Overflow is not detected.
*
*Entry:
*       nptr = ptr to string to convert
*
*Exit:
*       return long int value of the string
*
*Exceptions:
*       None - overflow is not detected.
*
******************************************************************************
*/

long __cdecl _tstol(
        
const _TCHAR *nptr
        )
{
        
int c;              /* current char */
        
long total;         /* current total */
        
int sign;           /* if '-', then negative, otherwise positive */
#if defined (_MT) && !defined (_UNICODE)
        pthreadlocinfo ptloci 
= _getptd()->ptlocinfo;

        
if ( ptloci != __ptlocinfo )
            ptloci 
= __updatetlocinfo();

        
/* skip whitespace */
        
while ( __isspace_mt(ptloci, (int)(_TUCHAR)*nptr) )
#else  /* defined (_MT) && !defined (_UNICODE) */
        
while ( _istspace((int)(_TUCHAR)*nptr) )
#endif  /* defined (_MT) && !defined (_UNICODE) */
            
++nptr;

        c 
= (int)(_TUCHAR)*nptr++;
        sign 
= c;           /* save sign indication */
        
if (c == _T('-'|| c == _T('+'))
            c 
= (int)(_TUCHAR)*nptr++;    /* skip sign */

        total 
= 0;

        
while ( (c = _tchartodigit(c)) != -1 ) {
            total 
= 10 * total + c;     /* accumulate digit */
            c 
= (_TUCHAR)*nptr++;    /* get next char */
        }

        
if (sign == '-')
            
return -total;
        
else
            
return total;   /* return result, negated if necessary */
}


/***
*int atoi(char *nptr) - Convert string to long
*
*Purpose:
*       Converts ASCII string pointed to by nptr to binary.
*       Overflow is not detected.  Because of this, we can just use
*       atol().
*
*Entry:
*       nptr = ptr to string to convert
*
*Exit:
*       return int value of the string
*
*Exceptions:
*       None - overflow is not detected.
*
******************************************************************************
*/

int __cdecl _tstoi(
        
const _TCHAR *nptr
        )
{
        
return (int)_tstol(nptr);
}

#ifndef _NO_INT64

__int64 __cdecl _tstoi64(
        
const _TCHAR *nptr
        )
{
        
int c;              /* current char */
        __int64 total;      
/* current total */
        
int sign;           /* if '-', then negative, otherwise positive */
#if defined (_MT) && !defined (_UNICODE)
        pthreadlocinfo ptloci 
= _getptd()->ptlocinfo;

        
if ( ptloci != __ptlocinfo )
            ptloci 
= __updatetlocinfo();

        
/* skip whitespace */
        
while ( __isspace_mt(ptloci, (int)(_TUCHAR)*nptr) )
#else  /* defined (_MT) && !defined (_UNICODE) */
        
while ( _istspace((int)(_TUCHAR)*nptr) )
#endif  /* defined (_MT) && !defined (_UNICODE) */
            
++nptr;

        c 
= (int)(_TUCHAR)*nptr++;
        sign 
= c;           /* save sign indication */
        
if (c == _T('-'|| c == _T('+'))
            c 
= (int)(_TUCHAR)*nptr++;    /* skip sign */

        total 
= 0;

        
while ( (c = _tchartodigit(c)) != -1 ) {
            total 
= 10 * total + c;     /* accumulate digit */
            c 
= (_TUCHAR)*nptr++;    /* get next char */
        }

        
if (sign == _T('-'))
            
return -total;
        
else
            
return total;   /* return result, negated if necessary */
}

#endif  /* _NO_INT64 */
posted on 2007-09-28 09:19 七星重剑 阅读(1668) 评论(1)  编辑 收藏 引用 所属分类: PL--c/c++

FeedBack:
# re: atoi使用时要注意了 2007-10-09 10:07 rise
好像在vs2003可以通过。  回复  更多评论
  

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理