Welcome to Linux Forums!

With a comprehensive Linux Forum, information on various types of Linux software and many Linux Reviews articles, we have all the knowledge you need a click away, or accessible via our knowledgeable members.

Linux Forum ArticlesLinux ForumsLinux Forum DownloadsLinux Hosts
Home|Register|FAQ|Member List|Calendar|Unanswered Posts|Forum Rules|Today's Posts|Advanced Search|
SEARCH FOR IN
Go Back   Linux Forums > GNU Linux Zone > Linux Programming & Scripting
Reload this Page Segmentation fault
Linux Forums
Linux Forums
Welcome To The Linux Forums!
Welcome to Linux Forums. We pride ourselves in being one of the largest Linux communities on the web, we encourage you to REGISTER on our forums and participate in the community. There are over 150,000 members ready to answer your questions. JOINING US today will allow you to make new posts, get support, send messages to other members and submit downloads to our downloads directory and many other great features!

Linux Programming & Scripting C, Perl, PHP, Bash Scripts, anything programming or script related post in here!

Reply
 
Thread Tools Display Modes
Old 06-30-2007   #1 (permalink)
psrujan
Just Joined!
 
Join Date: Jun 2007
Posts: 1
Segmentation fault

Hi ,

I have little bit of knowledge in C++. When I run the following program its gives me segmentation error. I tried to do all the possible things by reading online but not able to figure out the error. I ran it suing gdb debugger but still it stays error is somewhere near n=atoi(argv[2]);
but i am not sure. Can somebdy please help me in finding the error.

my command line arguments are
./output 2 3 abc.pbm 1.2 abc2.pbm 1.3


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <algorithm>
#include <iostream>
#include <time.h>

using namespace std;
#define MAX 90
#define Lwire 2 // length of the wire 2mm -- all values are for 130nm technology
#define Nwire 128 // Number of wires 128
#define Llink 0.5 // average link load .5
#define powerleakage 2032.8 // leakage power 2032.8nw
#define V 1 BIG_DYN[y] // voltage= 1V
#define Ibias 1 // Ibias=1nA
#define K1 0.39
#define K2 .12
#define POP_SIZE 50
#define R_CO .33
#define R_MUT .1


int pbm[MAX][MAX][MAX],pgm[MAX][MAX],dist[MAX][MAX]={0};
int a=0,b=0,nodes,arg;
//int m,n;
double TotalDynEnergy=0,minTotalDynEnergy=0;
char head[4],head1[4];
int maxd;
// int layoutnumber=0;
// int layoutminTotalDynEnergy;

/**** Dynamic Energy for each communication pattern ****/

double energy( void)
{ int i,j,d;
double powerdyn,powershtckt,powertotal,energy=0;

maxd=0;
powerdyn= ( K1 +( K2 * Lwire))* Nwire * Llink;// Dynamic power equation
// printf("\n power dyn %f \t",powerdyn);
//powershtckt=V*Ibias;
// powertotal=sumlinks*(powerdyn+powerleakage+powersh tckt);
// printf("\n %f\n ",powertotal);

for(j=0;j<b;j++)
{ for (i=0;i<a;i++)
{
d=dist[j][i]; // printf("\n value of d %d\t",d);
if(d > maxd)
maxd=d; // max distance in the layout
if (d!=0)
{// Dynamic Energy for each entry in comm pattern
energy= energy+(0.37*d)+(powerdyn*(d-1)); // printf("\n energy %f",energy);
}
}
}
// printf("\n Dynamic energy for this pattern is %f \n",energy);
// printf("pattmaxd %d ",maxd);
return(energy);
}

/**** Load Communication paterns ****/

void loadpatterns(int argc,char **argv,int index)
{ FILE *pbmread;
int i,j;


pbmread = fopen (argv[arg],"r"); // open communication pattern file
/* if (!pbmread)
printf("\n Cannot open the Communication Pattern file ");
else
printf("\nCommunication Pattern with weight %s",argv[arg+1]);*/

fscanf(pbmread,"%s\n%d %d\n",head,&a,&b); //printf("\n%s\n%d %d\n",head,a,b);

for (j=0;j<b;j++)
{ for(i=0;i< a;i++)
{ fscanf(pbmread,"%d",&pbm[index][j][i]); // printf("%d ",pbm[index][j][i]);
}
//printf("\n");
}
fclose(pbmread);
}


/**** Distance Matrix and Total Dynamic Energy ****/

void groute(int argc,char **argv)
{
int x1,y1,x2,y2,k,l;
int index;
double DynEnergy;
float weight;
int distance;
int i,j;

for (arg=3,index=0;arg<argc;arg+=2,index++)
{
weight=atof(argv[arg+1]);
loadpatterns(argc,argv,index); // FUNCTION Load Communication Pattern

for (j=0;j<b;j++)
for (i=0;i<a;i++)
{ if (pbm[index][j][i]!=0)
{for (l=0;l<m;l++)
for (k=0;k<n;k++)
{ if (pgm[l][k] == i)
{ x1=l;y1=k;}
if (pgm[l][k]==j)
{ x2=l;y2=k;}
}
distance=(abs(y2-y1)+abs(x2-x1));// 'x+y'or manhattan distance between nodes for pbm[i][j]!=0
dist[j][i]=distance; // Distance matrix
}
if (pbm[index][j][i]==0)
{dist[j][i]=0;}
}
/*printf("\nweight %f ",weight);
printf("\n\nDistance Matrix\n");
for ( j=0;j<b;j++)
{ for ( i=0;i<a;i++)
printf("%d ",dist[j][i]);
printf("\n");
}*/
DynEnergy= energy(); // FUNCTION Dynamic energy for the pattern
TotalDynEnergy=TotalDynEnergy+DynEnergy;//Total Dynamic Energy for all patterns
}

}



int main(int argc,char *argv[])
{
int N_CO,N_MUT;

int dim;
//m=2; n=3;
// int ext1,ext2;
//printf(" %d %s %s %s",argc,argv[1],argv[2],argv[3],argv[4]);
int q;
printf("break1\n");
m=atoi(argv[1]);
n=atoi(argv[2]); // mxn layout
printf("\n %d %d",m ,n );
dim=m*n;
int x[dim];
int POP[POP_SIZE][dim];
float POP_DYN[POP_SIZE];
int i,j,k,l;
int first,second;
// int* ptr_co;
int t1[i];
float t;
int gen;
int MAX_Gen=10;
int layoutnumber=0;

for ( i=0;i<dim;i++)
x[i]=i;
q=n;
printf(" q is %d",q);

/**** Initial pop ***/
do { printf("enter loop");

for(i=0;i<dim;i++)
POP[layoutnumber][i]=x[i];
printf("layout break1");

l=0;k=0;
for(i=0;i<dim;i++)
{if (l==q)
{k=k+1;l=0;}
pgm[k][l]=x[i]; // layout of the nodes
l++;
}
printf("node break/n");
// groute(argc,argv);
// Dynamic Energy array
POP_DYN[layoutnumber]=TotalDynEnergy;

layoutnumber++;
TotalDynEnergy=0;
pskreddy
}while((next_permutation(x,x+dim)) /*&& ( layoutnumber < POP_SIZE)*/);
}


when i run gdb it gives
#0 0xb7d9bd6c in __strtouq_internal () from /lib/tls/i686/cmov/libc.so.6
#1 0xb7d9baff in __strtol_internal () from /lib/tls/i686/cmov/libc.so.6
#2 0xb7d99166 in atoi () from /lib/tls/i686/cmov/libc.so.6
#3 0x08048b26 in main (argc=2, argv=0xbf8c2ac4) at groute.cpp:139
(gdb) up 3
#3 0x08048b26 in main (argc=2, argv=0xbf8c2ac4) at groute.cpp:139
139 m=atoi(argv[1]);n=atoi(argv[2]); // mxn layout
Current language: auto; currently c++


I appreciate any help

pskr
psrujan is offline   Reply With Quote
Old 07-01-2007   #2 (permalink)
Cabhan
Linux Guru
 
Cabhan's Avatar
 
Join Date: Jan 2005
Location: Seattle, WA, USA
Posts: 2,398
First off, in the future, please surround your code in [code] tags.

The best way to handle this is to add debug statements (just print to cerr) at various places so you can pinpoint exactly where the segmentation fault is happening. Then see what you're doing. In this case, I notice that you're accessing argv[1] and argv[2] without ever checking what the value of argc is. If no commandline arguments are given, then argv[1] and argv[2] are undefined, and will very possibly segfault. I recognize that you are including commandline arguments, but if you weren't, this would be bad.

Anyway, use debug statements, find out where the segfault is occurring, and examine that line and the variables used in it. If you have a more specific question, let us know.
__________________
DISTRO=Gentoo
Registered Linux User #388732
Gentoo Linux, 410 GB HD, 1.2 GB RAM, Fluxbox, These are a Few of my Favorite Things
Cabhan is offline   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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 Off
Pingbacks are Off
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
SSL Segmentation when users load SSL page. merval Slackware Linux Help 0 11-03-2006 05:50 AM
Segmentation fault baparna Linux Programming & Scripting 1 05-16-2006 02:06 PM
Segmentation fault due to font problem mwarren2 Debian Linux Help 1 03-25-2006 11:15 AM
Segmentation fault ?! HELP! hlstriker Redhat / Fedora Linux Help 2 06-30-2005 05:37 PM
Segmentation Fault on Startup... hitman_et Linux Newbie 11 01-21-2005 02:06 AM




All times are GMT. The time now is 10:11 AM.




© 2000 - 2008 - All Rights Reserved - Property of  MAS Media

Content Relevant URLs by vBSEO 3.0.0