segmentation fault in fwrite function


 
Thread Tools Search this Thread
Top Forums Programming segmentation fault in fwrite function
# 1  
Old 06-19-2009
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 );
# 2  
Old 06-19-2009
Please check whether fpWriteFile and fpWriteFileare valid pointers. Most of the time these are the pain points.
# 3  
Old 06-19-2009
You have not initialized recvgen. I mean, there is no value in that.
PS:- use code tags in your code please.
# 4  
Old 06-19-2009
i am receiving data from socket ...Please find the codes for that...

memset(recvgen,' ',NSE_MAX_PACKET_RECV_SIZE);

recv_bytes = recvfrom(sockfd1,recvgen,NSE_MAX_PACKET_RECV_SIZE,0,(struct sockaddr *)&serv_addr,&clilen)
# 5  
Old 06-19-2009
Code:
memset(recvgen,'\0',NSE_MAX_PACKET_RECV_SIZE);

debug it and see what exactly are the values of each variable and exactly from which statement are you getting error. use printf for this.
# 6  
Old 06-19-2009
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
# 7  
Old 06-19-2009
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

C. To segmentation fault or not to segmentation fault, that is the question.

Oddities with gcc, 2.95.3 for the AMIGA and 4.2.1 for MY current OSX 10.14.1... I am creating a basic calculator for the AMIGA ADE *NIX emulator in C as it does not have one. Below are two very condensed snippets of which I have added the results inside the each code section. IMPORTANT!... (11 Replies)
Discussion started by: wisecracker
11 Replies

2. Programming

Segmentation fault when I pass a char pointer to a function in C.

I am passing a char* to the function "reverse" and when I execute it with gdb I get: Program received signal SIGSEGV, Segmentation fault. 0x000000000040083b in reverse (s=0x400b2b "hello") at pointersExample.c:72 72 *q = *p; Attached is the source code. I do not understand why... (9 Replies)
Discussion started by: jose_spain
9 Replies

3. UNIX for Dummies Questions & Answers

Shell equivalent of matlab fwrite function

I have some matlab code that sends the serial port elements of an array using matlab's fwrite function, e.g.: fwrite(s, , 'uchar'); What would be a unix shell equivalent? E.g., after successfully accessing the port using instructions here: Simple terminal serial port program for Linux/MacOSX... (3 Replies)
Discussion started by: darwin_886
3 Replies

4. Shell Programming and Scripting

Segmentation fault in function call, shell script

I am getting Segmentation fault at below function call in my script: get_x() { sqlplus -s / <<end | grep KEEP | sed 's/KEEP//;s///g' select 'KEEP' ,table_name from all_synonyms where upper(synonym_name)= '$1'; exit end x=$(get_x $1) echo " SQL OUTPUT IS :: $x" } I am getting output of... (1 Reply)
Discussion started by: IB_88
1 Replies

5. Programming

The fwrite function is not returning error, if the file was removed.

The fwrite function call is not returning error, when the file it writes to is removed, please advise on how to find if the file already opened and being written by a program is removed manually or by some other process. please see the code below, #include<stdio.h> #include<stdlib.h> ... (3 Replies)
Discussion started by: Kesavan
3 Replies

6. Programming

Using gdb, ignore beginning segmentation fault until reproduce environment segmentation fault

I use a binary name (ie polo) it gets some parameter , so for debugging normally i do this : i wrote script for watchdog my app (polo) and check every second if it's not running then start it , the problem is , if my app , remain in state of segmentation fault for a while (ie 15 ... (6 Replies)
Discussion started by: pooyair
6 Replies

7. Programming

segmentation fault.

This code is causing a segmentation fault and I can't figure out why. I'm new to UNIX and I need to learn how to avoid this segmentation fault thing. Thank you so much. Thanks also for the great answers to my last post.:):b: int main() { mysqlpp::Connection conn(false); if... (3 Replies)
Discussion started by: sepoto
3 Replies

8. Programming

Segmentation fault.

I'm getting a segmentation fault. I'm new to Linux programming. Thanks so much for all of your input.:eek: #include </usr/include/mysql++/mysql++.h> #include <stdio.h> #include <iostream> #include <sstream> #include <string.h> using namespace std; int outputToImport(const char*... (1 Reply)
Discussion started by: sepoto
1 Replies

9. Programming

segmentation fault while returning from function.

I am working on the application in which I have to fetch values from the database and paste in url and send it to portal. table=get_result("SELECT serialno,cas,Mode,FLC,TLC,location,CompName,CompCode,FG,FC,DispNo,TruckNo,LWbill,RRGPN,INVNO,DCN,RQTY,DQTY,SQTY,DDATE,RDATE,SDATE,TTIME FROM... (1 Reply)
Discussion started by: er.rohan88
1 Replies

10. Programming

fwrite throws segmentation fault

Code : function sSaveTFFile ....................... iRetCode = link (caCurrentFilename, caBackupFilename); if (iRetCode == -1) { ERR_MSG2(LOG_ALERT, "Can't move %s to %s", caCurrentFilename, caBackupFilename); return(FAILURE); } iRetCode = unlink... (6 Replies)
Discussion started by: fermisoft
6 Replies
Login or Register to Ask a Question