功能中的免费分配内存

发布于 2025-01-24 07:56:35 字数 1063 浏览 0 评论 0原文

我的功能是用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

鸢与 2025-01-31 07:56:35

您对tmp进行分配。这意味着指针tmp可能不再指向返回的malloc的位置。

您需要将相同的指针传递给免费,该指针由malloc返回。

您在找到的情况下遇到了同样的问题,您将其分配给它,并可能在指向的位置进行更改。

将无效的指针传递给免费导致不确定的行为


也有另一个问题:您脱离了分配的原始内存的界限,并通过tmp指向。那是因为您似乎已经忘记了C中的字符串确实被称为 null终止 strings

当您为字符串分配内存时,您需要在末尾包含空末端的空间。而且它不被strlen计算。

脱离分配的内存的范围也导致不确定的行为。

You do assignments to tmp. That means the pointer tmp might no longer point to the same location that malloc returned.

You need to pass the exact same pointer to free that was returned by malloc.

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.

像极了他 2025-01-31 07:56:35

该功能没有意义。

对于初学者,它会调用未定义的行为

char* tmp = malloc((strlen(input)) * sizeof(char));
char* found = malloc((strlen(input)) * sizeof(char)); 

tmp=strcpy(tmp, input);
//...

,因为您分配给足够的内存来存储字符串输入的终止零字符'\ 0'在字符数组tmp中。

其次,该函数具有内存泄漏,因为首先分配了内存,并将其地址分配给指针找到的指针,然后在的呼叫中重新分配了Pointer 。 for循环中的strstr 。

char* found = malloc((strlen(input)) * sizeof(char)); 

//...

// #pragma omp parallel for
for (int i=0; i<size; i++) {
    found = strstr(tmp, delim);
    //...  

因此,早期分配的内存的地址丢失了,并且无法释放内存。

for (int i=0; i<size; i++) {

是毫无意义的。

您不得致电免费 TMP也不适用于找到的。指针 并未指向动态分配的内存,并且指针tmp正在for循环中更改。

The function does not make a sense.

For starters it invokes undefined behavior

char* tmp = malloc((strlen(input)) * sizeof(char));
char* found = malloc((strlen(input)) * sizeof(char)); 

tmp=strcpy(tmp, input);
//...

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 pointer found was reassigned in the call of strstr in the for loop.

char* found = malloc((strlen(input)) * sizeof(char)); 

//...

// #pragma omp parallel for
for (int i=0; i<size; i++) {
    found = strstr(tmp, delim);
    //...  

So the address of the early allocated memory is lost and the memory can not be freed.

And this for loop

for (int i=0; i<size; i++) {

is just senseless.

You may not call free neither for tmp nor for found. The pointer found does not point to a dynamically allocated memory and the pointer tmp is being changed within the for loop.

哭泣的笑容 2025-01-31 07:56:35

这是我的新功能。我以递归模式写了它。

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int get_number_of_occurence(char* string, char* delim) {

    char* found = strstr(string, delim);
 
    if(found == NULL || strcmp(found,delim) ==0 ||found =="" ){
        
        return 0;

    }else{
      
        return 1+get_number_of_occurence(found+strlen(delim), delim);
      
    }
}

int split(char* input, char* delim, char** result,int array_idx) {

    char* found= strstr(input, delim);

    if (found==""||strlen(input)==0){
        return 0;

    }else if(found == NULL){
    
        result[array_idx]=malloc(strlen(input)+1 * sizeof(char));
        strncpy(result[array_idx], input, strlen(input));
        *(result[array_idx] + strlen(input)) = '\0';
        return 0;

    }else{

        int length = found - input;
        result[array_idx]=malloc((length+1) * sizeof(char));
        strncpy(result[array_idx], input, length);
        *(result[array_idx] + length) = '\0';
        return split(found+strlen(delim),delim,result,array_idx+1); 
    }
}
int main () {

char * delim = "ooo";
char * input="ssooonn";

int size = get_number_of_occurence(input, delim);
printf("size is : %d \n",size);

char *splitted_values[size+1];

split(input, delim,splitted_values,0);

for (int i=0; i<(size+1); i++) {
    printf("%s\n",splitted_values[i]);
    free(splitted_values[i]);
}

}

在此代码中,首先我计算定界符的发生次数。

然后,我在该大小中创建数组,并借助拆分功能填充。

感谢您的帮助。

here is my new function. i wrote it in recursive mode.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int get_number_of_occurence(char* string, char* delim) {

    char* found = strstr(string, delim);
 
    if(found == NULL || strcmp(found,delim) ==0 ||found =="" ){
        
        return 0;

    }else{
      
        return 1+get_number_of_occurence(found+strlen(delim), delim);
      
    }
}

int split(char* input, char* delim, char** result,int array_idx) {

    char* found= strstr(input, delim);

    if (found==""||strlen(input)==0){
        return 0;

    }else if(found == NULL){
    
        result[array_idx]=malloc(strlen(input)+1 * sizeof(char));
        strncpy(result[array_idx], input, strlen(input));
        *(result[array_idx] + strlen(input)) = '\0';
        return 0;

    }else{

        int length = found - input;
        result[array_idx]=malloc((length+1) * sizeof(char));
        strncpy(result[array_idx], input, length);
        *(result[array_idx] + length) = '\0';
        return split(found+strlen(delim),delim,result,array_idx+1); 
    }
}
int main () {

char * delim = "ooo";
char * input="ssooonn";

int size = get_number_of_occurence(input, delim);
printf("size is : %d \n",size);

char *splitted_values[size+1];

split(input, delim,splitted_values,0);

for (int i=0; i<(size+1); i++) {
    printf("%s\n",splitted_values[i]);
    free(splitted_values[i]);
}

}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文