0

I need to make a simple DB in C using structures. I can make const size, but i want to increase size every time i add new element. Here is structure and functions to add and print.

typedef struct PASSPORT
{
    char    name[25];
    char surname[25];
    char address[25];
    int          day;
    int        month;
    int         year;
}passport;

int iterator = 0; //position in mas of passport

passport* add_pas(passport** pas)
{
    int scan_status = 187;
    *pas = realloc(*pas, sizeof(*pas) + sizeof(passport));
    if (*pas == NULL)
    {
        printf(" \nNULL prt in realloc");
        exit(-1);
    }

    printf("\n Enter surname: ");
    scan_status = scanf(" %25s", (*pas)[iterator].surname);

    printf(" Enter name: ");
    scan_status = scanf(" %25s", (*pas)[iterator].name);

    printf(" Enter street address: ");
    scan_status = scanf(" %25s", (*pas)[iterator].address);

    printf(" Enter date of birth(dd/mm/yyyy): ");
    scan_status = scanf("%d/%d/%d", &(*pas)[iterator].day, &(*pas)[iterator].month, &(*pas)[iterator].year);

    ++iterator;
    return (*pas);
}

void print_all_passports(passport* pas, FILE* file)
{
    for (int i = 0; i < iterator; ++i)
    {
        printf("\n---------------------------");
        fprintf(file, "\nPASSPORT %d: ", i + 1);
        printf("\n Surname: ");
        fprintf(file, "%s", pas[i].surname);
        printf("\n Name: ");
        fprintf(file, "%s", pas[i].name);
        printf("\n Address: ");
        fprintf(file, "%s", pas[i].address);
        printf("\n Date of birht: ");
        fprintf(file, "%d/", pas[i].day);
        fprintf(file, "%d/", pas[i].month);
        fprintf(file, "%d", pas[i].year);
    }
    printf("\n---------------------------\n");
}

So, actually it works: i can add elements and print them, BUT before exiting the program in main it throws an exception and says: wntdll.pdb contains the debug information required to find the source for the module ntdll.dll. Also i have a warninig when i realloc.

I am a new at c/c++, i understand that maybe i make smt wrong with memory an etc. I hope someone can help me to solve it.

acidx27x
  • 13
  • 2
  • 1
    Print out `sizeof(*pas)` and you’ll see it’s not doing what you think. You’ll have to keep track of size yourself. – Sami Kuhmonen Feb 08 '22 at 17:26
  • You should show the actual exception here, as well as the actual warning. – Random Davis Feb 08 '22 at 17:27
  • 1
    `sizeof(*pas) + sizeof(passport)` is not correct. That's just the size of 2 passport records. You need to multiply `sizeof *pas` by the total number of records. – Barmar Feb 08 '22 at 17:27
  • @RandomDavis: The actual exception is 0x77DFAA3C; and he's stopped at first chance in `ntdll` but can't extract the exception because he doesn't know how. To be fair, this is a pretty horrible problem with the debugger to try to solve at this level of experience. The exception merely means heap corruption. – Joshua Feb 08 '22 at 17:33
  • @Barmar Correct me if I'm wrong but... `sizeof *pas` is still just a pointer so that's not correct either – Support Ukraine Feb 08 '22 at 17:37
  • @4386427 Right, I got confused. – Barmar Feb 08 '22 at 17:38
  • 1
    This `*pas = realloc(*pas, sizeof(*pas) + sizeof(passport));` is wrong. You need `*pas = realloc(*pas, (iterator + 1) * sizeof(passport));` BUT, BUT .... Don't write code like that. Don't use a global variable called `iterator`. Don't use globals. Don't call it `iterator`. It's not even an iterator... it's a size. Wrap the size and the pointer to the array into another struct then your code will be much simpler. – Support Ukraine Feb 08 '22 at 17:44

0 Answers0