Jump to content

Segmentation fault


Recommended Posts

not comercial. no dependencies.

#include <stdio.h>

#include <string.h>



FILE *arq;

char hst[59000][100];

int qt_hst = 0;



struct classe

{

char name[50];

int start;

int end;

} tipo[500];





void open()

{

char cor;

int a = 0, b = 0, c = 0, d = 0, e = 0;



arq = fopen("test","r");



cor = getc(arq);

while (cor!=EOF)

{

 if (cor == 'n') qt_hst++;

 cor = getc(arq);

}

qt_hst++;

printf("Total lines: %dnn", qt_hst);



fseek(arq, 0, SEEK_SET);



/* drop all valid entries to memmory*/

cor = '0';

a=0; /* count blank/comments lines */

for (b; b < qt_hst-a; b++)

{

 fgets( hst[b], 99, arq );

 if (!strstr(hst[b],"#") )

 {

	 cor = '0';

	 if ( strchr(hst[b], 't') ) 

   strcpy(hst[b], strchr(hst[b], 't')+1);

	 else

	 {

   if ( strchr(hst[b], ' ') ) 

   {

  	 c = d = 0;

  	 for (c; c < strlen(hst[b]); c++)

     if (hst[b][c] == ' ') d++;

  	 strcpy(hst[b], strchr(hst[b], ' ')+d);

   }

   else { b--; a++; }

	 }

 }

 else

 {	

	 //printf("%c",7);

	 if (cor != '1')

	 {

   printf("%d - %s",e, hst[b]);

   if (tipo[e].start)

   {

  	 //printf("%s",tipo[e].start);

  	 tipo[e].end = b-1;

  	 e++;

  	 tipo[e].start = b;

   }

   else	tipo[e].start = b;

   strcpy( tipo[e].name, hst[b] );

   cor = '1';

	 }

	 b--; a++; 

 }

}

qt_hst = b;



if (tipo[0].start) tipo[e].end = qt_hst-1;



fclose(arq);



}



void repeat()

{

int a, b, c;

unsigned char tam[59000];

a = b = c = 0;



printf("ntScanning for duplicated entries...nn");



for (a; a<qt_hst;a++)

 tam[a]=strlen(hst[a]);



for (a=0; a < qt_hst; a++)

{

 for (b = a+1; b < qt_hst; b++)

 {

	 if (tam[a] == tam[b])

	 {

   if ( memcmp(hst[a], hst[b], tam[a]) == 0)

   {	

  	 printf("%d : %dn",a+1,b+1);

  	 printf("%s%sn",hst[a],hst[b]);

  	 c++;

   }

	 }	

 }

}  

printf("nnt%d repeated entries found.n",c);

}



void main()

{

char i;

st:

system("clear");



open();

repeat();

}

 

How do I ask for a keypress in ANSI default?

i=getch(); /* works in windows but not in linux */

 

Thank you!

Link to comment
Share on other sites

Okay, the problem is that when you do the fopen, it returns a NULL pointer since the file doesn't exist. Then the cor=getc(arq) command segfaults because it is being passed a NULL pointer.

 

Glitz.

Link to comment
Share on other sites

As a programmer, you need to anticipate oddball situations. Although this project may not need it due to expectation, in general, its good to do through error checking. Open a file? Check for a NULL. Do it everytime. Make another class or procedure that does that automatically for you so you don't have to repeat the work.

 

Also, how would you go about locating the source of the crash? Let's say you are not using an IDE, what kind of debugger can you use? On a primitive level, you can try inserting asserts() or simple printf() statements. If it reaches the printf() and displays it and then crashes, you can move it forward until you reach a point where you no longer see the result of the printf().

 

For more advance debugging, you can check a log that describes where the program crashes. I don't know enough about c programming on a linux platform to tell you how to do that. Anybody?

Link to comment
Share on other sites

I'm just a C beginner.

I could make a verification for the file existence. But I'm just testing...

I asked for because I have no trouble when using borland compiler on windows. By the way, you guys know a good debugger? I mean C debugger, not asm debugger.

Link to comment
Share on other sites

I use something called DDD. It has all the basic features that Borland does such as setting watches, viewing local variables, single stepping, viewing assembly (if you want to). I think it is available under the GPL. I got mine with a MacMillan edition of C-Forge.

 

Glitz.

Link to comment
Share on other sites

Silly me, DDD is included with Mandrake as well. You just have to make sure that when you compile the source code you have the compiler include debugging information. I think all that is needed is to compile with the -g option.

 

Glitz.

Link to comment
Share on other sites

DDD is a commandline compiler? Or you mean gcc -g?

 

ddd is a debugger. i am not sure but i remember it being a graphical front-end to gdb which is the commandline debugger. gcc -g just inserts debugging flags to the compiled code. think of this flags as 'hooks' that the debugger tries to hang on to when you want to stop a process on a specific line and browse its current status.

 

ciao!

Link to comment
Share on other sites

As a programmer, you need to anticipate oddball situations. Although this project may not need it due to expectation, in general, its good to do through error checking. Open a file? Check for a NULL. Do it everytime. Make another class or procedure that does that automatically for you so you don't have to repeat the work.

There is even better programming technique : some function defined as having a return code? Check this return code each time you call this function. Some function defined as returning a pointer ( fopen, malloc)? Check for NULL. The list continues...

Link to comment
Share on other sites

Hi guys!

I downloaded DDD and tried to install it.

'./configure' runs ok but when I do 'make' it generate an error:

 

In file included from logplayer.C:46:

/usr/include/c++/3.2/backward/fstream.h:38: using declaration `streampos' introduced ambiguous type `streampos'

make[1]: *** [logplayer.o] Error 1

make[1]: Leaving directory `/home/leandro/ddd-3.3.1/ddd'

make: *** [all-recursive] Error 1

How could I fix that?

Thank you in advance!

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

×
×
  • Create New...