功能中的免费分配内存
我的功能是用C编写的功能,在此功能中,我将2个字符串分配为温度并找到,但是我无法释放温度字符串。 我认为这可能是由于在结果数组中使用温度。 任何人都可以帮助我吗? 这是功能。
void split(char* input, char* delim, char** result,int size) {
char* tmp = malloc((strlen(input)) * sizeof(char));
char* found = malloc((strlen(input)) * sizeof(char));
tmp=strcpy(tmp, input);
// #pragma omp parallel for
for (int i=0; i<size; i++) {
found = strstr(tmp, delim);
if (found != NULL) {
int length = found - tmp;
result[i]=malloc((length+1) * sizeof(char));
result[i] = strncpy(result[i], tmp, length);
*(result[i] + length) = '\0';
tmp = found + strlen(delim);
} else {
result[i]=malloc(strlen(tmp) * sizeof(char));
result[i] =strncpy(result[i], tmp, strlen(tmp));
}
}
// free(tmp);
free(found);
}
这是分开后的sub字符串数
当我删除此行的评论时, : //免费(TMP); 然后发生这个错误:
munmap_chunk(): invalid pointer
Aborted (core dumped)
我可以请您帮助我编写正确的拆分功能
i have function which is written in c ,in this function i allocate 2 string as temp and found, but i cant free temp string.
i think it may due to using of temp in result array.
can any one helps me.
here is the function.
void split(char* input, char* delim, char** result,int size) {
char* tmp = malloc((strlen(input)) * sizeof(char));
char* found = malloc((strlen(input)) * sizeof(char));
tmp=strcpy(tmp, input);
// #pragma omp parallel for
for (int i=0; i<size; i++) {
found = strstr(tmp, delim);
if (found != NULL) {
int length = found - tmp;
result[i]=malloc((length+1) * sizeof(char));
result[i] = strncpy(result[i], tmp, length);
*(result[i] + length) = '\0';
tmp = found + strlen(delim);
} else {
result[i]=malloc(strlen(tmp) * sizeof(char));
result[i] =strncpy(result[i], tmp, strlen(tmp));
}
}
// free(tmp);
free(found);
}
here size is number of sub strings after split
when i remove the comment of this line:
// free(tmp);
then this err occurs:
munmap_chunk(): invalid pointer
Aborted (core dumped)
can i ask you to help me for writing correct split function
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您对
tmp
进行分配。这意味着指针tmp
可能不再指向返回的malloc
的位置。您需要将相同的指针传递给
免费
,该指针由malloc
返回。您在
找到
的情况下遇到了同样的问题,您将其分配给它,并可能在指向的位置进行更改。将无效的指针传递给
免费
导致不确定的行为。您也有另一个问题:您脱离了分配的原始内存的界限,并通过
tmp
指向。那是因为您似乎已经忘记了C中的字符串确实被称为 null终止 strings 。当您为字符串分配内存时,您需要在末尾包含空末端的空间。而且它不被
strlen
计算。脱离分配的内存的范围也导致不确定的行为。
You do assignments to
tmp
. That means the pointertmp
might no longer point to the same location thatmalloc
returned.You need to pass the exact same pointer to
free
that was returned bymalloc
.You have the same problem with
found
, you assign to it and possible change where it points.Passing an invalid pointer to
free
leads to undefined behavior.You also have another problem: You go out of bounds of the original memory allocated and pointed to by
tmp
. That's because you seem to have forgotten that strings in C are really called null-terminated strings.When you allocate memory for a string, you need to include space for the null-terminator at the end. And it's not counted by
strlen
.Going out of bounds of allocated memory also leads to undefined behavior.
该功能没有意义。
对于初学者,它会调用未定义的行为
,因为您分配给足够的内存来存储字符串输入的终止零字符'\ 0'在字符数组
tmp
中。其次,该函数具有内存泄漏,因为首先分配了内存,并将其地址分配给指针找到的指针,然后在
的呼叫中重新分配了Pointer
。 for循环中的strstr 。因此,早期分配的内存的地址丢失了,并且无法释放内存。
这
是毫无意义的。
您不得致电
免费
TMP
也不适用于找到的
。指针 并未指向动态分配的内存,并且指针tmp
正在for循环中更改。The function does not make a sense.
For starters it invokes undefined behavior
because you allocated to enough memory to store the terminating zero character '\0' of the string input in the character array
tmp
.Secondly the function has a memory leak because at first memory was allocated and its address was assigned to the pointer
found
and then the pointerfound
was reassigned in the call ofstrstr
in the for loop.So the address of the early allocated memory is lost and the memory can not be freed.
And this for loop
is just senseless.
You may not call
free
neither fortmp
nor forfound
. The pointerfound
does not point to a dynamically allocated memory and the pointertmp
is being changed within the for loop.这是我的新功能。我以递归模式写了它。
在此代码中,首先我计算定界符的发生次数。
然后,我在该大小中创建数组,并借助拆分功能填充。
感谢您的帮助。
here is my new function. i wrote it in recursive mode.
in this code, first i count the number of occurrence of delimiter.
then i create array in that size and fill it with the help of split function.
thanks for helping.