Sign in to follow this  

Segmentation fault!

This topic is 4716 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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");
}

Share this post


Link to post
Share on other sites
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)

Share this post


Link to post
Share on other sites
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!!!

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Pass-by-value vs pass-by-reference.

OpenDatabase only modifies the local copy of the f pointer. CloseDatabase still works on a garbage pointer.

Share this post


Link to post
Share on other sites
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 ???

Share this post


Link to post
Share on other sites
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().

Share this post


Link to post
Share on other sites
This works fine now. I did according to Dave Hunt's suggestion:

#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]);
OpenDatabase(filename, &f);
}
else
strcpy(filename, "NULL");
printf("Filename: %s\n", filename);

if ((strcmp(filename, "NULL"))!=0) {
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");
}




I've always been confused with this pointer stuff.
Thanks!

Share this post


Link to post
Share on other sites

This topic is 4716 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this