0
typedef struct dvdtype{
   int dvdcode;
   char title[50];
   int customerID;
   int daysowned;
}dvdtype;

typedef struct dvdstruct{
    dvdtype *dvd;
    int numdvds;
}dvdstruct;

void initDvds(dvdstruct *dvds);
int displayMainMenu();
void insertMovie(dvdstruct *dvds,int *n);

void initDvds(dvdstruct *dvds)
{
    int i;

    dvds->dvd=(dvdtype*)malloc(5*sizeof(dvdtype));
    if(dvds->dvd==NULL){

        printf("not enough memory\n");
        exit(1);
    }

    dvds->numdvds=0;
    for(i=0;i<5;i++)
    {
        dvds->dvd[i].customerID=-1;
        dvds->dvd[i].daysowned=-1;
        dvds->dvd[i].dvdcode=-1;
        dvds->dvd[i].title[0]='\0';
    }
}

void insertMovie(dvdstruct *dvds,int *n){
    int code;

    if(dvds->numdvds>=(*n))
    {
        dvds->numdvds++;
        dvds->dvd=realloc(dvds->dvd,(dvds->numdvds)*sizeof(dvdtype));
        if(dvds->dvd==NULL){

            printf("not enough memory\n");
            exit(1);
        }

        printf(" realloc succesful,size now is :%d \n",dvds->numdvds);
        dvds->dvd[dvds->numdvds].customerID=-1;
        dvds->dvd[dvds->numdvds].daysowned=-1;
        printf("give code and name of movie \n");
        scanf("%d\n",&code);
        dvds->dvd[dvds->numdvds].dvdcode=code;
        gets(dvds->dvd[dvds->numdvds].title);
    }
    else
    {
        printf("give code and name of movie \n");
        scanf("%d\n",&code);
        dvds->dvd[dvds->numdvds].dvdcode=code;
        gets(dvds->dvd[dvds->numdvds].title);
        dvds->numdvds++;
    }
}

I wrote this program to make a list of movies. With the function inidvds I "make room" with malloc for 5 dvds and initialize. Then, the user calls the function insertmovie to insert movie to the file.

The teacher wants us the following. If the user gives 5 dvds (that's the size that we wanted from the start), to call the function realloc and make room for 1 more dvd. This works, I mean when I give 5 movies it's okay. I have a choice for print movies and it prints all 5 movies. Then if I choose to add movie again (n=5) it goes to the if and must do the realloc function.

I put this

printf(" malloc successful,size now is :%d \n",dvds->numdvds);

so I can see if it does go to realloc. It prints:

malloc is successful and the new size is 6

and it asks from me to give code of movie and name which is what we want. I give a code and the name, then I print the list again. It prints the 5 first movies that I have given ok but it doesn't print the 6th movie.

For example:

Dvdcode is 5
dvd name is: tarzan
Customer id is: -1(the initialized value)

let's say that's the fifth movie that I had given. Then the 6th is like this.

Dvdcode is 0
dvd name is:   
Customer id is: 0
(Dvd name is empty)

What does that mean? realloc doesn't work?

Cœur
  • 37,241
  • 25
  • 195
  • 267
  • 1
    Side note: `X = realloc(X, ...);` is a bad idea. If `realloc` fails, you lose your pointer (`X`) which means no recovery as well as memory leaks. – Shahbaz Mar 18 '14 at 11:07
  • Thanks but is it any other way to allocate memory? – user3430128 Mar 18 '14 at 11:42
  • 1
    What @Shahbaz meant is to use `realloc` this way: `Y = realloc(X, ...);`. Do not save return value in `X` itself. First check if `Y` is non-`NULL` then do `X = Y`. – 0xF1 Mar 18 '14 at 11:52
  • Ah.ok..i understand what u mean..but in my other functions i pass as parameter delete(*X) for example...if i use Y = realloc(X, ...); when i have to call realloc, how i am going to work on Y..in all my other functions i pass X as parameter.. – user3430128 Mar 18 '14 at 12:08
  • Anyways...can anyone figure out the problem..??I dont know whats wrong... – user3430128 Mar 18 '14 at 12:10
  • You do `Y = realloc(X, ...)`, then check `Y == NULL`, if everything is ok, you do `X = Y;`. – Shahbaz Mar 18 '14 at 12:34
  • Not sure where you come from, but in every language I have seen a sentence is terminated with a single `.`, not by `..` – Shahbaz Mar 18 '14 at 14:57

1 Answers1

1

Arrays in C are 0-based, that is the first element is array[0] the second is array[1] and so on.

So those lines of insertMovie():

  if(dvds->numdvds>=(*n))
  {
    dvds->numdvds++;
    dvds->dvd=realloc(dvds->dvd,(dvds->numdvds)*sizeof(dvdtype));
    if(dvds->dvd==NULL){

        printf("not enough memory\n");
        exit(1);
    }

    printf(" realloc succesful,size now is :%d \n",dvds->numdvds);
    dvds->dvd[dvds->numdvds].customerID=-1;
    dvds->dvd[dvds->numdvds].daysowned=-1;
    printf("give code and name of movie \n");
    scanf("%d\n",&code);
    dvds->dvd[dvds->numdvds].dvdcode=code;
    gets(dvds->dvd[dvds->numdvds].title);

shall be:

  if(dvds->numdvds>=(*n))
  {
    dvds->dvd=realloc(dvds->dvd,(dvds->numdvds + 1)*sizeof(dvdtype));
    if(dvds->dvd==NULL){

        printf("not enough memory\n");
        exit(1);
    }

    printf(" realloc succesful,size now is :%d \n",dvds->numdvds + 1);
    dvds->dvd[dvds->numdvds].customerID=-1;
    dvds->dvd[dvds->numdvds].daysowned=-1;
    printf("give code and name of movie \n");
    scanf("%d\n",&code);
    dvds->dvd[dvds->numdvds].dvdcode=code;
    gets(dvds->dvd[dvds->numdvds].title);

    dvds->numdvds++;
alk
  • 69,737
  • 10
  • 105
  • 255
  • Thanks ..!!I t works...!!thank ui very much..but i dont understand why it works this way..basically the only thing you changed is the // numdvds++; ..When i finish inserting the first 5 movies numdvds is equal to 5, so when i do this ( numdvds++; )it should be equal to 6..so then with realloc the computer has to give 6* sizeof(..) Are my thoughts right..? I can't tell the difference...@alk – user3430128 Mar 18 '14 at 18:11
  • 1
    @user3430128: No, I did not just move `dvds->numdvds++;`. You might like to re-read the first sentence of my answer, see how the code I quote used indicies before and how they are used after my modification, think about it and find enlightment. – alk Mar 18 '14 at 18:27
  • @user3430128: Also if you like an answer you are free to upvote it. If you even feel it is the answer to your question you could mark it as such by clicking its check-mark. – alk Mar 18 '14 at 18:32