The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
.
google unix.com



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 05:01 AM
fwrite throws segmentation fault fermisoft High Level Programming 6 09-13-2005 01:46 AM
Segmentation fault jshaulis AIX 1 06-01-2004 04:16 PM
segmentation fault omran High Level Programming 2 08-01-2003 08:19 AM

Reply
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 06-19-2009
ajaysahoo ajaysahoo is offline
Registered User
  
 

Join Date: Feb 2008
Posts: 14
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 (permalink)  
Old 06-19-2009
GaneshCPUX GaneshCPUX is offline
Registered User
  
 

Join Date: May 2009
Posts: 20
Please check whether fpWriteFile and fpWriteFileare valid pointers. Most of the time these are the pain points.
  #3 (permalink)  
Old 06-19-2009
rakeshawasthi rakeshawasthi is offline
Registered User
  
 

Join Date: Aug 2004
Location: India
Posts: 379
You have not initialized recvgen. I mean, there is no value in that.
PS:- use code tags in your code please.
  #4 (permalink)  
Old 06-19-2009
ajaysahoo ajaysahoo is offline
Registered User
  
 

Join Date: Feb 2008
Posts: 14
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 (permalink)  
Old 06-19-2009
rakeshawasthi rakeshawasthi is offline
Registered User
  
 

Join Date: Aug 2004
Location: India
Posts: 379
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 (permalink)  
Old 06-19-2009
ajaysahoo ajaysahoo is offline
Registered User
  
 

Join Date: Feb 2008
Posts: 14
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 (permalink)  
Old 06-19-2009
redoubtable redoubtable is offline
Registered User
  
 

Join Date: Aug 2008
Location: Portugal
Posts: 242
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.
Sponsored Links
Reply

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 12:57 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0