gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 14:53:35 25 4
gpt4 key购买 nike

有人可以向我解释一下我下面的代码有什么问题吗?据我了解,我需要释放为 char* 分配的内存 - 否则它会泄漏。但是,如果我使用(在程序的最后 free()),我会得到:malloc:* 对象 0x7ff7274027a4 的错误:未分配正在释放的指针*在malloc_error_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);
}

最佳答案

该错误是因为您只能将 malloc()/calloc()/返回的指针传递给 free() 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() 返回的内存。

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

我还认为你应该重新考虑以下陈述

without "free()" this code runs just fine and gives me what I need.

代码并不总是给你你需要的,因为它有严重的错误,会导致未定义的行为,所以它不可能无论如何预测结果。

如果您仔细阅读以下示例中使用的函数的文档,您就会正确地执行以下代码

#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;
}

关于c - 释放 C 中为 char* 分配的内存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47148660/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com