How to call C++ like constructor in C

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • achaplot
    New Member
    • Jan 2010
    • 4

    How to call C++ like constructor in C

    Hi ..
    I have to use a global struct object, but I want to call some init function just when the object is created. In C++ this is achieved without hassle by writing a constructor.

    How do I achieve this in C? In C++ the init function is called before main() which is quite a pun for me in C.

    ex: main.c

    mystruct ms;

    int main()
    {
    ... some ops on ms ..
    }
  • achaplot
    New Member
    • Jan 2010
    • 4

    #2
    Real Example:
    std::map<std::s tring, mystruct*> registry;
    struct mystructD : public mustruct {
    mystructD() {
    registry["D"] = this;
    }
    };

    Comment

    • Banfa
      Recognized Expert Expert
      • Feb 2006
      • 9067

      #3
      In C you don't (unless you are willing to mess with the C-start-up code for your platform which I don't recomend since it is normally written at least partly in assembler).

      Write a function to do your initialisation and call it as the first operation in main.

      Comment

      • achaplot
        New Member
        • Jan 2010
        • 4

        #4
        Hi Banfa,
        Well the real life problem is - I dont have control on main. Yet I want to initialize my map with correct pointer.

        What will be the code generated by cfront or some c++ to c convertor?

        Thanks

        Comment

        • Banfa
          Recognized Expert Expert
          • Feb 2006
          • 9067

          #5
          Erm I am left wondering why you would be using a C++ to C compiler rather than just a C++ compiler, although I am aware a few platforms (main micro-processors) only have C compilers.

          I do not know the answer to your question, I have never had reason to convert from C++ to C but I suggest you run your code (or some simplified test code) through a C++ - C converter if you can find one now.

          Are you writing in C or C++? Is your project in C or C++? Is the project tool chain C or C++? If you have no control of main then you will need to look for another place early in code execution where you can insert your initialisation code.

          Comment

          • weaknessforcats
            Recognized Expert Expert
            • Mar 2007
            • 9214

            #6
            You have an easy solution.

            Write your code in C++ in a separate file, compile it in C++. At run time yur C++ global constructor will be called.

            Just remember that any C++ functions you will call from the code compiled in C must be extern "C" in the C++ code to trun off the mangler.

            This is done all the time.

            Comment

            • achaplot
              New Member
              • Jan 2010
              • 4

              #7
              I will rephrase -
              1. I do not want to use C++ at all.
              2. I want to declare a global struct object in my shared library
              3. Now I need a way to initialize this variable before main with some constructor like function.

              Please help me

              Comment

              • Banfa
                Recognized Expert Expert
                • Feb 2006
                • 9067

                #8
                You just can't do that in C, you have to have entered main before you call any functions.

                The only way to initialise structs in C, before entering main, is to provide a static initialiser.

                Code:
                struct MyStruct
                {
                    int a;
                    int b;
                    int c[3];
                } variable = { 1, 3, {4, 5}};

                Comment

                • donbock
                  Recognized Expert Top Contributor
                  • Mar 2008
                  • 2427

                  #9
                  You don't have access to main ... do you have access to some other function that is called before your global structure is used? If so, you can insert initialization code there. If this lower-level function is executed multiple times you can use the following snippet to insure the initialization only happens once:
                  Code:
                  ...
                  static int firstTime = 1;
                  ...
                  if (firstTime != 0)
                  {
                      /* insert initialization code here */
                      firstTime = 0;
                  }
                  What is the target platform? If the program executes on an embedded processor then you might have access to the start-up code that executes before main. If so, then you could initialize your structure there; but be careful how you declare the structure in C or else C start-up will overwrite your initialized values.

                  Comment

                  Working...