access violation segmentation fault

the dev c++ compiler shows "access violation segmentation fault&

Page 1 of 1

5 Replies - 2675 Views - Last Post: 06 January 2010 - 07:20 AM Rate Topic: -----

#1 nikunj029   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 19-December 08

access violation segmentation fault

Posted 06 January 2010 - 02:08 AM

#include <iostream>
#include<stdio.h>
#include<string.h>
#include<math.h>

using namespace std;

int i=0,j=0,k;

void strconv(char t[] ,long &tin,long &tout) 
{	char tinc[15],toutc[15];
	 for(i=0;t[i]!=' ';i++)
	 {			   tinc[i]=t[i];
	 }
	 tinc[i]='\0';
	 for(j=i+1,k=0;t[j]!='\0';j++,k++)
	 {			   toutc[k]=t[j];
	 }
	 toutc[k]='\0';
	 tin=0;
	 tout=0;
	 int tinl=strlen(tinc);
	 int toutl=strlen(toutc);
	 
	 for(i=0;i<tinl;i++)
	 {			   tin+=(long(tinc[i])-long('0'))*pow(10,tinl-i);
	 }													  
	  for(i=0;i<toutl;i++)
	 {			   tout+=(long(toutc[i])-long('0'))*pow(10,tout-i);
	 }													  
	
}

void sort(long time[],int n)
{	long temp;
	 for(i=0;i<n;i++)
	 {			   for(j=0;j<n-i;j++)
					 {			   if(time[j]>=time[j+1])
									 {		  temp=time[j];
												time[j]=time[j+1];
												time[j+1]=temp;
									 }
					 }
	 }
}

int main()
{   int n;			  //No. of enteries
	char times[50][500];
	cin>>n;
	long timein[500],timeout[500];
   // timein=new long[n];
	//timeout=new long[n];
	
	for(i=0;i<n;i++)
	{			   gets(times[i]);
	}
		for(i=0;i<n;i++)
	{   
					strconv(times[i],timein[i],timeout[i]);
	}		   
	sort(timein,n);
	sort(timeout,n);
	
	
	long tmin=timein[0];
	long tmax=timeout[n-1];
	
	long count=0, cmax=0;
	j=0,k=0;
	for(i=tmin;i<=tmax||j==n;i++)
	{	 if(i==timein[j])
		  {			  count++;
						 j++;
		  }
		  if(i==timeout[k])
		  {			   count-=1;
						  k++;
		  }
		  if (cmax<count)
		  {			  cmax=count;
		  }
	}
	
	cout<<cmax;
	
	char ex;
	cin>>ex;
}


**Mod Edit: Added code tags :) **

This post has been edited by OliveOyl3471: 06 January 2010 - 06:47 AM


Is This A Good Question/Topic? 0
  • +

Replies To: access violation segmentation fault

#2 tommynator128   User is offline

  • D.I.C Head
  • member icon

Reputation: 8
  • View blog
  • Posts: 127
  • Joined: 05-January 10

Re: access violation segmentation fault

Posted 06 January 2010 - 04:55 AM

can you please give a bit more information about the error, an please put the code into the code tag
like this

Was This Post Helpful? 0
  • +
  • -

#3 nikunj029   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 19-December 08

Re: access violation segmentation fault

Posted 06 January 2010 - 05:05 AM

the fault as i presume is in the strconv function which i was unable to figure out

#include <iostream>
#include<stdio.h>
#include<string.h>
#include<math.h>

using namespace std;

int i=0,j=0,k;

void strconv(char t[] ,long &tin,long &tout) 
{ char tinc[15],toutc[15];
for(i=0;t[i]!=' ';i++)
{ tinc[i]=t[i];
}
tinc[i]='\0';
for(j=i+1,k=0;t[j]!='\0';j++,k++)
{ toutc[k]=t[j];
}
toutc[k]='\0';
tin=0;
tout=0;
int tinl=strlen(tinc);
int toutl=strlen(toutc);

for(i=0;i<tinl;i++)
{ tin+=(long(tinc[i])-long('0'))*pow(10,tinl-i);
} 
for(i=0;i<toutl;i++)
{ tout+=(long(toutc[i])-long('0'))*pow(10,tout-i);
} 

}

void sort(long time[],int n)
{ long temp;
for(i=0;i<n;i++)
{ for(j=0;j<n-i;j++)
{ if(time[j]>=time[j+1])
{ temp=time[j];
time[j]=time[j+1];
time[j+1]=temp;
}
}
}
}

int main()
{ int n; //No. of enteries
char times[50][500];
cin>>n;
long timein[500],timeout[500];
// timein=new long[n];
//timeout=new long[n];

for(i=0;i<n;i++)
{ gets(times[i]);
}
for(i=0;i<n;i++)
{ 
strconv(times[i],timein[i],timeout[i]);
} 
sort(timein,n);
sort(timeout,n);


long tmin=timein[0];
long tmax=timeout[n-1];

long count=0, cmax=0;
j=0,k=0;
for(i=tmin;i<=tmax||j==n;i++)
{ if(i==timein[j])
{ count++;
j++;
}
if(i==timeout[k])
{ count-=1;
k++;
}
if (cmax<count)
{ cmax=count;
}
}

cout<<cmax;

char ex;
cin>>ex;
}


Was This Post Helpful? 0
  • +
  • -

#4 anonymouscodder   User is offline

  • member icon

Reputation: 126
  • View blog
  • Posts: 710
  • Joined: 01-January 10

Re: access violation segmentation fault

Posted 06 January 2010 - 06:46 AM

for(i=0;t[i]!=' ';i++)
{ tinc[i]=t[i];
}


What if t[i]!=' ' is true and i is already 14?
Was This Post Helpful? 0
  • +
  • -

#5 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6246
  • View blog
  • Posts: 24,014
  • Joined: 23-August 08

Re: access violation segmentation fault

Posted 06 January 2010 - 06:52 AM

Mixing C and C++ I/O is bad.
Your naming conventions make your code very nearly unreadable.

As anonymouscodder says...you have no array bounds checking.
Was This Post Helpful? 0
  • +
  • -

#6 Delta_Echo   User is offline

  • D.I.C Addict

Reputation: 5
  • View blog
  • Posts: 722
  • Joined: 24-October 07

Re: access violation segmentation fault

Posted 06 January 2010 - 07:20 AM

What anonymouscodder and Jack are refering to is the fact that you are trying to access an element of an array that does not exist. See this:http://en.wikipedia.org/wiki/Segmentation_fault
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1