Search code examples
cstructmallocfreedynamic-memory-allocation

Program "sometimes" crash when using free


My program sometimes crash, and sometimes not when im calling free() on a element from my array. The elements in the array are a struct. I'll show with some code:

//This first part might be a bit messy, and hard to understand but it is working
int main(int argc, char *argv[]){
      komplexNumber komplexNumbers[antal-1]; 
      int i;

      float temp;
      int realCounter=0;
      int imaginarCounter=0;
      int numberOfElements=6 // this variable is set by the user using scanf, and can be any number>=2. Each komplexNumber consists of two numbers (i.e 9 -3j is a complex number)

       for(i=0;i<numberOfElements*2;i++){ // 
           printf("Enter number %i\n", i+1);
           scanf("%f", &temp); 
           if(i%2==0){  //a complex number consts of two parts. first entered number is first part of number1, second is second part of number1, third is first part of nr 2 etc
               komplexNumbers[realCounter].real=temp;
               realCounter++;
            }
            else{
              komplexNumbers[imaginarCounter].imaginar=temp;
              imaginarCounter++;
              }
//The struct
typedef struct komplexNumber{ 
  float real;
  float imaginar;
} komplexNumber;




//The method that mallocs memory for each element:
void calculation(float a1, float a2, float b1, float b2, komplexNumber komplexNumbers[]){ 

    float temp1 = (a1*a2)-(b1*b2);     
    float temp2 = (a1*b2)+(a2*b1);

    komplexNumber *k;
    k=(komplexNumber*)malloc(sizeof(komplexNumber));              
    k->real=temp1;
    k->imaginar=temp2;
    komplexNumbers[0]=*k;
}

//The loop, in which im calling free each iteration:

int counter=1;
for(i=0;i<(numberOfIterations-1);i++){                   
    a1=komplexNumbers[0].real;
    b1=komplexNumbers[0].imaginar;
    a2=komplexNumbers[counter].real;
    b2=komplexNumbers[counter].imaginar;
    calculation(a1, a2, b1, b2, komplexNumbers);         
    counter++;

    free(komplexNumbers[counter]);
}

This program crashes sometimes, and sometimes not. I haven't been able too see a pattern why it does, but it's the free() function that causes the crash (since when I remove free and run the program with the same values, it doesn't crash). I have not been able too see a pattern in which causes the crash. It can handle negative numbers

Note: each struct element is called complexNumber, and the array is called complexNumbers (with an s:) )


Solution

  • There are at least a few issues:

    • You're always assigning komplexNumbers[0] and freeing komplexNumbers[count]

    • komplexNumbers[0] = *k probably means komplexNumbers is an array of structs, not an array of pointers - you're assigning a struct, not a pointer

    EDIT

    In light of recent code, it's easier to show you what to do than to explain what you're doing wrong. As suspected, komplexNumbers is an array of structs. In your calculation function you don't need all that malloc stuff (and consequently you don't need the free bit). Do this instead:

    komplexNumbers->real = temp1;
    komplexNumbers->imaginar = temp2;