[英]why does realloc crash my program when i am trying to allocate new memory?
i have some issue with my realloc function. 我的realloc函数有一些问题。 i have the struct Country and inside i have struct "City" wich include point to cities array that every city is have 3 fiels:
我有结构国家和内部我有结构“城市”,包括指向城市阵列,每个城市有3个fiels:
typedef struct Country {
char *name;
int numberOfCities;
City* cities;
cordinate cordinateOfCountryLeft;
cordinate cordinateOfCountryRight;
}Country;
typedef struct City
{
char *name;
char * popluarFood;
int numberOfPeople;
}City;
i need to delete city from the cities array so i free the city with function that i built called freeCity: 我需要从城市数组中删除城市,所以我释放了我建立的名为freeCity的功能的城市:
void freeCity(City *pCity)
{
free(pCity->name);
free(pCity->popluarFood);
free(pCity);
}
but after delelte when i am trying to realloc i get error in this function when realloc 但在删除后,当我尝试重新分配时,我在realloc时遇到此函数的错误
status freeCityFromCountry(Country *country, char *cityName)
{
for (int i = 0; i < country->numberOfCities; i++) {//for
if (strcmp(country->cities[i].name, cityName)==0)
{
freeCity(country->cities+i);
country->cities[i] = country->cities[country->numberOfCities - 1];
// free(country->cities[country->numberOfCities - 1]);
country->cities = (City*)realloc(country->cities,(country->numberOfCities-1));
country->numberOfCities--;
return success;
}
}//for
return failure;
}
i malloc country->cities in other function. 我在malloc country->其他功能的城市。 where can be the problem?
哪里可以问题? thank you
谢谢
You cannot call free
on a pointer to the middle of an allocated chunk. 你不能在指向分配块中间的指针上调用
free
。 country->cities
points to a contiguous block in memory ( N x sizeof(City)
). country->cities
指向内存中的连续块( N x sizeof(City)
)。 A good rule of thumb is, if you have malloc(something)
, you must have (and can only have) free(something)
somewhere else ( malloc(country->cities)
-> free(country->cities)
). 一个好的经验法则是,如果你有
malloc(something)
,你必须拥有(并且只能拥有) free(something)
地方的free(something)
( malloc(country->cities)
- > free(country->cities)
)。
The code crashes because the first call to freeCity
frees country->cities
. 代码崩溃是因为第一次调用
freeCity
释放freeCity
country->cities
。 Perhaps you want .cities
to be an array of pointers, ie City**
, in that case you would allocate each City
separately, and the cities
array would then point to chunks which you can free individually. 也许您希望
.cities
是一个指针数组,即City**
,在这种情况下,您将分别分配每个City
,然后cities
数组将指向您可以单独释放的块。
Assuming that you allocate country->cities
in another function. 假设您在另一个函数中分配
country->cities
。
You are calling freeCity(country->cities+i);
您正在呼叫
freeCity(country->cities+i);
But in the freeCity
function you are also freeing the city free(pCity);
但是在
freeCity
函数中你也可以free(pCity);
获得城市free(pCity);
So, for the cities
array, you are calling free on city[i]
. 所以,对于
cities
阵列,你在city[i]
上免费拨打电话。 This has two issues 这有两个问题
i
is 0
, you are freeing the array and then reallocating it. i
为0
,您释放数组然后重新分配它。 i
is non zero, you are freeing at the wrong place. i
非零时,您在错误的位置释放。 You should free the base of the array and not inside the array.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.