English 中文(简体)
C - 接触功能造成有IO档案的ault
原标题:C - Exponential function causes a segfault with file IO

EDIT:为了回答某些问题,这是订正的、仍然不可行的法典(其中多数是先开始的,但我本应明确表明,我已初步提出档案点等)。 再说一遍,如果我要么在(a)项之前加上一封信,要么完全删除(a)项,那就行了。

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, "
");
    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,
", gauss[i]);
        /* Seg fault is here */
        fprintf(outfile, "%g,
", gauss[i]);

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

• 编辑:

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

为了澄清,它没有达到功能的结束,因此它没有放弃它坠毁。 坠毁时正试图在案卷中写上 lo。

我检查一下,正如我所说的那样,支出是拖长或流入不足的,我可以打印产出,但书面文件是没有的。 如果我试图用简单的话说话,就失败了。

背书是(不熟悉 g,认为它可能有助于):

#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 ()

任何帮助都将受到高度赞赏。

最佳回答

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");

发生的情况是,你重写了<编码>>outfilename号指示,该指示号指代号。 之后,您尝试免费发放<代码>;。 由于你释放了直截了当的字面,行为没有定义,因此你正在坠毁。

为什么它坠毁于一起而不是另一起案件。 行为不明确,因此任何事情都允许发生。 您的成指数功能代码有可能对可能造成坠毁/失事的 st/飞跃有所作为。

http://www.un.org。 我希望这只是一个打字或错误的复印件,但我也看不到在什么地方启用了<代码>outfile。 如果它实际上从未开始,那就会产生另一个错误。 (很可能是造成你特别陷阱)

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.
}
问题回答

首先,您应实际编辑的所有警报生成的信息;gcc。 这意味着<代码>-Wall -g。

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

页: 1 页: 1

 outfile = fopen(outfilename, "r");

And you should learn to use the debugger gdb (or perhaps its ddd graphical front-end).

outfilename = "file.txt";
/* snip */
free(outfilename);

You can only free something you got back from malloc. You can t pass free a pointer to a constant!





相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...

热门标签