+ 1
free() aborts program - double free or corruption (out) (C)
So I have like 3 arrays in main() that I declare as pointers and initialize with calloc(). If I don't free the pointers, the program runs perfectly. However, when freeing all three pointers, the program compiles but it doesn't run entirely, as when it reaches the free() declarations, it "aborts" with the error "double free or corruption (out)". Nevertheless, the pointers I'm freeing are all different, so I can't figure out where is the "double free" the program is telling me there is. What could be the error then? Thank you for using your time reading this. PD: I have run the program on Code:Blocks and it worked with the free() declaration, however, it still doesn't work when running on linux (gcc).
5 ответов
+ 1
You write outside of the allocated memory and corrupt service information of memory allocation unit:
You allocate n+m doubles in line #43:
pq = (double*) calloc(n+m, sizeof(double));
but write to n+m+1 double in line #92:
pq[i] += (x[j] * y[i-j]);
variable "i" iterate from 0 to m+n inclusively
whem "i" becomes m+n you write outside of the allocated block.
The same for other allocations.
Add 1 to allocated sizes of your calloc calls:
lines #41...#43 should be:
p = (double*) calloc(n + 1, sizeof(double));
q = (double*) calloc(m + 1, sizeof(double));
pq = (double*) calloc(n+m + 1, sizeof(double));
lines #76, #77 should be:
x = (double*) calloc(n+m +1, sizeof(double));
y = (double*) calloc(n+m + 1, sizeof(double));
+ 2
Show your code.
May be you free a pointer that was not allocated with calloc or malloc?
+ 1
Please show the code - specifically where you allocate and free your arrays.
+ 1
https://www.codepile.net/pile/Jp18m4xz this is the code, the problem frees are between comments.
Thank you for your help
+ 1
So that's it! Thank you both very much for answering and solving it!