Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
试图修复C程序中的分段错误_C - Fatal编程技术网

试图修复C程序中的分段错误

试图修复C程序中的分段错误,c,C,我是一名学生,我得到了这个项目,我已经工作了一周。每次我运行这个程序时,我都会遇到这个分割错误问题,我改变并尝试了几乎所有的方法,在互联网上读了很多书,但没有任何帮助。我一直在尝试解决这个问题,有没有调试器 我想这是我在eurovisionAddState()中分配内存的方式,但我更改了很多次,没有任何效果 这是我的节目: #include <stdio.h> #include <stdlib.h> #include <assert.h> #include &

我是一名学生,我得到了这个项目,我已经工作了一周。每次我运行这个程序时,我都会遇到这个分割错误问题,我改变并尝试了几乎所有的方法,在互联网上读了很多书,但没有任何帮助。我一直在尝试解决这个问题,有没有调试器

我想这是我在
eurovisionAddState()
中分配内存的方式,但我更改了很多次,没有任何效果

这是我的节目:

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

#define NUM_OF_JUDGE_VOTES 10
#define NOT_FOUND -1

typedef enum eurovisionResult_t {
    EUROVISION_NULL_ARGUMENT,
    EUROVISION_OUT_OF_MEMORY,
    EUROVISION_INVALID_ID,
    EUROVISION_INVALID_NAME,
    EUROVISION_STATE_ALREADY_EXIST,
    EUROVISION_STATE_NOT_EXIST,
    EUROVISION_JUDGE_ALREADY_EXIST,
    EUROVISION_JUDGE_NOT_EXIST,
    EUROVISION_SAME_STATE,
    EUROVISION_SUCCESS
} EurovisionResult;

//Structs:
struct country_t {
    int *ID;
    char *CountryName;
    char *SongName;
};

typedef struct country_t *Country;

//Nodes
typedef struct CountryNode_t {
    Country data;
    struct CountryNode_t *next;
    struct CountryNode_t *before;
} *CountryNode;

typedef struct eurovision_t {
    CountryNode Countries;
} *Eurovision;

//========
//Functions:
Eurovision eurovisionCreate() {
    Eurovision euro = (Eurovision)malloc(sizeof(*euro));
    euro->Countries = (CountryNode)malloc(sizeof(struct CountryNode_t));
    return euro;
}

static Eurovision setupEurovision() {
    Eurovision eurovision = eurovisionCreate();
    assert(eurovision);
    return eurovision;
}

CountryNode countryGetFirst(CountryNode cn) {
    while (cn->before) {
        cn = cn->before;
    }
    return cn;
}

bool countryNodeExists(CountryNode c, int ID) //Returns TRUE if country with the given ID exists,
{                                            // and FALSE if country with the given ID doesn't exist
    CountryNode cn = countryGetFirst(c);
    while (cn) {
        if (*(cn->data->ID) == ID) {
            return true;
        }
        cn = cn->next;
    }
    return false;
}

int countryNodeSize(CountryNode countryNode) //Returns the amount of countries in countryNode
{
    CountryNode cn = countryGetFirst(countryNode);
    int size = 0;

    while (cn) {
        size++;
        cn = cn->next;
    }
    return size;
}

void countryNodePut(CountryNode countryNode,Country country) //Puts country inside the correct
{                                                            //place (via ID comparison) in countryNode
    //if country is first
    if (countryNodeSize(countryNode) == 0) {
        countryNode = (CountryNode)malloc(sizeof(struct CountryNode_t));
        countryNode->before = NULL;
        countryNode->next = NULL;
        countryNode->data = country;
        return;
    }

    CountryNode new_country_node = (CountryNode)malloc(sizeof(struct CountryNode_t));
    new_country_node->data = country;

    //If ID is before First
    CountryNode first = countryGetFirst(countryNode);
    if (*(first->data->ID) > *(country->ID)) {
        new_country_node->next = first;
        new_country_node->before = NULL;
        first->before = new_country_node;
        return;
    }

    //check if the country exists, and replace the data
    if (countryNodeExists(countryNode, *(country->ID))) {
        CountryNode cn = countryGetFirst(countryNode);
        while (cn) {
            if (*(cn->data->ID) == *(country->ID)) {
                cn->data = country;
                return;
            }
            cn = cn->next;
        }
    }

    //place it in its place
    CountryNode cn = countryGetFirst(countryNode);
    while (cn->next) { //cn->next so we wouldnt try to read from a null
        if (*(cn->data->ID) < *(country->ID) && *(cn->next->data->ID) > *(country->ID)) {
            cn->next->before = new_country_node;
            new_country_node->before = cn;
            new_country_node->next = cn->next;
            cn->next = new_country_node;
            return;
        }
    }

    //got here if countryNode should be last
    cn->next = new_country_node;
    new_country_node->before = cn;
}

bool checkInvalidName(const char *name) {
    int i = 0;
    while (*(name + i) != '\0') {
        if ((*(name + i) < 'a' || *(name + i) > 'z') && *(name + i) != ' ')
            return true;
        i++;
    }
    return false;
}

EurovisionResult eurovisionAddState(Eurovision eurovision, int stateId,
                                    const char *stateName,
                                    const char *songName)
{   //CHECK IF stateName IS VALID
    if (checkInvalidName(stateName))
        return EUROVISION_INVALID_NAME;
    //----
    //CHECK IF stateId IS POSITIVE
    if (stateId < 0)
        return EUROVISION_INVALID_ID;
    //----
    //CHECK IF THE SAME STATE EXIST
    if (countryNodeExists(eurovision->Countries, stateId))
        return EUROVISION_STATE_ALREADY_EXIST;
    //----

    Country state = (Country)malloc(sizeof(struct country_t));
    if (!state) {
        return EUROVISION_OUT_OF_MEMORY;
    }

    state->ID = (int *)malloc(sizeof(int));
    *(state->ID) = stateId;
    state->CountryName = (char*)malloc(sizeof(char) * strlen(stateName) + 1);
    strcpy(state->CountryName, stateName);
    state->SongName = (char *)malloc(sizeof(char) * strlen(songName) + 1);
    strcpy(state->SongName, songName);

    countryNodePut(eurovision->Countries, state);

    /*  //TEST - DELETE THIS
    Country stateNew = countryNodeGet(eurovision->Countries, stateId);
    printf("eurovisionAddState: after mapPut-Cname=%s,Sname=%s,id=%d\n",(stateNew->CountryName), (stateNew->SongName), *(stateNew->ID));
    //----*/

    return EUROVISION_SUCCESS;
}

int main() {
    printf("Starting Test\n");
    Eurovision eurovision = setupEurovision();
    printf("After setupEurovision()\n");
    eurovisionAddState(eurovision, 1, "malta", "chameleon");
    printf("After eurovisionAddState of Malta\n");
    eurovisionAddState(eurovision, 2, "croatia", "the dream");
    eurovisionAddState(eurovision, 3, "russia", "scream");
    eurovisionAddState(eurovision, 4, "moldova", "stay");
    eurovisionAddState(eurovision, 5, "cyprus", "replay");
    eurovisionAddState(eurovision, 6, "spain", "la venda");
    eurovisionAddState(eurovision, 7, "italy", "soldi");
    eurovisionAddState(eurovision, 8, "france", "roi");
    eurovisionAddState(eurovision, 9, "germany", "sister");
    eurovisionAddState(eurovision, 10, "united kingdom", "bigger than us");
    eurovisionAddState(eurovision, 11, "armenia", "walking out");
    eurovisionAddState(eurovision, 12, "austria", "limits");
    eurovisionAddState(eurovision, 13, "ireland", "twenty two");
    eurovisionAddState(eurovision, 14, "netherlands", "arcade");
    eurovisionAddState(eurovision, 15, "sweden", "too late for love");
    return 0;
}
#包括
#包括
#包括
#包括
#包括
#定义法官投票数10
#未找到定义-1
类型定义枚举eurovisionResult\t{
EUROVISION_NULL_参数,
EUROVISION内存不足,
EUROVISION\u无效\u ID,
EUROVISION_名称无效,
欧洲视觉国家已经存在,
欧洲视觉国家不存在,
欧洲电视台已经存在,
欧洲电视台法官不存在,
同一州的欧洲视觉,
欧洲电视台的成功
}欧洲视觉结果;
//结构:
结构国家/地区{
int*ID;
字符*国家名称;
char*SongName;
};
typedef结构国家/地区*country;
//节点
类型定义结构CountryNode\u t{
国家数据;
结构CountryNode\u t*next;
结构CountryNode\u t*before;
}*国家节点;
类型定义结构eurovision\u t{
国家节点国家;
}*欧洲电视台;
//========
//职能:
Eurovision Eurovision创建(){
Eurovision euro=(Eurovision)malloc(sizeof(*欧元));
欧元->国家=(CountryNode)malloc(sizeof(struct CountryNode_t));
回归欧元;
}
静态Eurovision设置Eurovision(){
Eurovision Eurovision=Eurovision创建();
assert(欧洲电视台);
返回欧洲视觉;
}
CountryNode countryGetFirst(CountryNode cn){
while(cn->before){
cn=cn->before;
}
返回cn;
}
bool countryNodeExists(CountryNode c,int ID)//如果具有给定ID的国家/地区存在,则返回TRUE,
{//如果具有给定ID的国家/地区不存在,则为FALSE
CountryNode cn=countryGetFirst(c);
while(中国){
如果(*(cn->数据->ID)==ID){
返回true;
}
cn=cn->next;
}
返回false;
}
int countryNodeSize(CountryNode CountryNode)//返回CountryNode中的国家数量
{
CountryNode cn=countryGetFirst(CountryNode);
int size=0;
while(中国){
大小++;
cn=cn->next;
}
返回大小;
}
void countryNodePut(CountryNode CountryNode,Country Country)//将Country放入正确的
{//countryNode中的位置(通过ID比较)
//如果国家第一
如果(countryNodeSize(countryNode)==0){
countryNode=(countryNode)malloc(sizeof(struct countryNode_t));
countryNode->before=NULL;
countryNode->next=NULL;
countryNode->data=国家;
返回;
}
CountryNode new_country_node=(CountryNode)malloc(sizeof(struct CountryNode_t));
新建国家/地区节点->数据=国家/地区;
//如果ID在第一个之前
CountryNode first=countryGetFirst(CountryNode);
如果(*(第一->数据->ID)>*(国家->ID)){
新建国家/地区节点->下一步=第一步;
新建国家/地区节点->之前=空;
第一个->之前=新建国家/地区节点;
返回;
}
//检查国家/地区是否存在,并替换数据
如果(countryNodeExists(countryNode,*(国家->ID))){
CountryNode cn=countryGetFirst(CountryNode);
while(中国){
如果(*(中国->数据->ID)=*(国家->ID)){
cn->数据=国家;
返回;
}
cn=cn->next;
}
}
//把它放回原处
CountryNode cn=countryGetFirst(CountryNode);
而(cn->next){//cn->next,所以我们不会尝试从null中读取
如果(*(cn->data->ID)<*(国家->ID)&&*(cn->next->data->ID)>*(国家->ID)){
cn->next->before=新建国家/地区节点;
新建国家/地区节点->之前=cn;
新建国家/地区节点->下一步=中国->下一步;
cn->next=新建国家/地区节点;
返回;
}
}
//如果countryNode是最后一个
cn->next=新建国家/地区节点;
新建国家/地区节点->之前=cn;
}
bool checkInvalidName(常量字符*名称){
int i=0;
而(*(名称+i)!='\0'){
如果(*(名称+i)<'a'| |*(名称+i)>'z')&*(名称+i)!='')
返回true;
i++;
}
返回false;
}
EurovisionResult eurovisionAddState(Eurovision Eurovision,int stateId,
const char*stateName,
常量字符*songName)
{//检查stateName是否有效
如果(检查InvalidName(stateName))
返回EUROVISION\u无效的\u名称;
//----
//检查stateId是否为正
if(stateId<0)
返回EUROVISION\u无效\u ID;
//----
//检查是否存在相同的状态
if(countryNodeExists(eurovision->Countries,stateId))
返回已存在的EUROVISION状态;
//----
国家/地区=(国家)malloc(结构国家/地区的规模);
如果(!状态){
将EUROVISION_从_存储器中返回;
}
state->ID=(int*)malloc(sizeof(int));
*(状态->ID)=状态ID;
state->CountryName=(char*)malloc(sizeof(char)*strlen(stateName)+1);
strcpy(state->CountryName,stateName);
state->SongName=(char*)malloc(sizeof(char)*strlen(SongName)+1);
strcpy(state->SongName,SongName);
countryNodePut(欧洲视觉->国家,州);
/*//测试-删除此
Country stateNew=countryNodeGet(eurovision->Countries,stateId);
printf(“eurovisionAddState:映射后Cname=%s,Sname=%s,id=%d\n”,(stateNew->CountryName),(stateNew->SongName),*(stateNew->id));
//----*/
回报欧洲电视大学的成功;
}
int main(){
printf(“开始测试”);
Eurovision Eurovision=设置Eurovision();
printf(“在设置Eurovision()\n之后”);
eurovisionAddState(eurovision,1,“马耳他”,“变色龙”);
printf(“在欧洲视觉数据发布日期之后
while (cn->before)
* thread #1, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=EXC_I386_GPFLT)
  * frame #0: 0x000000010000064c DeleteThis`countryGetFirst(cn=0x8b480850ff078b48) at main.c:65
    frame #1: 0x0000000100000688 DeleteThis`countryNodeExists(c=0x0000000102905f60, ID=1) at main.c:74
    frame #2: 0x0000000100000a1e DeleteThis`eurovisionAddState(eurovision=0x0000000102908750, stateId=1, stateName="malta", songName="chameleon") at main.c:182
    frame #3: 0x0000000100000b76 DeleteThis`main at main.c:212
    frame #4: 0x00007fff617253d5 libdyld.dylib`start + 1
    frame #5: 0x00007fff617253d5 libdyld.dylib`start + 1
Eurovision eurovision = setupEurovision();
Eurovision euro=(Eurovision)malloc(sizeof(*euro));
euro->Countries=(CountryNode)malloc(sizeof(struct CountryNode_t));
return euro;
eurovisionAddState(eurovision, 1, "malta", "chameleon");
if(countryNodeExists(eurovision->Countries,stateId))
CountryNode cn = countryGetFirst(c);
while (cn->before)
(CountryNode)malloc(sizeof(struct CountryNode_t));
cn = cn->before;
while (cn->before)