如何释放子字符串?谢谢#include <stdio.h>#include <stdlib.h>#include <string.h>struct st_ex { char product[16]; float price;};struct st_temp { char *prod;};char *temp = NULL;// from stackoverflowchar* substr( const char* source, size_t start, size_t end ){ char* dest = malloc( end - start + 1) ; memcpy( dest, &source[start], end - start ) ; dest[end - start] = 0 ; return dest ;}int main(){ struct st_ex structs[] = {{"mp3 player", 2.0f}, {"plasma tv", 20.0f}, {"notebook", 10.0f}, {"smartphone", 49.9f}, {"dvd player", 10.0f}, {"matches", 0.2f }}; struct st_temp **temp_struct; size_t j, i; temp_struct = malloc(sizeof *temp_struct * 6); for (j = 0; j < 6; j++) temp_struct[j] = malloc(sizeof *temp_struct[j]); size_t structs_len = sizeof(structs) / sizeof(struct st_ex); for(i=0; i<structs_len; i++){ temp = substr(structs[i].product, 0, 4); temp_struct[i]->prod = temp; free(temp); temp = NULL; } for(i=0; i<6; i++ ) printf("%s\n",temp_struct[i]->prod); for(i=0; i<6; i++ ) free(temp_struct[i]); free(temp_struct); return 0;}解决方案 1)您正在释放子字符串 temp = substr(structs[i].product, 0, 4); temp_struct[i]->prod = temp; free(temp);上面第三行释放了您在substr中分配的内存. 2)因为您要释放此处的内存,所以引入了一个错误.释放它之后访问malloc的内存是无效的,因此尝试打印temp_struct[i]->prod是无效的.解决方案?不要free(temp),而是在释放temp_struct[i]的循环中,您首先需要释放temp_struct[i]->prod,像这样for(i=0; i<6; i++ ){ free(temp_struct[i]->prod); free(temp_struct[i]);}I'm trying to get a sub-string for each member of the struct 'structs' and then assign that sub-string to a new member of the temp_struct.The problem I'm having is how to free the sub-string on each iteration, for some reason the code runs, however valgrind throws an Invalid read of size 1, which I assume I'm reading off the block of memory.How could I free the sub-string?Thanks#include <stdio.h>#include <stdlib.h>#include <string.h>struct st_ex { char product[16]; float price;};struct st_temp { char *prod;};char *temp = NULL;// from stackoverflowchar* substr( const char* source, size_t start, size_t end ){ char* dest = malloc( end - start + 1) ; memcpy( dest, &source[start], end - start ) ; dest[end - start] = 0 ; return dest ;}int main(){ struct st_ex structs[] = {{"mp3 player", 2.0f}, {"plasma tv", 20.0f}, {"notebook", 10.0f}, {"smartphone", 49.9f}, {"dvd player", 10.0f}, {"matches", 0.2f }}; struct st_temp **temp_struct; size_t j, i; temp_struct = malloc(sizeof *temp_struct * 6); for (j = 0; j < 6; j++) temp_struct[j] = malloc(sizeof *temp_struct[j]); size_t structs_len = sizeof(structs) / sizeof(struct st_ex); for(i=0; i<structs_len; i++){ temp = substr(structs[i].product, 0, 4); temp_struct[i]->prod = temp; free(temp); temp = NULL; } for(i=0; i<6; i++ ) printf("%s\n",temp_struct[i]->prod); for(i=0; i<6; i++ ) free(temp_struct[i]); free(temp_struct); return 0;} 解决方案 1) You are freeing the substring temp = substr(structs[i].product, 0, 4); temp_struct[i]->prod = temp; free(temp);The third line above frees the memory you malloc'd in substr.2) because you're freeing the memory here, you've introduced a bug.It's invalid to access the malloc'd memory after you free it, therefore it's invalid to try to print temp_struct[i]->prod.The solution?Don't free(temp), instead in your loop to free temp_struct[i], you first need to free temp_struct[i]->prod, like thisfor(i=0; i<6; i++ ){ free(temp_struct[i]->prod); free(temp_struct[i]);} 这篇关于释放c循环中的子字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 07-23 00:36