![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here. |
|
|
||||
| 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 |
|
|
Submit Tools | LinkBack | Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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 |
| Forum Sponsor | ||
|
|
|
#2
|
||||
|
||||
|
Quote:
Code:
for(i=0; i<p ;i++) |
|
#3
|
|||
|
|||
|
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. |
|||
| Google The UNIX and Linux Forums |