Freeing an array of mpc_t in C -


i'm new c programming , couldn't find solution problem. although code works (i've been able include in other program), when tries free memory assigned calloc(), returns following error:

free(): invalid next size (normal): 

followd appears memory address. i'm using mpc libraries (for arbitrary precision complex numbers). smallest program repeats error:

#include <stdio.h> #include <stdlib.h> #include <gmp.h> #include <mpfr.h> #include <mpc.h>  int n = 10; int precision = 512;  int main(void) {     mpc_t *dets2;     dets2 = (mpc_t*)calloc(n-2,sizeof(mpc_t));      (int = 0; i<=n-2; i++) {         mpc_init2(dets2[i],512); //initialize complex numbers         mpc_set_str(dets2[i],"1",0,mpfr_rndn); //set numbers 1     }      free(dets2); //release memory occupied numbers     return 0; } 

thanks help!

your loop breaks after i == n-2, should break before. condition in loop should i<n-2 instead of i<=n-2.

so try access memory, out of bounds. leads undefined behaviour, can happen, including segmentation fault, free run time error or nothing.


Comments

Popular posts from this blog

c++ - No viable overloaded operator for references a map -

java - Custom OutputStreamAppender not run: LOGBACK: No context given for <MYAPPENDER> -

java - Cannot secure connection using TLS -