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

  • SEARCH
  • Home
  • 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 6834447
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T23:06:47+00:00 2026-05-26T23:06:47+00:00

EDIT: To answer some questions, this is the revised and still not working code

  • 0

EDIT: To answer some questions, this is the revised and still not working code (most of it was there to begin with, but I should have been explicit that I initialised the file pointer, etc). Again, only works if I either add a write before the exp() or remove the exp() entirely:

FILE *outfile;
char *outfilename;
outfilename = (char *)malloc(FILENAME_MAX*sizeof(char));
strcpy(outfilename, "outfile.txt");
outfile = fopen(realoutfilename, "w");

/* If this is uncommented, there isn't a segfault
if(realoutfile!=NULL && imoutfile!=NULL){
    fprintf(outfile, "\r\n");
    fseek(outfile,0,SEEK_SET);
}
*/

gauss = (double*) calloc(points, sizeof(double));

/* Maths and stuff */

if(outfile!=NULL){
    for(i=0;i<points;i++){
        /* this prints fine */
        printf(outfile, "%g,\r\n", gauss[i]);
        /* Seg fault is here */
        fprintf(outfile, "%g,\r\n", gauss[i]);

    }
}
fclose(outfile);
free(outfile);

And I’m compiling with:

gcc main.c -lm -Wall -Wextra -Werror -Wshadow -g -o main

To clarify, it doesn’t reach the end of the function – so it’s not the freeing that it crashes on. The crash is when it tries to write to the file in that for loop.

I’ve checked that exp() isn’t over or underflowing, as I say, I can printf the output, but file writing is a no-no. It also fails if I try a simple call, say exp(2).

The gdb backtrace is (I’m not that familiar with gdb, thought it might help):

#0  0xff15665c in _malloc_unlocked () from /lib/libc.so.1
#1  0xff15641c in malloc () from /lib/libc.so.1
#2  0xff1a8c80 in _findbuf () from /lib/libc.so.1
#3  0xff1a8f0c in _wrtchk () from /lib/libc.so.1
#4  0xff1ad834 in _fwrite_unlocked () from /lib/libc.so.1
#5  0xff1ad798 in fwrite () from /lib/libc.so.1
#6  0x000128ac in gaussian ()
#7  0x00010f78 in main ()

Any help would be greatly appreciated!

  • 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-26T23:06:47+00:00Added an answer on May 26, 2026 at 11:06 pm

    The problem, is here:

    outfilename = (char *)malloc(FILENAME_MAX*sizeof(char));
    outfilename = "file.txt";
    

    You can’t assign a string like that, you have to use strcpy:

    strcpy(outfilename, "file.txt");
    

    What’s happening is that you’re are overwriting the outfilename pointer with the string assignment. Then you try to free it free(outfilename);. Since you are freeing a string literal, the behavior is undefined, hence the crash you are getting.

    As for why it crashes in one case and not the other. The behavior is undefined, therefore anything is allowed to happen. It’s possible that your exponential function code does something to the stack/heap that could be causing it crash/not crash.

    EDIT : I hope it’s just a typo or mis-copy, but I also don’t see where outfile is initialized. If it really is never initialized, then that’s the other error. (and most likely the one that’s causing your particular segfault)

    So it should look like this:

    FILE *outfile;
    outfilename = (char *)malloc(FILENAME_MAX*sizeof(char));
    strcpy(outfilename, "file.txt");
    
    outfile = fopen(outfilename, "w");
    if (outfile == NULL){
        //  Error, file cannot be openned.
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

UPDATE: Solved. Thanks BusyMark! EDIT: This is revised based on the answer below from
This questions is related to java exceptions, why are there some cases that when
There have been a couple of questions very close to this topic, but none
I've seen some similar questions to mine here, but they don't really answer me...
Is there a way to convert from System.Windows.Forms.HtmlElement to mshtml.IHTMLElemenet3? --edit after answer accepted
Edit: I have solved this by myself. See my answer below I have set
EDIT: See my working code in the answers below. In brief: I have a
So I read some related questions here before I asked, but can't find the
I see some related questions have been asked, but they're either too advanced for
Hi I've searched some other posts, but most of them assumed that people knew

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.