Chinaunix首页 | 论坛 | 博客
  • 博客访问: 653446
  • 博文数量: 160
  • 博客积分: 2384
  • 博客等级: 大尉
  • 技术积分: 1366
  • 用 户 组: 普通用户
  • 注册时间: 2008-12-01 11:35
文章分类
文章存档

2015年(45)

2014年(36)

2012年(28)

2011年(37)

2010年(2)

2009年(10)

2008年(2)

分类: C/C++

2014-11-04 23:42:21

//

//  main.cpp

//  AUTO_PRO

//

//  Created by yanzhengqing on 12-12-11.

//  Copyright (c) 2012 yanzhengqing. All rights reserved.

//


#include 

using namespace std;



/***

*char *memset(dst, val, count) - sets "count" bytes at "dst" to "val"

*

*Purpose:

*       Sets the first "count" bytes of the memory starting

*       at "dst" to the character value "val".

*

*Entry:

*       void *dst - pointer to memory to fill with val

*       int val   - value to put in dst bytes

*       size_t count - number of bytes of dst to fill

*

*Exit:

*       returns dst, with filled bytes

*

*Exceptions:

*

*******************************************************************************/




/////////////////////////////////////////////////////////////////////////////////

/*说明:

  1. __cdecl C Declaration的缩写(declaration,声明),表示C语言默认的函数调用方法:所有参数从右到左依次入栈,这些参数由调用者清除,称为手动清栈。被调用函数不会要求调用者传递多少参数,调用者传递过多或者过少的参数,甚至完全不同的参数都不会产生编译阶段的错误。

  2.将s所指向的某一块内存中的每个字节的内容全部设置为ch指定的ASCII值, 块的大小由第三个参数指定,这个函数通常为新申请的内存做初始化工作, 其返回值为指向S的指针。

  3.  按照ANSI(American National Standards Institute)标准,不能对void指针进行算法操作,即不能对void指针进行如p++的操作,所以需要转换为具体的类型指针来操作,例如char *。(引用网友的结论)

  4.  size_t 类型定义在cstddef头文件中,该文件是C标准库的头文件stddef.hC++版。它是一个与机器相关的unsigned类型,其大小足以保证存储内存中对象的大小。

*/



void * __cdecl memset (

                      void *dst,

                      int val,

                      size_t count

                       )

{

   void *start = dst;

    

#if defined (_M_MRX000) || defined (_M_ALPHA) || defined (_M_PPC)

    {

       externvoid RtlFillMemory(void *, size_t count,char );

        

        RtlFillMemory( dst, count, (char)val );

    }

#else  /* defined (_M_MRX000) || defined (_M_ALPHA) || defined (_M_PPC) */

   while (count--) {

        *(char *)dst = (char)val;

        dst = (char *)dst +1;

    }

#endif  /* defined (_M_MRX000) || defined (_M_ALPHA) || defined (_M_PPC) */

    

   return(start);

}




int main()

{

    char dst[50]="you are a good boy!!";

    cout<<"Before memcpy dest = \n"<endl;

    memset(dst,0,sizeof(dst));

    cout<<"After memcpy dest = \n"<endl;

    cout<<"program end...."<<endl;



   return0;

}



/**********************************************************************************************************/
实例:

Before memcpy dest = 

you are a good boy!!

After memcpy dest = 

program end....

阅读(446) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~