Segmentation fault!

Started by
9 comments, last by Q-Parser 19 years, 3 months ago
Hi! I'm playing with parameters to function main() and I'm getting this error if no arguments are passed to executable. However, when I pass an argument it is print out. Here's the the code. I think it's ok.

int main (int argc, char *argv[]) {
  if (argc>1)
    printf("%s\n", argv[1]);
  return 0;
}

Could it be caused by compiler. I'm compiling with gcc.
Advertisement
Works for me. What version of gcc?
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Quote:Original post by Fruny
Works for me. What version of gcc?


gcc version 3.3.5 (Gentoo Linux 3.3.5-r1, ssp-3.3.2-3, pie-8.7.7.1)

Ok, I've probably posted it wrong. The code above works for me, too. I'd rather post all the code.

#include <stdio.h>#include <string.h>/* Function declaration */void OpenDatabase(char[], FILE*);void CloseDatabase(FILE*);/*  Main function*/int main (int argc, char *argv[]) {  //PERSON *entry;  FILE *f;	/* Pointer to file */    char filename[15];    if (argc>1)    strcpy(filename, argv[1]);  else    strcpy(filename, "NULL");  printf("Filename: %s\n", filename);    if ((strcmp(filename, "NULL"))!=0) {    OpenDatabase(filename, f);  printf("bla");  CloseDatabase(f);    return 0;}void OpenDatabase(char database[15], FILE *f) {  if ((f=fopen(database, "wb+"))==NULL)      printf("ERROR\n");}    void CloseDatabase(FILE *f) {  if (fclose(f)==EOF)     printf("ERROR\n");}
Quote:Original post by j0seph
Quote:Original post by Fruny
Works for me. What version of gcc?


gcc version 3.3.5 (Gentoo Linux 3.3.5-r1, ssp-3.3.2-3, pie-8.7.7.1)

Ok, I've probably posted it wrong. The code above works for me, too. I'd rather post all the code.

*** Source Snippet Removed ***

Could be because you attempt to call CloseDatabase without first calling OpenDatabase? (If you don't pass any arguments)
Quote:Original post by Spoonbender
Quote:Original post by j0seph
Quote:Original post by Fruny
Works for me. What version of gcc?


gcc version 3.3.5 (Gentoo Linux 3.3.5-r1, ssp-3.3.2-3, pie-8.7.7.1)

Ok, I've probably posted it wrong. The code above works for me, too. I'd rather post all the code.

*** Source Snippet Removed ***

Could be because you attempt to call CloseDatabase without first calling OpenDatabase? (If you don't pass any arguments)


Even if I call OpenDatabase and then CloseDatabase, I'm getting the same error. The strange thing about it is that, if I OpenDatabase and don't close it with CloseDatabase everything works fine, but it's not safe to leave the file open. I've found that the error must me in the CloseDatabase "procedure" but what???

EDIT: Even pure fclose(f) does not work!!!
Try initializing the array to all zeros before using it. This might be your problem.

char filename[15] = {0};

I have a feeling that it is full of junk and when you do the strcpy into it for the NULL value that it does not termiante at the end of the NULL, though the printf should reveal this as the problem. You could also change strcpy to:

strcpy(filename, "NULL\0")

to force the placement of the null character at the end of the string.

Quote:Original post by corliss
Try initializing the array to all zeros before using it. This might be your problem.

char filename[15] = {0};

I have a feeling that it is full of junk and when you do the strcpy into it for the NULL value that it does not termiante at the end of the NULL, though the printf should reveal this as the problem. You could also change strcpy to:

strcpy(filename, "NULL\0")

to force the placement of the null character at the end of the string.


strcpy always null-terminates the resulting string.

The problem is with the file pointer being passed to Open/CloseDatabase. You pass the uninitialized file pointer to OpenDatabase, which opens the database and sets the pointer. However, the pointer is passed to OpenDatabase by value, so changing it in that function doesn't change the actual variable used in main. Thus, when you pass this (still unitialized) pointer to CloseDatabase, you get the segmentation fault.

You need to modify the OpenDatabase method to take a FILE ** and pass the address of the pointer to it.

edit -- Or, better yet, have OpenDatabase return the pointer instead of taking it as a parameter, similar to the fopen function.
Pass-by-value vs pass-by-reference.

OpenDatabase only modifies the local copy of the f pointer. CloseDatabase still works on a garbage pointer.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Quote:Original post by corliss
Try initializing the array to all zeros before using it. This might be your problem.

char filename[15] = {0};

I have a feeling that it is full of junk and when you do the strcpy into it for the NULL value that it does not termiante at the end of the NULL, though the printf should reveal this as the problem. You could also change strcpy to:

strcpy(filename, "NULL\0")

to force the placement of the null character at the end of the string.


This is not the case. strcpy will terminate the string with '\0' automaticaly.

I just don't understand why I can't close file with fclose(f) after it's been fopen-ed ???
Quote:Original post by j0seph
I just don't understand why I can't close file with fclose(f) after it's been fopen-ed ???


Because the f you call fclose() on is not the same pointer as was returned by fopen().
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan

This topic is closed to new replies.

Advertisement