我对C语言还很陌生,所以我不知道这里会发生什么.

在应该以面向对象的方式运行的String struct 的这个实现中,当我得到以下诊断时,set函数中似乎有一些错误:

Process finished with exit code -1073741819 (0xC0000005)

这是我的.h文件:

#include <string.h>
#include <stdbool.h>

typedef struct String_Struct
{
    char* value;
    unsigned int length;

    void (*set) (struct String_Struct* self, char* value);
    bool (*equals) (const struct String_Struct* self, const struct String_Struct* other);
    int (*compareTo) (const struct String_Struct* self, const struct String_Struct* other);
    void (*concat) (struct String_Struct* self, const struct String_Struct* other);
    void (*freeString) (struct String_Struct* self);
} String;

String* newString (char* value);
void set (String* self, char* value);
bool equals (const String* self, const String* other);
int compareTo (const String* self, const String* other);
void concat (String* self, const String* other);
void freeString (String* self);

以下是它的实现:

#include "mystring.h"
#include <stdlib.h>
#include <stdio.h>

void set (String* self, char* value)
{
    // Only proceed if self and value exist
    if (!self || !value)
    {
        return;
    }

    free(self->value);
    self->length = strlen(value) + 1;
    self->value = (char*) malloc(sizeof(char) * self->length);
    strcpy(self->value, value);
}

int compareTo (const String* self, const String* other)
{
    if ((!self && other) || (self && !other))
        return INT_MIN;

    return strcmp(self->value, other->value);
}

bool equals (const String* self, const String* other)
{
    if ((!self && other) || (self && !other))
        return false;

    if (self == other)
        return true;

    return strcmp(self->value, other->value) == 0;
}

void concat (String* self, const String* other)
{
    if (!self || !other)
    {
        return;
    }

    char* result = (char*) malloc(sizeof(char) * (strlen(self->value) + strlen(other->value) + 2));

    strcpy(result, self->value);
    strcat(result, other->value);

    self->set(self, result);
}

void freeString (String* self)
{
    free(self->value);
    free(self);
}

String* newString (char* value)
{
    String* str = (String*) malloc(sizeof(String));

    str->set = &set;
    str->equals = &equals;
    str->compareTo = &compareTo;
    str->concat = &concat;
    str->freeString = &freeString;

    str->set(str, value);

    return str;
}

这是我的主电源.

#include <stdio.h>
#include "mystring.h"

int main()
{
    String* string = newString("Hello");

    printf("%s", string->value);

    return 0;
}

当我运行这段代码时,我得到了上面提供的错误.但当我在"set"中添加一个不相关的printf语句时,如下所示:

void set (String* self, char* value)
{
    // Only proceed if self and value exist
    if (!self || !value)
    {
        return;
    }

    printf("Debug");

    free(self->value);
    self->length = strlen(value) + 1;
    self->value = (char*) malloc(sizeof(char) * self->length);
    strcpy(self->value, value);
}

以下是控制台输出: 调试Hello 进程已完成,退出代码为-1073741819(0xC0000005)

有谁能解释一下原因吗?

推荐答案

set函数中至少存在一个问题:

  free(self->value);                 //  you free a pointer that has 
  self->length = strlen(value) + 1;  //  never been initialized
                                    

释放从未初始化的指针是没有意义的,它会导致未定义的行为(大多数时候会发生某种崩溃).

您应该将该指针初始化为newString中的NULL:

  ...
  str->freeString = &freeString;
  str->value = NULL;  // <<< add this

  str->set(str, value);
  ...

经过修改后,您的代码似乎可以正常工作.我没有进一步调查,所以这段代码中的其他地方可能有更多的问题(错误和/或设计错误).

C++相关问答推荐

C如何显示字符串数组中的第一个字母

当main函数调用被重构时,C函数给出错误的结果

如何使用Python C API实现多线程程序?

为什么复合文字(C99)的返回会生成更多的汇编代码?

ZED for SDL上的C语言服务器

如何创建一个C程序来存储5种动物的名字,并在用户 Select 其中任何一种动物时打印内存地址?

如何在ASM中访问C struct 成员

GCC创建应用于移动项的单独位掩码的目的是什么?

这个C程序在工作中途停止获取输入.我收到分段故障(核心转储).我还是不知道问题出在哪里

==284==错误:AddressSaniizer:堆栈缓冲区下溢

当b是无符号字符时,int a=(b<;<;2)>;>;2;和int a=b&;0x3F;之间有什么区别?

在函数外部使用内联ASM时无法指定操作数

如何将大写/小写土耳其字母相互转换?

STM32 FATFS用户手册(Um1721)中的代码正确吗?

从整型转换为浮点型可能会改变其值.

与外部SPI闪存通信时是否应禁用中断?

为什么<到达*时不会转换为>?

添加/删除链表中的第一个元素

快速准确计算double的小数指数

无法在 C 中打开文本文件,我想从中读取文本作为数据并将其写入数组