Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I wouldn't. If your program crashes, because of some exotic, unforeseen things that happen in your program, it might even be pointless to try and free any allocated heap memory.<br/> I think it'd be best if you just call <code>exit (EXIT_FAILURE)</code>, if you can, and leave the OS to reclaim the memory you allocated as good as it possibly can.</p> <p>However, I would try to clean up any <em>other</em> resources that you've used/claimed/opened that may also cause leak. Close as many opened file pointers as you can, or flush any buffers lying around.<br/> Other than that, I'd say: leave it to the OS. Your program has crashed, or is crashing: trying to clean up after yourself in an unforeseen situation might be pointless, or -who knows- eventually do more harm than good.</p> <p>Of course, if by <em>"a library errors"</em> you mean something like:</p> <pre><code>MYSQL *connection = mysql_init(); if (connection == NULL) {//connection could not be initiated //what to do here? } </code></pre> <p>Or, no lib:</p> <pre><code>char **arr_of_strings = malloc(200*sizeof(char *)); //some code arr_of_strings[0] = calloc(150, sizeof(char)); //some more arr_of_strings[120] = calloc(350, sizeof(char)); if (arr_of_strings == NULL) {//ran out of heap memory //what do I do here? } </code></pre> <p>So, basically: it's a matter of: What does your program have to <em>do</em>, and can you easily find a way around the problems you're being faced with.<br/> If, for example, you're writing a mysql client, and the <code>mysql_init</code> call fails, I think it pretty evident you <em>cannot</em> continue. You could try to provide a fallback for every reason why this could happen, or you could just exit. I'd opt for the latter.<Br/> In the second case, it's pretty clear that you've depleted the heap memory. If you're going to write these strings to a file anyway, you could prevent this kind of error like so:</p> <pre><code>int main() { char **arr_str = malloc(20*sizeof(char *)); const char *fileName = "output.txt"; int i, j; int alloc_str(char ***str, int offset, int size); void write_to_file(const char *string, const char *fileName); for(i=0;i&lt;10;++i) { if (alloc_str(&amp;arr_str, i, 100+i) == -1) { if (i == 0) exit(EXIT_FAILURE);//this is a bigger problem for (j=0;i&lt;i;++j) {//write what we have to file, and free the memory if (arr_str[j] != NULL) { write_to_file(arr_str[j], fileName); free(arr_str[j]); arr_str[j] = NULL; } if (alloc_str(&amp;arr_str, i, 100+i) != -1) break;//enough memory freed! } } //assign value to arr_str[i] } for(i=0;i&lt;10;++i) free(arr_str[i]); free(arr_str); return 0; } void write_to_file(const char *string, const char *fileName) {//woefully inefficient, but you get the idea FILE* outFile = fopen(fileName, "a"); if (outFile == NULL) exit (EXIT_FAILURE); fprintf(outFile, "%s\n", string); fclose(outFile); } int alloc_str(char ***str, int offset, int size) { (*str)[offset] = calloc(size, sizeof(char)); if ((*str)[offset] == NULL) return -1; return 0; } </code></pre> <p>Here, I'm attempting to create an array of strings, but when I run out of memory, I'll just write some of the strings to a file, deallocate the memory they take up, and carry on. I could then refer back to the file to which I wrote the strings I had to clear from memory. In this case, I can ensure, though it does cause some additional overhead, my program will run just fine.<br/> In the second case, freeing memory is a must, though. I <em>have</em> to free up the memory required for my program to continue running, but all things considered, it's an easy fixed.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload