信号:SIGABRT(中止)@realloc

信号:SIGABRT(中止)@realloc,c,realloc,C,Realloc,我试图给c字符串添加一个扩展名,但我刚刚得到信号:SIGABRT(中止),有人能告诉我这是什么原因吗?这就是我到目前为止所做的,错误来自函数“prepareFileName”中的@realloc: #包括 #包括 #包括 #定义\u内存中的\u 3 #定义文件扩展名“.txt” typedef enum\u Bool\u//enum\u Bool\u是一个非类型定义的枚举 { FALSE=0,//枚举元素 TRUE=1//枚举元素 }Bool;//Bool是类型定义的枚举 Bool cStrin

我试图给c字符串添加一个扩展名,但我刚刚得到信号:SIGABRT(中止),有人能告诉我这是什么原因吗?这就是我到目前为止所做的,错误来自函数“prepareFileName”中的@realloc:

#包括
#包括
#包括
#定义\u内存中的\u 3
#定义文件扩展名“.txt”
typedef enum\u Bool\u//enum\u Bool\u是一个非类型定义的枚举
{
FALSE=0,//枚举元素
TRUE=1//枚举元素
}Bool;//Bool是类型定义的枚举
Bool cStringEndsWith(常量字符*源字符串,常量字符*后缀){
if(!sourceString | |!suffix)//检查指针是否为非空
{
返回FALSE;
}
字符串的大小和长度=strlen(源字符串);
后缀的大小和长度=strlen(后缀);
if(后缀的长度>字符串的长度){
返回FALSE;
}
int compare_result=strncmp(源字符串+_c_字符串的长度_-_后缀的长度_,后缀,_后缀的长度_);
如果(比较结果==0){
返回TRUE;
}否则{
返回FALSE;
}
}
int prepareFileName(字符**ptr\u文件名){
int以文件扩展名=cStringEndsWith(*ptr文件名,文件扩展名)结尾;
如果(!以文件扩展名结尾)
{
char*new_ptr_file_name=realloc(*ptr_file_name,strlen(*ptr_file_name)+strlen(文件扩展名)+1);
如果(!新建文件名)
返回\u内存中的\u;
*ptr_file_name=新的ptr_file_name;
strcat(*ptr文件名、文件扩展名);
}
}
int main()
{
char*file_name=“testFileName”;
printf(“未准备的文件名:\%s\”,文件名);
prepareFileName(&文件名);
printf(“准备好的文件名:\%s\”,文件名);
返回0;
}

文件名
位于程序的文本段(只读)中。使用
malloc()
+
strcpy()
分配堆上的空间

参见
man realloc

除非ptr为NULL,否则它必须是由先前对的调用返回的 malloc()、calloc()或realloc()


文件名
位于程序的文本段(只读)中。使用
malloc()
+
strcpy()
分配堆上的空间

参见
man realloc

除非ptr为NULL,否则它必须是由先前对的调用返回的 malloc()、calloc()或realloc()


你是在试图修改
char*file\u name
可能的只读内存吗?哦,多亏了你的用户3121023,我的错,我不知道你是在试图修改
char*file\u name
可能的只读内存吗?哦,多亏了你的用户3121023,我的错,我不知道,谢谢,这就是答案(我必须等待8分钟,直到我可以接受它)没有问题。您也可以考虑使用<代码> ASPrTNF(…,“%s %s”,文件名,扩展名< /COD>)谢谢。答案(我必须等待8分钟直到我能接受它)没有问题。您也可以考虑使用<代码> ASPrTNF(…,“%s%s”,文件名,扩展)< /代码>。
#include <stdio.h>
#include <string.h>
#include <stdlib.h>


#define OUT_OF_MEMORY 3
#define FILE_EXTENSION ".txt"

typedef enum _Bool_ // enum _Bool_ is a non-typedef'ed enum
{
  FALSE = 0, // Enum element
  TRUE = 1 // Enum element
} Bool; // Bool is the typedef'ed enum

Bool cStringEndsWith(const char *sourceString, const char *suffix) {
  if (!sourceString || !suffix)  // Check for not null pointer
  {
    return FALSE;
  }
  size_t length_of_c_string = strlen(sourceString);
  size_t length_of_suffix = strlen(suffix);
  if (length_of_suffix > length_of_c_string) {
    return FALSE;
  }
  int compare_result = strncmp(sourceString + length_of_c_string - length_of_suffix, suffix, length_of_suffix);
  if (compare_result == 0) {
    return TRUE;
  } else {
    return FALSE;
  }
}

int prepareFileName(char **ptr_file_name){
  int ends_with_file_extension = cStringEndsWith(*ptr_file_name, FILE_EXTENSION);
  if(!ends_with_file_extension)
  {
    char *new_ptr_file_name = realloc(*ptr_file_name, strlen(*ptr_file_name) + strlen(FILE_EXTENSION) + 1);
    if(!new_ptr_file_name)
      return OUT_OF_MEMORY;
    *ptr_file_name = new_ptr_file_name;
    strcat(*ptr_file_name, FILE_EXTENSION);
  }
}

int main()
{
  char *file_name = "testFileName";
  printf("Filename unprepared: \"%s\"", file_name);
  prepareFileName(&file_name);
  printf("Filename prepared: \"%s\"", file_name);
  return 0;
}