Can you tell me specific what this program is doing? [closed]

First of all, this line

 if(!(temp = (double*)realloc(data, capacity*sizeof(double))))

should look like

 if(!(temp = realloc(data, capacity*sizeof(double))))

because as per this discussion we need not to cast the return value of malloc() and family in C..

That said, to break down the statement,

  1. First, temp = realloc(data, capacity*sizeof(double)) gets evaluated. This statement reallocates data to have a memory allocated equal to the size of capacity*sizeof(double) bytes. The returned pointer is stored to temp.

  2. Then essentially the whole statement reduces to if (! (temp)). This check for the success of the realloc() call by checking the returned pointer against NULL.

    • If realloc() failed, it returned NULL, and the if will evaluate to TRUE, so the program will execute exit(1); and get over.

    • If realloc() is a success, temp will have a non-NULL pointer, thereby, the if check will fail and the program will continue normally.

Leave a Comment