编辑代码

#include <stdio.h>
#include <stdlib.h>

void func(unsigned char *buf)
{
    int size = 10;
    printf("%p\n", buf);
    buf = (char *)malloc(size);
    printf("%p\n", buf);
    for (int i = 0; i < size; i++)
    {
        buf[i] = 'a' + i;
    }
}

void print(unsigned char *buf, int size)
{
    printf("%p\n", buf);
    // for (int i = 0; i < size; i++)
    // {
    //     printf("buf[%d]: %c \n", i, buf[i]);
    // }
    // printf("\n");
}

void func2(unsigned char **buf, int size)
{
    *buf = (unsigned char*)malloc(size);
    for (int i = 0; i < size; i++)
    {
        (*buf)[i] = 'a' + i;
    }
}

void func_free(unsigned char **buf)
{
    if (NULL != *buf)
    {
        free(*buf);
        *buf = NULL;
    }
}

void func3(unsigned int *puiNumList, int size)
{
    if ((NULL == puiNumList) ||
        (0 == size))
    {
        return;
    }

    for (int i = 0; i < size; i++)
    {
        printf("num: %d\n", puiNumList[i]);
    }
}

void func4(unsigned char *buf, int size)
{
    buf = (unsigned char*)malloc(size);
    if (NULL == buf)
    {
        printf("malloc error\n");
    }

    for (int i = 0; i < size; i++)
    {
        buf[i] = 'a' + i;
    }
}

int main () {

    char* buf;

    func(buf);

    print(buf, 10);

    // free(buf);
    // buf = NULL;

    // if (buf)
    // {
    //     printf("here\n");
    // }

    // unsigned char *buf = NULL;

    // unsigned char *buf2 = NULL;

    // unsigned int auiNumList[10];

    // func2(&buf, 10);
    // if (buf)
    // {
    //     printf("here buf1\n");
    // }

    // func2(&buf2, 10);

    // if (buf2)
    // {
    //     printf("here buf2\n");
    // }

    // func_free(&buf);
    // if (buf)
    // {
    //     printf("here2\n");
    // }
    
    // memset(auiNumList, 0, sizeof(auiNumList));
    // func3(auiNumList, 10);

    return 0;
}