释放C中为char*分配的内存

释放C中为char*分配的内存,c,memory-management,C,Memory Management,请有人给我解释一下,我下面的代码出了什么问题? 据我所知,我需要释放分配给char*的内存,否则它会泄漏内存。但是,如果我使用(在程序free()的末尾),我会得到: malloc:*对象0x7ff7274027a4的错误:未分配要释放的指针 *在malloc\u error\u break中设置断点以进行调试 中止陷阱:6 如果没有“free()”,这段代码运行得很好,可以满足我的需要。 提前谢谢你 #include <stdio.h> #include <strin

请有人给我解释一下,我下面的代码出了什么问题? 据我所知,我需要释放分配给char*的内存,否则它会泄漏内存。但是,如果我使用(在程序free()的末尾),我会得到: malloc:*对象0x7ff7274027a4的错误:未分配要释放的指针 *在malloc\u error\u break中设置断点以进行调试 中止陷阱:6

如果没有“free()”,这段代码运行得很好,可以满足我的需要。 提前谢谢你

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

  int main ()
 {
    char *src="start=3.6307436653491267,2.2931731236906048,0";
    char *psrc;
    psrc=calloc((strlen(src)+1),sizeof(char));
    strcpy(psrc,src);
    size_t len= strlen(src);
    len +=1;
    char str[len];
    int count=0;
    char *wt=calloc(3,sizeof(char));
    char *ft=calloc(17, sizeof(char));
    char *finalstr=calloc((strlen(wt) + strlen(ft) + 1), sizeof(char));
    while (psrc != NULL && *psrc != '\0' && count <= len)
    {
        if(psrc[0] == '=')
        {
            psrc +=1;
            strcpy(str, psrc);
            wt=strtok(str, ".");
            ft=strtok(NULL, ".");
            ft=strtok(ft, ",");
            strcpy(finalstr, wt);
            strcat(finalstr, ".");
            strncat(finalstr, ft, 2);
        }
        ++psrc;
        ++count;
   }
   printf("finalstr: %s\n", finalstr);
   //free(psrc);
   exit(EXIT_SUCCESS);
}
#包括
#包括
#包括
int main()
{
char*src=“start=3.6307436653491267,2.2931731236906048,0”;
char*psrc;
psrc=calloc((strlen(src)+1),sizeof(char));
strcpy(psrc,src);
尺寸长度=标准长度(src);
len+=1;
char-str[len];
整数计数=0;
char*wt=calloc(3,sizeof(char));
char*ft=calloc(17,sizeof(char));
char*finalstr=calloc((strlen(wt)+strlen(ft)+1),sizeof(char));

虽然(psrc!=NULL&&*psrc!='\0'&&count
psrc
不再指向其原始地址。您可能希望在释放内存之前添加
psrc-=count;

错误是因为您只能将
free()
malloc()返回的指针传递给
malloc()
calloc()
/
realloc()
并且您正在传递递增的
psrc

比如说,

char *string = malloc(SIZE);
free(string + 1);
如果是未定义的行为,则调用
free()
的唯一可接受且正确的方法是

free(string);
你有

psrc += 1;
它更改了
psrc
的值,并且不再指向最初由
calloc()返回的地址

虽然如果您担心内存泄漏,您需要了解它们

在你的代码中,你有

char *wt = calloc(3, sizeof(char));
然后呢,

wt = strtok(str, ".");
这正是内存泄漏:因为您现在无法
free()
先前由
calloc()返回的内存

在继续之前,您需要研究指针,当您的代码中有显式内存泄漏但您不知道时,担心内存泄漏是没有多大意义的

我还认为,你应该重新考虑以下声明

如果没有“free()”,这段代码运行得很好,可以满足我的需要

代码不会总是提供您所需要的,因为它有严重的bug,会导致未定义的行为,因此无论如何都无法预测结果

如果您仔细阅读下面示例中使用的函数的文档,您将更正以下代码

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

int main(void)
{
    const char *src = "start=3.6307436653491267,2.2931731236906048,0";
    const char *equal;
    char *endptr;
    float value;
    int length;
    char *result;

    // Find the equal sign, if not found the function
    // returns and no output is shown
    equal = strchr(src, '=');
    if (equal == NULL)
        return -1;
    // Convert the string to a `float' starting
    // at the `=' sign and up to the `,'
    value = strtof(equal + 1, &endptr);
    // If the first non numeric character wasn't a `,'
    // then this input string is not as we expected it to
    // be so we abort the program.
    if (*endptr != ',')
        return -1;
    // Create a string from the number, with just 2
    // decimal places.

    // Compute the length of the resulting string
    length = snprintf(NULL, 0, "%.2f", value);
    if (length == -1)
        return -1;
    // Allocate space for it
    result = malloc(length + 1);
    if (result == NULL)
        return -1;
    // Finally "print" the number into the string
    snprintf(result, length, "%.2f", value);
    // Now we have the string we wanted
    fprintf(stdout, "%s\n", result);
    // After we've done our work with `result' then we
    // have to `free()' it
    free(result);
    return 0;
}
#包括
#包括
#包括
内部主(空)
{
const char*src=“start=3.6307436653491267,2.2931731236906048,0”;
常量字符*相等;
char*endptr;
浮动值;
整数长度;
字符*结果;
//如果未找到函数,则查找等号
//返回,不显示输出
相等=strchr(src,'=');
如果(相等==NULL)
返回-1;
//将字符串转换为“float”开头
//在“=”符号处,向上到“,”
值=strof(等于+1,&endptr);
//如果第一个非数字字符不是“,”
//那么这个输入字符串就不是我们期望的那样了
//所以我们中止这个计划。
如果(*endptr!=',')
返回-1;
//从数字创建一个字符串,仅使用2
//小数位。
//计算结果字符串的长度
长度=snprintf(空,0,“%.2f”,值);
如果(长度==-1)
返回-1;
//为它分配空间
结果=malloc(长度+1);
如果(结果==NULL)
返回-1;
//最后,将数字“打印”到字符串中
snprintf(结果,长度,“%.2f”,值);
//现在我们有了我们想要的绳子
fprintf(标准输出,“%s\n”,结果);
//在我们用“结果”完成工作之后,我们
//必须“释放”它
自由(结果);
返回0;
}

谢谢。我知道。但我的问题不同。不,你应该解决所有这些问题。你的代码有未定义的行为,无论你是否
free()
在这一点上都是无关的。你必须释放
malloc
等返回的确切指针。在你的情况下,你通过添加到它来修改指针。你
malloc()
ft
wt
等,然后重新分配它们,这正是内存泄漏。你写的内容超出了
finalstr
(大小为1)所指向的块的范围,这是正确的想法,尽管
count
不是按OK移动的正确数量。我因为说“回答”而受到批评没有提供答案。好的。我因为说“答案”而没有提供答案而受到批评。所以,这是我糟糕的C代码的另一个版本。你能表达你的观点吗?发布需要的东西有点困难…:(char*truncnum(const char*original,unsigned int precision){char*psrc=calloc((strlen(original)+1),sizeof(char));strcpy(psrc,original);char*wt=calloc(10,sizeof(char));char*ft=calloc(精度+1,sizeof(char));char*finalstr=calloc((strlen(wt)+strlen(ft)+1,sizeof(char));无符号整数计数=0;无符号整数计数2=0;而(psrc!=NULL&&*psrc!='\0'&&count谢谢你。我感谢你的努力,但是,我特别避免将该字符串中的“数字”视为数字(无论是浮点、双精度还是其他)。我希望它们是字符串,这就是为什么所有这些都与指向char的指针一起跳舞。使用
snprintf()
带数字。再次感谢。我对数字作为数字的集合不感兴趣。这只是程序的一小部分,它读取文本文件并形成ngc文件(用于数控机床)。