The UNIX and Linux Forums  

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
Memory Fault - Core Dumped bayuz UNIX for Advanced & Expert Users 0 10-17-2007 11:21 AM
Segmentation Fault (core dumped) bankpro High Level Programming 2 01-20-2006 04:23 AM
Abort core dumped!!!! zing Filesystems, Disks and Memory 2 07-09-2003 09:59 PM
core dumped yls177 UNIX for Dummies Questions & Answers 9 09-20-2002 11:32 AM
Segmentation fault (core dumped) Ivo UNIX for Dummies Questions & Answers 1 02-08-2002 08:23 AM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 06-10-2008
Registered User
 

Join Date: Apr 2008
Posts: 6
Memory Fault,Core dumped

I have written a code in UNIX which is complied by using g++. Compling with turbo C didnt yield any errors, but with g++ I am getting Memory fault, core dumped. Could anyone help me out with this?

Given below is the code:

Code:
#include<stdio.h>

#include<string.h>
#include<stdlib.h>
#include<ctype.h>
main()
{
long int lt;
int i,r=0,p,m=0,q=0;
char a[100];
char *str;
//a=(char*) malloc(sizeof(char) *100);
str=(char *)  malloc(sizeof(char) *100);


scanf("%s",str);
p=strlen(str);
for(i=0;i<p;i++)
{
        if(isdigit(str[i])==0)
        {
        if((str[i]!='.')&& str[i]!='-')
       {
        m=1;
        }
        }
}
for(i=0;i<=p;i++)
{
        if(str[i]=='-')
        r=r+1;
}
for(i=0;i<=p;i++)
{
        if(str[i]=='.')
        q=q+1;
}
lt=atol(str);
if(p>=16 || strcmp(str,NULL)==0 || m==1 || r>1 ||q>1 || lt==0)
{
printf("ERROR");
}
else
{
//ltoa(lt,a,10);
sprintf(a,"%l",lt);
printf("%s",a);
}
}

Last edited by vino; 06-10-2008 at 05:35 AM. Reason: added code tags
Reply With Quote
Forum Sponsor
  #2  
Old 06-10-2008
vino's Avatar
Supporter (in vino veritas)
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,698
Quote:
Originally Posted by usshell View Post

Given below is the code:

Code:
for(i=0;i<=p;i++)
{
        if(str[i]=='-')
        r=r+1;
}
for(i=0;i<=p;i++)
{
        if(str[i]=='.')
        q=q+1;
}
I did not look further but... in your second and third for-loop, the counter goes from 0 to p, both inclusive. It should be
Code:
for(i=0; i<p ;i++)
Reply With Quote
  #3  
Old 06-13-2008
Registered User
 

Join Date: Mar 2008
Location: India(Sarkaghat)
Posts: 20
Hi,


Your code is giving segmentation fault due to wrong use of strcmp(str1,str2) function.

A string can't be compared using strcmp with a NULL pointer. Both the arguments should be not NULL only. Use

str==NULL

for comparison instead.
This 'll remove the segmentation fault.


Regards,
Vinod.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 12:32 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0