4
int main() {
    struct lottery *array;      

    array = (struct lottery *)malloc(3000 * sizeof(struct lottery));       
    int opt, counter;

    menu1();
    scanf("%d", &opt);
    if (opt == 1)
        Load(array, &counter);
    else
        exit("0");
    menu2();
    counter--;
    scanf("%d", &opt);
    while (opt != 7) {
        switch (opt) {
        case 1:
            Save(array);
            break;
        case 2:
            Enterd(array, &counter);
            printf("%d\n", counter);
            break;
        }
        menu2();
        scanf("%d", &opt);
    }
    return 0;
}

void Enterd(struct lottery *a, int *count) {
     struct lottery *b;
     int x;

     (*count)++;
     x = *count;

    printf("Your new data will have an ID of %d\n",x);
    a[x].aa = x;

    b = (struct lottery *)realloc(a, x * sizeof(struct lottery));
    if (b == NULL) {
        printf("Memory could not be allocated for your new input.Program will now exit...\n");
        exit("0");
    }

    a = b;

    printf("What is the date of your new draw?\n");
    scanf("%d/%d/%d", &a[x].date1.day, &a[x].date1.month, &a[x].date1.year);
    printf("Now please insert the 5 non-joker numbers\n");
    scanf("%d%d%d%d%d", &a[x].n1, &a[x].n2, &a[x].n3, &a[x].n4, &a[x].n5);
    printf("What is the 'Joker' number of this draw?\n");
    scanf("%d", &a[x].joker);
    printf("Your input is now complete.");
}

I am writing a protect about some lottery files. I have this problem in my function which is adding more data to the lottery array. Whenever x contains 1989, my realloc call returns NULL. I set x to be 1985 and i could add 4 more inputs to the array, but whenever x is 1989, it still returns NULL. My question is: is there something wrong with the code or I am still running out of memory?

18
  • 5
    We need to see a bit more code; could you paste up a main showing how this function is called? Commented Jan 20, 2017 at 14:22
  • 2
    You do not need cast on malloc/realloc Commented Jan 20, 2017 at 14:23
  • 1
    And please format your code properly. Commented Jan 20, 2017 at 14:23
  • 4
    exit takes an integer not a string - replace exit("0") with exit(0); Commented Jan 20, 2017 at 14:24
  • 3
    Where's the definition of struct lottery? Commented Jan 20, 2017 at 14:24

3 Answers 3

5

If realloc returns null, firstly print out the amount of memory you are asking to allocate. If it is a negative number or a huge amount, there's the problem. If it is a sensible amount, and you have a halfway decent machine, it's most unlikely you are out of memory. So the malloc() system must have been corrupted in some way. Either you are passing an invalid pointer, or you have written past the end of a block, maybe in a totally unrelated part of the program.

Sign up to request clarification or add additional context in comments.

2 Comments

I asked the question because i have 8GB or ram so i though it was unlike to get a memory full.
Modern machines "over commit" memory anyway, so even if the machine can't honour the request it claims to honour it, then starts shutting down apps if the memory is actually used.
0

Two significant errors:

C array indexing starts at zero, so after you realloc to x * sizeof(thing), only elements zero to x-1 are valid. Accessing element x will cause chaos.

Second, a = b modifies the local copy of a, but not the value array that you wanted it to...

4 Comments

1st:So you are saying i sould change x to x-1?
And about the 2nd,it worked for me in the Load function,it properly changed my array in main too.
@Edward 1st, Maybe. Your code's too convoluted to easily tell.
@Edward 2nd: If realloc moved the memory, array gets left pointing at the old data. One fix is to make enterd return a pointer to the new data. Then use array = enterd(array, &counter)
0

realloc can change the base address, but array pointer is passed by value, so a local reallocation is not visible in the main and generates some trouble.

You also reallocate to 0-sized array, probably not what you want, please use x+1 as the number of records in the reallocation. More, you access index x before reallocation, which is undefined behavior as before reallocation size is x-1, so move the line a[x].aa = x after reallocation.

Also please initialize your variables (like counter).

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.