I have been asked to set the error when API service used without module Initialization. But I don't understand what it means. When this situation typically happens? Can someone please explain me?
I have been asked to set the error when API service used without module Initialization. But I don't understand what it means. When this situation typically happens? Can someone please explain me?
It means you should do something like this.
Code:// api.h enum { API_SUCCESS, API_NOT_INITIALISED, // more errors } // public function prototypes // api.c static int isInitialised = 0; int apiInit ( ) { // do stuff isInitialised = 1; return 0; } // every other public API method follows this pattern. int apiSomeMethod ( ) { if ( !isInitialised ) { return API_NOT_INITIALISED; } // do stuff }
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
I am confused in what is the main advantage of doing this? I mean to say the other modules can call this functionality without it being initialized. Am i correct?
I think looking for an advantage in something like this is a waste of time... the way that you use either works reliably or it doesn't. I guess that can be a sort of advantage.
If there is some functionality that works regardless of the API being initialized, then you would omit the check(s), as it makes no difference anyway.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
Personally I think it's bad practice to check for something like that in a called function (it's the callee's responsibility to ensure pre-conditions -- like an API being initialised -- are met IMO). Mainly because the way suggested is horribly inefficient.
Did you just make an argument in favour of using gets()?
Assuming the user / caller is sane and trustworthy will get you owned every single time.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
For some reason I wrote it back to front anyway. In my opinion a function is not responsible for what calling functions do. If a function requires something to be initialised then the calling function is responsible (IMO) for making sure that condition is met, otherwise everything becomes an inefficient mess.
If the above is making an argument in favour of using gets() then I guess I am (but I'm not). Are you saying that gets() makes sure that stdin is a valid file handle? (because the source code I'm looking at has no such check) (confused)
Yes, this. If the stream isn't open for reading then that's the caller's responsibility (well, problem), not the called function and this is the complete opposite to the OP's library init example
Edit: looking at more real source code implementations for gets() it seems that I was wrong and it is indeed fine to fclose(stdin) and then call gets(). Oh well![]()
Last edited by Hodor; 02-25-2018 at 03:33 AM.