Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7627333
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T05:26:42+00:00 2026-05-31T05:26:42+00:00

My program sometimes crash, and sometimes not when im calling free() on a element

  • 0

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:) )

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T05:26:43+00:00Added an answer on May 31, 2026 at 5:26 am

    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;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to store some temporary files from my program sometimes, currently I use
I have the following C++ code and when I run the program sometimes it
My program is crash intermittently when it tries to copy a character array which
My program will sometimes be launched from another program. If that's the case, I
I sometimes notice programs that crash on my computer with the error: pure virtual
My program installation sometimes gives the following error in the installer log: `Failed to
I was recently working on a windows program that would sometimes become unresponsive when
Sometimes my c++ program crashes in debug mode, and what I got is a
Sometimes whenever I write a program in Linux and it crashes due to a
I program in C++. Sometimes there are 1000 ways to do something, and depending

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.