Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>When you free a pointer, what happens to the memory allocated to it is undefined. It will not be cleared, it might be re-used and the content of the memory might be overwritten... or maybe not.</p> <p>In your case, it seems that the memory stayed untouched between the moment you freed it and the moment you print it, but this might not be the case at every execution of your program or on every operating system.</p> <p><code>void* realloc (void* ptr, size_t size)</code> will copy the memory allocated at <code>ptr</code>, and copy it the a new memory region of size <code>size</code>. But what do you ant to achieve with <code>realloc</code>?</p> <blockquote> <p>Is there any easier way to do all this in separate functions? can you be more specific on this ?</p> </blockquote> <p>UPDATE (on comment) : you can refactor your two functions to have the same type of interface but to be usable in different contexts with the following prototypes : </p> <pre><code>// the functions take as a parameter an array of integers, an a pointer to an integer. It returns an array of transformed data and through the second parameter, the length of the output. int* findDividers(int input, int* output_length) { int* dividers = malloc(input * sizeof(int)); int found_dividers = 0; // do things int* tmp = realloc(dividers, found_dividers); free(dividers); *output_length = found_dividers; return tmp; } int* primeFilter(int* input, int input_length, int* output_length) { /* same structure as before */ } // you can then use it as follow int integer_to_process = 0xdeadbeef; int dividers_count = 0; int* dividers = findDividers(integer_to_process, &amp;dividers_count); int primes_count = 0; int* primes = primeFilter(dividers, dividers_count, &amp;prime_count); // now feel free to use the two arrays, with a minimal length, as you want. </code></pre>
    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