views:

198

answers:

3

I am working on an Operating Systems assignment for one of my summer classes. The teacher has provided an object file that provides functions that mimic the behaviour of a disk device driver. We are then to write a file system API that uses the disk device driver in C.

I am working on my file system format function named Format() which calls a function named DevFormat() from the teachers object file. My function is supposed to return 1 if it was able to successfully format the file system and 0 otherwise. DevFormat() returns 1 if it was able to successfully format the disk drive and 0 otherwise. Here is come code:

int Format()
{
    if (!DevFormat())
    {
        printf("Disk drive wasn't formatted successfully\n");
        return 0;
    }

    <Do some stuff to the file system here>

    printf("File system successfully formatted\n");
    return 1;
}

My problem is that Format() just abruptly ends without returning a value. I have found that the offending piece of code is the line: if (!DevFormat()). Now I am writing the assignment in C but am using the GNU C++ (g++) compiler to compile and link my project as the teacher said we could. I want to say that the reason Format() abruptly ends when the line if (!DevFormat()) is executed has to do something with the compilers interpretation of my code (I could be way way off. Its just a guess.). I found that my function abruptly ends as well if I change the code to if (0 == DevFormat()). The only way I can test for failure is by assigning the return value of DevFormat() to an int variable and then checking that.

Any help would be much appreciated. Is it something to do with the way the C++ compiler interprets my code? Is it I missed something so mundane that I should be ashamed of myself?

Thanks again for the help.

+14  A: 

I think maybe you have forgotten the braces around your if statement - I imagine you meant to write this:

if (!DevFormat()) {
    printf("Disk drive wasn't formatted successfully\n");
    return 0;
}

Only the printf statement was inside the if block, so the return statement was executed every time regardless of the return value of DevFormat(). That's a common gotcha in C :)

Peter
At the time of this comment, the code seems to be fixed.
Javier Badia
umm, what? The question has braces, and there is no sign of it having been edited... why is this so upvoted?
rmeador
@rmeador: Edits to the question within the first five minutes are not counted as an "edit" and don't show up in the edit history.
Greg Hewgill
@rmeador: the braces weren't there when I originally answered.
Peter
Yes- the braces were not there when I upvoted. The OP seems to have edited in.
DeadMG
A: 

That would certainly be a bug, but I don't see any missing braces in the code above - has it been edited?

Dugan
Yes, originally there were indeed missing braces.
Greg Hewgill
A: 

Try compiling with gcc -fno-exceptions and see if that changes anything. (Recomile the library too, just to be sure.) Also, I second @tholomew's request for elaboration on "Format() just abruptly ends without returning a value.".

David X