I've managed to complete recover however I want to understand the following segemntation fault issues:

1) Why if i use char * for a string for the filename variable it comes with an error? If i use an actual array of chars it's fine.

2) Why can't you use any file pointer based function in an else statement? If i use an IF statement it's fine but else throws a seg fault.

3) I used BYTE * and malloc for my buffer but I know others used an array of BYTE types for a buffer. Which is better methodology?

Thank you

  • So i fugured out the char * issue, you can't just leave it to be null, you need to allocate something to it first.
    – funprog31
    May 9, 2020 at 17:36

1 Answer 1

  1. You've already figured that one out.

  2. There's nothing that prevents using a file pointer in any kind of code block. We'd have to see an example of what you're doing that's not working here. My guess is that the else track is resulting in trying to use a pointer with a NULL value.

  3. It depends on what is being done. If allocating an unknown amount of memory dynamically (you don't know how much memory is going to be needed, or it can't be reused) then this will usually suggest using a pointer and mallocing memory as the program continues. OTOH, if you need to allocate a fixed amount of memory, or you can reuse the memory over and over, then it suggests allocating a regular variable or array. In this case, allocating a fixed array of a maximum size that can be reused repeatedly (a 512 byte array) is the better choice. Remember too that allocating memory dynamically does have overhead processing associated with it. It's not a big deal until you're talking about millions of executions.

You'll find out more about dynamic allocations with nodes in the next pset. ;-)

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

  • Thanks, I've added if else and else statements and it still works.
    – funprog31
    May 10, 2020 at 11:44

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .