Thread: API Service used without Module Initialization

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    347

    API Service used without Module Initialization

    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?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    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.

  3. #3
    Registered User
    Join Date
    Feb 2012
    Posts
    347
    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?

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    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.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Quote Originally Posted by Satya View Post
    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?
    Well I suppose you could try reading from a file without opening it first. Let me know how you get on with that.
    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.

  6. #6
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    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.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    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.

  8. #8
    Registered User
    Join Date
    Feb 2012
    Posts
    347
    Quote Originally Posted by Salem View Post
    Well I suppose you could try reading from a file without opening it first. Let me know how you get on with that.
    Thank You, I understood.

  9. #9
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    Quote Originally Posted by Salem View Post
    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.
    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)

  10. #10
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    Quote Originally Posted by Salem View Post
    Well I suppose you could try reading from a file without opening it first. Let me know how you get on with that.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. functions anywhere? (in the module)
    By Mnemonist in forum C++ Programming
    Replies: 3
    Last Post: 03-05-2010, 01:03 AM
  2. Module Help!
    By J-Camz in forum C Programming
    Replies: 10
    Last Post: 11-27-2008, 08:29 PM
  3. module without using %
    By KIBO in forum C++ Programming
    Replies: 9
    Last Post: 01-16-2008, 11:24 PM
  4. module.h
    By podiyan in forum Linux Programming
    Replies: 3
    Last Post: 10-24-2007, 05:53 AM
  5. Replies: 1
    Last Post: 06-03-2005, 01:03 AM

Tags for this Thread