如何修复 strcat 函数中的分段错误?
How can I fix segmentation fault in strcat function?
我正在尝试将函数指针用作 C struct.I 的成员,其中包含 Identity、Person 和 RandomPeople types.My 程序以 "program has stopped working" 结尾。我用 gdb 调试了我的程序,我得到了以下输出。
[New Thread 18028.0x2c28]
[New Thread 18028.0x4150]
enter the number of people:2
Program received signal SIGSEGV, Segmentation fault.
0x754c5619 in strcat () from C:\WINDOWS\SysWOW64\msvcrt.dll
(在RandomPeople.h中可能是strcat
)
这是我的程序:
Identity struct
可以创建一个标识号并检查给定的标识号。
Identity.h 文件:
#ifndef Identity_H
#define Identity_H
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
struct IDNO {
char *(*CreateIDNo)(struct IDNO *);
};
typedef struct IDNO *Id;
Id CreateID();
char *CreateIDNo(Id this);
#endif
Identity.c 文件:
#include "Identity.h"
char *CreateIDNo(Id this) {
int str[11];
int totalodd = 0;
int totaleven = 0;
this->id = "";
for (int i = 1; i < 12; i++) {
if (i == 1) {
int n = 1 + rand() % 9;
totalodd += n;
str[i - 1] = n;
continue;
} else
if (i != 1 && i % 2 == 0 && i < 10) {
int n = rand() % 10;
totaleven += n;
str[i - 1] = n;
continue;
} else
if (i != 1 && i % 2 != 0 && i < 10) {
int n = rand() %10;
totalodd += n;
str[i - 1] = n;
continue;
} else
if (i == 10) {
int n11 = (7 * totalodd - totaleven) % 10;
str[i - 1] = n11;
continue;
} else
if (i == 11) {
int n12 = (totalodd + totaleven + str[9]) % 10;
str[i - 1] = n12;
continue;
}
}
for (int i = 0; i < 11; i++) {
char *b;
itoa(str[i], b, 10);
strcat(this->id, b);
}
return this->id;
}
每个 Person
都有 Identity
引用,可以使用此引用创建身份号码。
Person.h 文件:
#ifndef PERSON_H
#define PERSON_H
#include "Identity.h"
struct PERSON {
Id superid;
};
typedef struct PERSON *Person;
Person CreatePerson();
#endif
Person.c 文件:
#include "Person.h"
Person CreatePerson() {
Person this;
this = (Person)malloc(sizeof(struct PERSON));
this->superid = CreateID(); //Creating my reference
return this;
}
RandomPeople.c 文件:
#include "RandomPeople.h"
void CreateRandomPeopleData(RandomPeople k) {
Person person = CreatePerson();
char *formatted_identity = person->superid->CreateIDNo(person->superid);
strcat(data, formatted_identity);
}
test.c 文件
int main() {
RandomPeople rastgelekisiler = CreateRandomPeople();
rastgelekisiler->CreateRandomPeopleData(rastgelekisiler);
return 0;
}
编辑:我把这些结构放在我的问题上,因为分段问题可能出现在这些函数指针主体中。这些函数可能 return 可能指向错误位置或 null 的字符指针。我认为这个问题有最小的、完整的和可验证的例子。我不想被低估。
你在 Identity.c 中初始化 this->id
这样:this->id = "";
strcat(this->id, b)
将尝试写入字符串文字 ""
.
的只读存储,但失败
顺便说一句,b
在传递给 itoa(str[i], b, 10);
时未初始化,这是未定义行为的另一个计数。
我正在尝试将函数指针用作 C struct.I 的成员,其中包含 Identity、Person 和 RandomPeople types.My 程序以 "program has stopped working" 结尾。我用 gdb 调试了我的程序,我得到了以下输出。
[New Thread 18028.0x2c28]
[New Thread 18028.0x4150]
enter the number of people:2
Program received signal SIGSEGV, Segmentation fault.
0x754c5619 in strcat () from C:\WINDOWS\SysWOW64\msvcrt.dll
(在RandomPeople.h中可能是strcat
)
这是我的程序:
Identity struct
可以创建一个标识号并检查给定的标识号。
Identity.h 文件:
#ifndef Identity_H
#define Identity_H
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
struct IDNO {
char *(*CreateIDNo)(struct IDNO *);
};
typedef struct IDNO *Id;
Id CreateID();
char *CreateIDNo(Id this);
#endif
Identity.c 文件:
#include "Identity.h"
char *CreateIDNo(Id this) {
int str[11];
int totalodd = 0;
int totaleven = 0;
this->id = "";
for (int i = 1; i < 12; i++) {
if (i == 1) {
int n = 1 + rand() % 9;
totalodd += n;
str[i - 1] = n;
continue;
} else
if (i != 1 && i % 2 == 0 && i < 10) {
int n = rand() % 10;
totaleven += n;
str[i - 1] = n;
continue;
} else
if (i != 1 && i % 2 != 0 && i < 10) {
int n = rand() %10;
totalodd += n;
str[i - 1] = n;
continue;
} else
if (i == 10) {
int n11 = (7 * totalodd - totaleven) % 10;
str[i - 1] = n11;
continue;
} else
if (i == 11) {
int n12 = (totalodd + totaleven + str[9]) % 10;
str[i - 1] = n12;
continue;
}
}
for (int i = 0; i < 11; i++) {
char *b;
itoa(str[i], b, 10);
strcat(this->id, b);
}
return this->id;
}
每个 Person
都有 Identity
引用,可以使用此引用创建身份号码。
Person.h 文件:
#ifndef PERSON_H
#define PERSON_H
#include "Identity.h"
struct PERSON {
Id superid;
};
typedef struct PERSON *Person;
Person CreatePerson();
#endif
Person.c 文件:
#include "Person.h"
Person CreatePerson() {
Person this;
this = (Person)malloc(sizeof(struct PERSON));
this->superid = CreateID(); //Creating my reference
return this;
}
RandomPeople.c 文件:
#include "RandomPeople.h"
void CreateRandomPeopleData(RandomPeople k) {
Person person = CreatePerson();
char *formatted_identity = person->superid->CreateIDNo(person->superid);
strcat(data, formatted_identity);
}
test.c 文件
int main() {
RandomPeople rastgelekisiler = CreateRandomPeople();
rastgelekisiler->CreateRandomPeopleData(rastgelekisiler);
return 0;
}
编辑:我把这些结构放在我的问题上,因为分段问题可能出现在这些函数指针主体中。这些函数可能 return 可能指向错误位置或 null 的字符指针。我认为这个问题有最小的、完整的和可验证的例子。我不想被低估。
你在 Identity.c 中初始化 this->id
这样:this->id = "";
strcat(this->id, b)
将尝试写入字符串文字 ""
.
顺便说一句,b
在传递给 itoa(str[i], b, 10);
时未初始化,这是未定义行为的另一个计数。