W
Wang WolfLouis
#include "stdafx.h"
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
typedef struct {
char *data;
} pkt_buff_st;
typedef struct {
pkt_buff_st *pkt;
int isUsed;
} pkt_obj_st;
static pkt_obj_st *pkt_objs[5];
void pkt_obj_free(pkt_obj_st **pp)
{
pkt_obj_st *obj;
obj = *pp;
if (obj->pkt) {
free(obj->pkt->data);
free(obj->pkt);
obj->pkt = NULL;
}
free(*pp);
*pp = NULL;
}
void pkt_obj_print(void)
{
for (int i = 0; i < 5; i++) {
if (pkt_objs)
printf("pkt_objs[%d]: \n", i);
}
}
void set_pkt_obj(pkt_obj_st **set)
{
(*set) = NULL;
}
void pkt_buff_create(pkt_buff_st **one)
{
*one = (pkt_buff_st*)malloc(sizeof(pkt_buff_st));
}
int main(int argc, char* argv[])
{
pkt_buff_st *pkt01;
pkt_obj_st *obj01;
for (int j = 0; j < 5; j++) {
pkt_objs[j] = NULL;
}
pkt_buff_create(&pkt01);
pkt01->data = (char*)malloc(sizeof(char)*100);
memset(pkt01->data, '\0', 100);
obj01 = (pkt_obj_st*)malloc(sizeof(pkt_obj_st));
obj01->pkt = pkt01;
obj01->isUsed = 1;
for (int i = 0; i < 5; i++) {
pkt_objs = obj01;
}
pkt_obj_print();
pkt_obj_free(&pkt_objs[2]);
//set_pkt_obj(&pkt_objs[2]);
pkt_obj_print();
// free(obj01);
// obj01 = NULL;
printf("Hello World!\n");
return 0;
}
if I change *pp = NULL in
void pkt_obj_free(pkt_obj_st **pp) function to obj = NULL, then
the pkt_objs pointer array didn't remove pkt_objs[2] pointer. I don't
know why? can anybody explain the reason?
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
typedef struct {
char *data;
} pkt_buff_st;
typedef struct {
pkt_buff_st *pkt;
int isUsed;
} pkt_obj_st;
static pkt_obj_st *pkt_objs[5];
void pkt_obj_free(pkt_obj_st **pp)
{
pkt_obj_st *obj;
obj = *pp;
if (obj->pkt) {
free(obj->pkt->data);
free(obj->pkt);
obj->pkt = NULL;
}
free(*pp);
*pp = NULL;
}
void pkt_obj_print(void)
{
for (int i = 0; i < 5; i++) {
if (pkt_objs)
printf("pkt_objs[%d]: \n", i);
}
}
void set_pkt_obj(pkt_obj_st **set)
{
(*set) = NULL;
}
void pkt_buff_create(pkt_buff_st **one)
{
*one = (pkt_buff_st*)malloc(sizeof(pkt_buff_st));
}
int main(int argc, char* argv[])
{
pkt_buff_st *pkt01;
pkt_obj_st *obj01;
for (int j = 0; j < 5; j++) {
pkt_objs[j] = NULL;
}
pkt_buff_create(&pkt01);
pkt01->data = (char*)malloc(sizeof(char)*100);
memset(pkt01->data, '\0', 100);
obj01 = (pkt_obj_st*)malloc(sizeof(pkt_obj_st));
obj01->pkt = pkt01;
obj01->isUsed = 1;
for (int i = 0; i < 5; i++) {
pkt_objs = obj01;
}
pkt_obj_print();
pkt_obj_free(&pkt_objs[2]);
//set_pkt_obj(&pkt_objs[2]);
pkt_obj_print();
// free(obj01);
// obj01 = NULL;
printf("Hello World!\n");
return 0;
}
if I change *pp = NULL in
void pkt_obj_free(pkt_obj_st **pp) function to obj = NULL, then
the pkt_objs pointer array didn't remove pkt_objs[2] pointer. I don't
know why? can anybody explain the reason?