zoukankan      html  css  js  c++  java
  • C 函数 strcpy and memcpy, strdup, memcmp,memset 库函数皆小写

     void *memcpy(void *dst, const void *src, size_t n);

    //If copying takes place between objects that overlap, the behavior is undefined

    因此标准库提供了地址重叠时的内存拷贝函数:memmove(),

    该函数把源字符串拷贝到临时buf里,然后再从临时buf里写到目的地址

     

    strcpy   and  memcpy 

    memcpy 可以进行指定字节数量的任意类型数据的拷贝, 可以用于内存拷贝。

      char a[100], b[50];

      memcpy(b, a,sizeof(b)); //not sizeof(a),which may cause overflow

    strcpy只能用于拷贝字符串,遇到''终止拷贝。

      char a[100], b[50];

      strcpy(a,b);

    void *memset(void *buffer,int c,int count);

    把buffer所指内存区域的前count个字节设置成字符c, 一般用于对指定的字符串清零。

    below from    http://www.cnblogs.com/aprilapril/p/4333173.html

    ..........strdup("adm"); //as it shows itself, only valid to string type.

    memcpy function,return an adress pointed at heap which store"adm"(with string end sign).

    .........strcpy(pAim, "4021");
    "4021" is copied into the memory started at address pAim.

    ...........memcpy(void*to,void*from,int size)
    copy designated size  to the memory.

    ..........memcmp(void *a, void *b, int size)

    copare two storage, if equal,return to 0,otherwise, get a positive or negative value.

    estrdup

    #include <config.h>
    #include <stdlib.h>
    #include <err.h>
    
    #include "roken.h"
    
    /*
     * Like strdup but never fails.
     */
    
    ROKEN_LIB_FUNCTION char * ROKEN_LIB_CALL
    estrdup (const char *str)
    {
        char *tmp = strdup (str);
    
        if (tmp == NULL)
        errx (1, "strdup failed");
        return tmp;
    }
  • 相关阅读:
    Java集合
    C#高级应用
    使用C#分层查询多个表数据
    数据库之SQL语句查询基础
    简要介绍一下MD5加密的书写
    C#简单工厂模式和单列设计模式潜要解析
    Struts2测试题
    小程序自定义组件
    flex布局笔记
    小程序的双线程模型
  • 原文地址:https://www.cnblogs.com/aprilapril/p/3191136.html
Copyright © 2011-2022 走看看