简单的C练习。最后一个位置被覆盖

时间:2014-01-09 20:35:15

标签: c malloc realloc

这是我编辑代码的一整天,但无法找到我的数组的最后一个位置被覆盖的原因!!

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

typedef struct  Person{
    char *name;
    char *surname;
    char *address;
    char *number;
} Person;

char * getString(void);

int main(void) {
    struct Person *rub = NULL, *ttmp = NULL;
    int idx=0,i,j,k;
    char c;
    char *tmp = NULL;
    do{
        printf("*******************************\ni - Insert new\n");
        printf("n - Find by name\n");
        printf("c - Find by surname\n");
        printf("e - Delete\n");
        printf("p - Print list\n");
        printf("0 - Exit\n*******************************\n");
        c = getchar();
        printf("%d\n",c);
        getchar();
        switch(c){
            case 'i':
                ttmp = (struct Person *) realloc(rub, (idx+1)*sizeof(Person));
                if(ttmp == NULL){
                    printf("Cannot allocate more memory.\n");
                    exit(1);
                }
                else
                    rub = ttmp;
                printf("Nome: ");
                tmp = getString();
                rub[idx].name = (char *) malloc(strlen(tmp));
                rub[idx].name = tmp;
                //printf("Surname: "); //commented in order to test faster
                //tmp = getString();
                rub[idx].surname = (char *) malloc(strlen(tmp));
                rub[idx].surname = tmp;
                //printf("Address: ");
                //tmp = getString();
                rub[idx].address = (char *) malloc(strlen(tmp));
                rub[idx].address = tmp;
                //printf("Number: ");
                //tmp = getString();
                rub[idx].number = (char *) malloc(strlen(tmp));
                rub[idx].number = tmp;
                idx++;
                for(k=0;k<idx;k++){
                    printf("%d) %s %s\n%s\n%s\n-------------------\n", k+1, rub[k].name,rub[k].surname,rub[k].address,rub[k].number);
                }
                break;
            case 'n':
                printf("What name are you looking for? ");
                scanf("%s",tmp);
                for(k=0;k<idx;k++){
                    if(strcmp(rub[k].name,tmp) == 0){
                        printf("%s\n%s\n%s\n%s\n", rub[k].name,rub[k].surname,rub[k].address,rub[k].number);
                    }
                }
                break;
            case 'c':
                printf("What surname are you looking for? ");
                scanf("%s",tmp);
                for(k=0;k<idx;k++){
                    if(strcmp(rub[k].surname,tmp) == 0){
                        printf("%s\n%s\n%s\n%s\n", rub[k].name,rub[k].surname,rub[k].address,rub[k].number);
                    }
                }
                break;
            case 'e':
                printf("Select number to delete record:\n ");
                for(k=0;k<idx;k++){
                    printf("%d) %s %s\n", k+1, rub[k].name,rub[k].surname);
                }
                scanf("%d",&j);
                ttmp = NULL;
                for(k=0,i=0;k<idx;k++){
                    if(k+1 != j){
                        ttmp = (struct Person *) realloc(ttmp, (i+1)*sizeof(Person));
                        ttmp[i].name = (char *) malloc(strlen(rub[k].name));
                        ttmp[i].surname = (char *) malloc(strlen(rub[k].surname));
                        ttmp[i].address = (char *) malloc(strlen(rub[k].address));
                        ttmp[i].number = (char *) malloc(strlen(rub[k].number));
                        ttmp[i].name = rub[k].name;
                        ttmp[i].surname = rub[k].surname;
                        ttmp[i].address = rub[k].address;
                        ttmp[i].number = rub[k].number;
                        i++;
                    }
                }
                --idx;
                rub = (struct Person *) realloc(ttmp, (idx)*sizeof(Person));
                for(k=0;k<idx;k++){
                    printf("%d/%d) %s %s\n%s\n%s\n-------------------\n", k,idx, rub[k].name,rub[k].surname,rub[k].address,rub[k].number);
                }
                break;
            case 'p':
                for(k=0;k<idx;k++){
                    printf("%d) %s %s\n%s\n%s\n-------------------\n", k+1, rub[k].name,rub[k].surname,rub[k].address,rub[k].number);
                }
                break;
            case '0':
                return 0;
                break;
        }
        fseek(stdin,0,SEEK_END);
    }
    while(c != 0);
    return 0;
}

char * getString(void){
    char *stringa = NULL, c;
    int i=0;
    stringa = malloc(sizeof(char));
    while((c=getchar()) != '\n'){
        stringa = (char *) realloc(stringa, (i+1)*sizeof(char));
        stringa[i++] = c;
    }
    stringa[i] = '\0';
    return stringa;

}

这是我的输入(请输入相同的输入,并告诉我是否收到相同的错误)。我将在这个例子中使用一些愚蠢和随机的词:

i
asd
i
qwe
i
zxc
p
n
asd
p

这是我上一次&#39; p&#39;的输出。命令:

1) asd asd
asd
asd
-------------------
2) qwe qwe
qwe
qwe
-------------------
3) asd asd
asd
asd
-------------------

为什么数组的最后位置变得与第一个相同? 欢迎任何其他建议或建议!

感谢

2 个答案:

答案 0 :(得分:1)

我不读完整个代码,但发现这段代码似乎不正确:

stringa = malloc(sizeof(char));
while((c=getchar()) != '\n'){
    stringa = (char *) realloc(stringa, sizeof(char));

你正在重新分配一个字节,字符串nevers一直在增长!

您应该根据变量i

重新分配

答案 1 :(得分:1)

为什么数组的最后一个位置与第一个位置相同?

case 'n':
    printf("What name are you looking for? ");
    scanf("%s",tmp);

tmp 会被重复使用。 (case 'c':也是)

更改为

E.g

case 'n':
    printf("What name are you looking for? ");
    tmp=getString();
    for(k=0;k<idx;k++){
        if(strcmp(rub[k].name,tmp) == 0){
            printf("%s\n%s\n%s\n%s\n", rub[k].name,rub[k].surname,rub[k].address,rub[k].number);
        }
    }
    free(tmp);
    break;

rub[idx].name = (char *) malloc(strlen(tmp));
rub[idx].name = tmp;
//As well as in other like

内存泄漏。

rub[idx].name = tmp;//by getString()