我基本上试图从具有用户输入数据的结构中取消分配内存,因此目标是擦除数据,以便可以将新数据集重新输入到结构中。从结构中释放内存
这是结构
struct packet{
int source;
int destination;
int type; // Varibles for the structure
int port;
char data[50];
char * filename;
};
指针结构
struct packet s[50]; //Array for structure input
struct packet *p_s;
p_s = malloc(sizeof(struct packet));
其中用户输入的数据被添加到变量在结构
printf("\n****Adding a packet*****\n");
printf("Where is the packet from?\n");
scanf("%i", &s[NetworkPacket].source);
printf("Where is the packet going?\n");
scanf("%i", &s[NetworkPacket].destination);
printf("What type is the packet?\n");
scanf("%i", &s[NetworkPacket].type); // collecting the data of the packet inputted by the user
printf("What is the packet's port?\n");
scanf("%i", &s[NetworkPacket].port);
printf("Enter up to 50 characters of data.\n");
scanf("%s", s[NetworkPacket].data);
这是怎样的代码我查看结构数据
printf("\n%i\nSource: %d", ii, s[ii].source);
printf("\nDestination: %d", s[ii].destination);
printf("\nType : %d", s[ii].type);// if statement giving the limit of 50 packets allowed to be added
printf("\nPort : %d", s[ii].port);
printf("\nData: %s\n---\n", s[ii].data);
最后的代码我希望能帮助它从上面删除一组用户输入的数据,这样就可以添加一个新的集合。
system("cls");
free(p_s);
printf("All the data entered has been cleared. Press any key to continue");
一般来说,你只需要在''malloc''之前使用'free'。 – dreamlax
你只需要释放你拥有malloc的struct成员。 – moeCake