【问题标题】:.exe crashes when I enter a vaue for &records[*rCount].source当我为 records[*Count].source 输入一个值时,.exe 崩溃
【发布时间】:2014-01-04 12:07:00
【问题描述】:

更新*

我现在尝试从函数中返回一些东西,但 .exe 仍然崩溃!我对 c 很陌生,如果我有点粗心没有发现原因,我很抱歉。

struct packet* addRecord(int *rCount, struct packet *records){
int valid = 0;  //used to indicated valid input
int length = 0; //used to store the string lengths
int i = 0;    //used in the for loops
char dataTest[51];     //temporary storage of input to be checked before adding to records




do{
    puts("What is the source of this packet?: ");
    if(scanf(" %c", &records[*rCount].source) == 1){  //if correct insert the record at the index
        valid=1;                                //determined by rCount(the current record count passed to addRecord
    }
    else{
        valid = 0;
        getchar();
        puts("\nNot a valid input");
    }

}while(valid!=1);

do{
    puts("What is the destination of this packet?: ");
    if(scanf(" %c", &records[*rCount].destination) == 1)
    {
        valid = 1;
    }
    else
    {
        valid = 1;
        getchar();
        puts("\nNot a valid input");
    }
   }
   while(valid!=1);
   records = realloc(records,(*rCount+1)*sizeof(struct packet));
   return records;

}

所以我已经让这段代码工作了,但是当我为 &records[*rCount].source 输入一个值时,.exe 崩溃了。我一直在看这段代码一个小时,找不到损坏的链接,但我觉得它很简单。

这是我觉得不能正常工作的一小段代码。

也有人可以解释一下 if 语句中 == 1 的含义,我只是将这段代码一起破解了。谢谢

do{
        puts("What is the source of this packet?: ");
        if(scanf("%i", &records[*rCount].source) == 1){  //if correct insert the record at the index
            valid=1;                                //determined by rCount(the current record count passed to addRecord
        }
        else{
            valid = 0;
            getchar();
            puts("\nNot a valid input");
        }

    }while(valid!=1);

完整代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

struct packet{ // declare structure for packet creation
        int source;
        int destination;
        int type;
        int port;
        char data[51];
    };

//function prototypes
void listRecords(int, struct packet*);
struct packet* addRecord(int*, struct packet*);
void save(int, struct packet*);
struct packet* open(int*, struct packet*);

int main ()
{
    int recordCount = 0;
    char choice;
    struct packet *records;
    struct packet *temp;

    do {
                printf("\nWhat would you like to do?\n");

                printf("\t1) Add a packet.\n");                 //---------------------//
                printf("\t2) List all packets.\n");             //---------------------//
                printf("\t3) Save packets.\n");                 //---------MENU--------//
                printf("\t4) Clear all packets.\n");            //---------------------//
                printf("\t5) Quit the programme.\n");           //---------------------//

                scanf("%i", &choice); // scan user input and put the entry into variable "choice"
                if(choice == '/n')
                    scanf("%i", &choice);



                switch(choice)
                {
                    case 1: system("cls");
                            records = addRecord(&recordCount, records);
                            break;
                    case 2: system("cls");
                            break;
                    case 3: system("cls");
                            break;
                    case 4: system("cls");
                            break;
                    default: system("cls");
                             printf("%i was not a valid option\n", choice);
                             break;
                }

            }
    while (choice != 5);
    return 0;
}

struct packet* addRecord(int *rCount, struct packet *records){
    int valid = 0;  //used to indicated valid input
    int length = 0; //used to store the string lengths
    int i = 0;    //used in the for loops
    char dataTest[51];     //temporary storage of input to be checked before adding to records




    do{
        puts("What is the source of this packet?: ");
        if(scanf("%i", &records[*rCount].source) == 1){  //if correct insert the record at the index
            valid=1;                                //determined by rCount(the current record count passed to addRecord
        }
        else{
            valid = 0;
            getchar();
            puts("\nNot a valid input");
        }

    }while(valid!=1);

    do{
        puts("What is the destination of this packet?: ");
        if(scanf("%i", &records[*rCount].destination == 1))
        {
            valid = 1;
        }
        else
        {
            valid = 1;
            getchar();
            puts("\nNot a valid input");
        }
       }
       while(valid!=1);
}

【问题讨论】:

    标签: c while-loop crash do-while


    【解决方案1】:

    改变

     if(scanf("%i", &records[*rCount].destination == 1))
    

     if(scanf("%d", &records[*rCount].destination) == 1)  
    

    还将%i 更改为%dchar choice; 更改为int choice; 另一个问题是你没有从你的函数中返回任何东西,它有指向 struct packet 返回类型的指针。

    我做了一些修改后的编译代码是:

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <ctype.h>
    
    struct packet{ // declare structure for packet creation
            int source;
            int destination;
            int type;
            int port;
            char data[51];
        };
    
    //function prototypes
    void listRecords(int, struct packet*);
    void addRecord(int*, struct packet*);
    void save(int, struct packet*);
    struct packet* open(int*, struct packet*);
    
    int main (void)
    {
        int recordCount = 0;
        int choice;
        struct packet *records;
        //struct packet *temp;
    
        do {
                    printf("\nWhat would you like to do?\n");
    
                    printf("\t1) Add a packet.\n");                 //---------------------//
                    printf("\t2) List all packets.\n");             //---------------------//
                    printf("\t3) Save packets.\n");                 //---------MENU--------//
                    printf("\t4) Clear all packets.\n");            //---------------------//
                    printf("\t5) Quit the programme.\n");           //---------------------//
    
                    scanf("%d", &choice); // scan user input and put the entry into variable "choice"
                    if(choice == '\n')
                        scanf("%d", &choice);
    
    
    
                    switch(choice)
                    {
                        case 1: system("cls");
                                 addRecord(&recordCount, records);
                                 break;
                        case 2: system("cls");
                                 break;
                        case 3: system("cls");
                                 break;
                        case 4: system("cls");
                                 break;
                        default: system("cls");
                                 printf("%d was not a valid option\n", choice);
                                 break;
                    }
    
                }
        while (choice != 5);
        return 0;
    }
    
    void addRecord(int *rCount, struct packet *records){
        int valid = 0;  //used to indicated valid input
        //int length = 0; //used to store the string lengths
        //int i = 0;    //used in the for loops
        //char dataTest[51];     //temporary storage of input to be checked before adding to records
    
    
    
    
        do{
            puts("What is the source of this packet?: ");
            if(scanf("%d", &records[*rCount].source) == 1){  //if correct insert the record at the index
                valid=1;                                //determined by rCount(the current     record count passed to addRecord
            }
            else{
                valid = 0;
                getchar();
                puts("\nNot a valid input");
            }
    
        }while(valid!=1);
    
        do{
            puts("What is the destination of this packet?: ");
            if(scanf("%d", &records[*rCount].destination) == 1)
            {
                valid = 1;
            }
            else
            {
                valid = 1;
                getchar();
                puts("\nNot a valid input");
            }
        }
           while(valid!=1);
    }
    

    【讨论】:

    • 这仍然会崩溃,尽管以后可能会对我有所帮助,谢谢!
    • 将 %i 更改为 %c 是否也意味着我会更改结构中目标类型的数据类型?
    • 没有。此更改仅适用于char 类型。无需更改目的地类型。我编辑了我的答案。有一些错字。现在修好了。这次它会正常工作。
    • 只需更新主帖,我是否按照您所说的缺少/需要编辑的内容进行操作。谢谢
    • 更新了我的答案。再看一遍。现在它对我有用,没有任何崩溃。
    【解决方案2】:
    struct packet *records;
    

    一切都很好,但您实际上从未为此指针创建一个 struct packet 指向。因此,通过这个指针的所有访问都是对不属于你的无效内存。

    我认为这里不需要指针。简单地声明为:

    struct packet records;
    

    然后传递一个指向该对象的指针:

    case 1: system("cls");
        addRecord(&recordCount, &records);
    

    请注意,我已经取消了 addRecord 的返回;你根本不需要它。让它返回void。就像现在一样,您正在使用一个无效指针并用 another 填充了随机性的无效指针覆盖它,因为您从来没有实际上 return 任何东西。也是同样的问题,只是碰巧因为你得到的随机值触发了崩溃。

    【讨论】:

    • 所以把返回记录:在函数的末尾应该清除这个。谢谢
    • 非常感谢,一切顺利!感谢上帝! :)
    • 仍然需要*记录或代码因某种原因无效,除非有解决办法,再次感谢您!
    • 不,事实上你不应该返回任何东西。您已经通过指针传递了结构;也无需将其退回。你确实需要声明实际的结构,而不仅仅是一个指针。 “代码因某种原因变得无效”并不能清楚地描述您所面临的问题,但目前,除非您按照我在回答中所说的去做,否则您仍然有一个您可能不知道的内存损坏问题。
    • 啊,抱歉,我对此很陌生,试图解释我面临的问题非常困难。我已经按照你说的做了,现在一切似乎都运行良好。谢谢
    【解决方案3】:

    %i 应该做什么?你在找一个整数吗?如果是这样,您需要 %dd 表示十进制)。

    == 1 检查 scanf 是否成功处理了 1 个项目。

    以及@hacks 所说的丢失)。

    【讨论】:

    • i 应该是一个整数,是的,它试图从用户输入中获取整数并将其存储到结构的“源”部分中。
    猜你喜欢
    • 2011-03-01
    • 2012-08-07
    • 2019-05-31
    • 2019-05-30
    • 1970-01-01
    • 2021-06-17
    • 1970-01-01
    • 1970-01-01
    • 2015-12-09
    相关资源
    最近更新 更多