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

typedef struct _proc {
    char **argv;
} PROC;  

void test(PROC * this)
{
    /* 申请指针数组空间 */
    if (!(this->argv = (char**)malloc(sizeof(char *) * 3))) {
        perror("malloc");
        exit(1);
    }
   
    /* argv[0] */
    if (!(this->argv[0] = malloc(0x40))) {
        perror("malloc");
        exit(1);
    }
    printf("this->argv    = %p:%p\n", &this->argv, this->argv);             // 0x7fffd65d9de0:0x5604d873e2a0
    printf("this->argv[0] = %p:%p\n", &this->argv[0], this->argv[0]);       // 0x5604d873e2a0:0x5604d873e2c0

    memcpy(this->argv[0], "ac-test0", 8);
    printf("argv[0]=%s\n", this->argv[0]);
    
    /* argv[1] */
    if (!(this->argv[1] = malloc(0x40))) {
        perror("malloc");
        exit(1);
    }
    printf("this->argv[1] = %p:%p\n", &this->argv[1], this->argv[1]);      // 0x5564a05f42a8:0x5564a05f4720
    memcpy(this->argv[1], "ac-test1", 8);
    printf("argv[0]=%s\n", this->argv[1]);
    
    /* argv[2] */
    if (!(this->argv[2] = malloc(0x40))) {
        perror("malloc");
        exit(1);
    }
    printf("this->argv[2] = %p:%p\n", &this->argv[2], this->argv[2]);      // 0x5564a05f42b0:0x5564a05f4770
    memcpy(this->argv[2], "ac-test2", 8);
    printf("argv[0]=%s\n", this->argv[2]);
     
    /* argv[3] */
    if (!(this->argv[3] = malloc(0x40))) {
        perror("malloc");
        exit(1);
    }   
    printf("this->argv[3] = %p:%p\n", &this->argv[3], this->argv[3]);      // 0x5564a05f42b8:0x5564a05f47c0
    memcpy(this->argv[3], "ac-test3", 8);
    printf("argv[0]=%s\n", this->argv[3]);
    
       free(this->argv);
    free(this->argv[0]);
    free(this->argv[1]);
    free(this->argv[2]);
    free(this->argv[3]);
}

int main()
{
    PROC this = {0};
    
    test(&this);
    
    return 0;
}

点赞(0) 打赏

评论列表 共有 0 条评论

暂无评论

微信公众账号

微信扫一扫加关注

发表
评论
返回
顶部