Jason
Jason

Reputation: 11363

Passing String as argument- getting segfault in function

SOLVED See bottom of question for solution.

I'm having trouble with passing on a String argument to my function, and am getting a segmentation fault when the function is called. The program takes in a command line input and passes on the file provided to the function after validation.

My function code goes like this:

char *inputFile; //
inputFile= argv[2];
strcpy(inputFile, argv[2]);
compress(inputFile){ 

//file open and creation work bug-free
//compression action to be written
void compress(char inputFile){
    //compression code here
}

When the function is called, a segfault is thrown, and the value of inputFile is 0x00000000, when prior to the function call, it had a memory location and value of the test file path.

Some of the variations I've tried, with matching function prototypes:

compress(char *inputFile)
compress (char inputFile[])

I also changed the variable.

Why is a variable with a valid memory address and value in the debugger suddenly erase when used as a parameter?

Edit 1:

Incorporating suggestions here, I removed the inputFile= argv[2] line, and the debugger shows the strcpy function working.

However, I've tried both compress(char *inputFile) per Edwin Buck and compress(argv[2]) per unwind, and both changes still result in Cannot access memory at address 0xBEB9C74C

The strange thing is my file validation function checkFile(char inputFile[]) works with the inputFile value, but when I pass that same parameter to the compress(char inputFile[]) function, I get the segfault.

Edit 2- SOLVED

You know something is going on when you stump your professor for 45 min. It turns out I had declared the file read buffer as a 5MB long array inside the compress() method, which in turn maxed out the stack frame. Changing the buffer declaration to a global variable did the trick, and the code executes.

Thanks for the help!

Upvotes: 1

Views: 3440

Answers (3)

Edwin Buck
Edwin Buck

Reputation: 70909

First, to copy something from argv[2] to somewhere else, you need some memory for "that somewhere else". You could allocate the memory based on the size of argv[2] but for our simple example, a very large fixed size buffer will do.

char inputfile[2048];

It looks like you tried to do this by the assignment operator, which doesn't really do what you intended.

// this is not the way to what you seek, as it doesn't create any new memory for inputfile
char* inputfile = argv[2];

in passing the inputfile variable to a procedure, you want to pass much more than a single character, so void compress(char inputfile) is not an option. That leaves

compress(char *inputFile)  // I prefer this one
compress (char inputFile[])

which both work, but in my experience the first is preferred, as some older compilers tend to make distinctions between pointer and array semantics. These compilers have no issues casting an array to a pointer (which is required as part of the C language specification); however, casting a pointer to an array gets a bit messy in such systems.

Upvotes: 2

unwind
unwind

Reputation: 399881

You shouldn't be writing into memory used to hold argv[2].

You don't seem to quite understand how strings are represented; you're copying both the pointer (with the assignment) and the actual characters (with the strcpy()).

You should just do compress(argv[2]); once you've verified that that argument is valid.

Upvotes: 4

brismith
brismith

Reputation: 696

You've not allocated any memory for the char * to use. All you've done with char *inputfile is allocated a pointer.

Upvotes: 0

Related Questions