malloc,memset,memcpy使用


    char *temp1 = 0;
    char *buf1 = "123456789AB";
    temp1 = (char*)malloc(10 * sizeof(char));

    memset(temp1,'0',10); //使用前初始化,malloc中的值全为 字符0
    memcpy(temp1+4,buf1,1);
    std::cout << "*ptr=" << sizeof(temp1) << "_" << strlen(temp1) << "_" << temp1 << std::endl;
    free(temp1);
    memset(temp1,0,10); //清空,注意这句会让指针指向空指针,打印指针的值为0
    std::cout << sizeof(temp1) << strlen(temp1) << temp1 << std::endl;

posted @ 2024-04-05 22:13  BISTER  阅读(11)  评论(0)    收藏  举报  来源