473,326 Members | 2,108 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

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 ..
}
Jan 2 '10 #1
8 2767
Real Example:
std::map<std::string, mystruct*> registry;
struct mystructD : public mustruct {
mystructD() {
registry["D"] = this;
}
};
Jan 2 '10 #2
Banfa
9,065 Expert Mod 8TB
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.
Jan 2 '10 #3
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
Jan 3 '10 #4
Banfa
9,065 Expert Mod 8TB
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.
Jan 3 '10 #5
weaknessforcats
9,208 Expert Mod 8TB
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.
Jan 3 '10 #6
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
Jan 7 '10 #7
Banfa
9,065 Expert Mod 8TB
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.

Expand|Select|Wrap|Line Numbers
  1. struct MyStruct
  2. {
  3.     int a;
  4.     int b;
  5.     int c[3];
  6. } variable = { 1, 3, {4, 5}};
  7.  
Jan 7 '10 #8
donbock
2,426 Expert 2GB
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:
Expand|Select|Wrap|Line Numbers
  1. ...
  2. static int firstTime = 1;
  3. ...
  4. if (firstTime != 0)
  5. {
  6.     /* insert initialization code here */
  7.     firstTime = 0;
  8. }
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.
Jan 7 '10 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: S³awek | last post by:
Can one constructor of an object call another constructor of the same class? class foo { foo(float f, int i) // a "full" constructor { ... } foo(int i) // a "simplified" constructor {
23
by: Fabian Müller | last post by:
Hi all, my question is as follows: If have a class X and a class Y derived from X. Constructor of X is X(param1, param2) . Constructor of Y is Y(param1, ..., param4) .
8
by: trying_to_learn | last post by:
Why do we need to explicitly call the copy constructor and the operator = , for base class and member objects in composition? ....book says "You must explicitly call the GameBoard copy-constructor...
4
by: Greg | last post by:
Is it possible to call a constructor from within a constructor I mean Class A { public A(string getModifiedVal) { .........
18
by: AlexanderVX | last post by:
How do I write a constructor mehtod call in this case /*-----------*/ template<typename Tclass CObjectPoolImpl { public: void smth(T* pObj) { if (pObj)
13
by: shsingh | last post by:
I have a class A containing some map as data variables. I creat an object of class A on heap by allocatiing memory by using "malloc". This will return me the required memory but the object is not...
7
by: dragoncoder | last post by:
Hello experts, I have the following code me. =cat mystring.h #include<iostream> using namespace std; class mystring {
7
by: cppquester | last post by:
What does this code do? #include <iostream> class A { public: A() { std::cout << "A::A()" << std::endl;} };
12
by: Rahul | last post by:
Hi Everyone, I have the following code and i'm able to invoke the destructor explicitly but not the constructor. and i get a compile time error when i invoke the constructor, why is this so? ...
15
by: asm23 | last post by:
Hi, everyone, I'm studying the <<Thinking in C++>volume Two. In Chapter One, the example code : Auto_ptr.cpp //------------------------------------------------------- #include <memory> #include...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.