![]() |
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here. |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| segmentation fault | joey | High Level Programming | 3 | 12-22-2008 05:28 PM |
| Segmentation fault | big123456 | Linux | 0 | 07-20-2007 06:01 AM |
| fwrite throws segmentation fault | fermisoft | High Level Programming | 6 | 09-13-2005 02:46 AM |
| Segmentation fault | jshaulis | AIX | 1 | 06-01-2004 05:16 PM |
| segmentation fault | omran | High Level Programming | 2 | 08-01-2003 09:19 AM |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
segmentation fault in fwrite function
Hi,
my code is written in proC and it is in UNIX(AIX).I have written a small code for writing data into a binary file,but while writing my program is giving core dump. Here Is my code---- fpWriteFile = fopen(WriteFileName,"wb+"); CHAR *recvgen; recvgen = (char *)malloc(sizeof(char)*NSE_MAX_PACKET_RECV_SIZE); fwrite (recvgen,NSE_MAX_PACKET_RECV_SIZE,1,fpWriteFile ); |
|
||||
|
i did the following code change...
memset(recvgen,'\0',NSE_MAX_PACKET_RECV_SIZE); still same problem,i debug using dbx ...its showing segmentation fault at fwrite. also sizeof(recvgen) is 4 where i have defined NSE_MAX_PACKET_RECV_SIZE=1050 |
|
||||
|
I'm guessing the problem is either in WriteFileName or fwriting to a closed stream due to improper verification if fopen() fails. The following code works under Linux Code:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#define NSE_MAX_PACKET_RECV_SIZE 1050
int
main(int argc, char *argv[])
{
FILE *fpWriteFile;
char *recvgen;
char WriteFileName[1024];
if (argc != 2)
exit(1);
strncpy(WriteFileName, argv[1], 1024);
if (strlen(argv[1]) >= 1024)
WriteFileName[1023] = '\0';
fpWriteFile = fopen(WriteFileName,"wb+");
if (fpWriteFile == NULL)
exit(1);
recvgen = (char *)malloc(sizeof(char)*NSE_MAX_PACKET_RECV_SIZE);
if (recvgen == NULL)
exit(1);
fwrite (recvgen,NSE_MAX_PACKET_RECV_SIZE,1,fpWriteFile);
exit (0);
}
Code:
cc -o file file.c ./file `perl -e 'print "A" x 10'` && echo $? -> OK: 0 ./file `perl -e 'print "A" x 1024'` ; echo $? -> NOT OK (failing with filename too long): 1 You won't have a segfault now, please try to just debug it with printf()s and find out the problem. |
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|