Hello.

I am wondering how to pass data to hundred and hundreds of functions from main() in an application that has more than 1000 files. Can anybody give me some code?

Regards,

Supriyo

If you know how to pass the data to one function then there should not be a problem passing it to a million or more functions. The number of files (*.c or *.cpp) has nothing to do with it.

Data is passed to a function by calling it through an object in main(). Certain parameters can also passed by intitializing them. Can this be repeated for millions of functions? I have seen command line argumants but they do not seem to call those millions of funtions through objects.
Please correct me if I am wrong.
Regards
Supriyo

I think you need to clarify what you mean because I certainly don't know what you are talking about.

// function prototypes which may or may not be in the same *.cpp file as main()
int foo1(int), foo2(int), foo3(int), foo4(int) // etc. etc for each of the functions.  


int main()
{
    int data = 123;
    foo1(data);
    foo2(data);
    foo3(data);
    foo4(data);
    foo5(data);

    // now repeat the above line for as many times as you want to
}

>>I have seen command line argumants but they do not seem to call those millions of funtions through objects.

command-line parameters are only strings, not functions, so that do not call anything.

#include "stdafx.h"
#include<iostream>
using namespace std;
class employee
{
protected:
int basesal;
int houserent;
int tax;
public:
	employee()
	{
	}

employee(int a, int s,int t)
{
	basesal=a;
	houserent=s;
	tax=t;
}

virtual int getsal()
{
	int netsalary;
netsalary=basesal+houserent-tax;
return(netsalary);
}
};
class salesrep:public employee
{
int commission;
public:
	salesrep()
	{
	}
	salesrep(int a, int s,int t,int com)
{
	basesal=a;
	houserent=s;
	tax=t;
	commission=com;
}

int getsal()
{
	int netsalary;
netsalary=(basesal+ commission+houserent)-tax;
return(netsalary);
}
};

int main()
{
	employee em(10000,7000,12);
    salesrep sp(10000,7000,12,2000);
	employee *emp=&em;	
	cout<<emp->getsal()<<endl;
	emp=&sp;
	cout<<emp->getsal();
	return 0;
}

Is there a question somewhere in all that code?

Thanx.
First a employee object is constructed. Then a pointer to employeee is taken and assigned to the address of employee. The employee::getsal() is called through this pointer. An employee object is constructed again via the salesrep constructor. Now this pointer is assigned to the address of salesrep and the function salesrep::getsal() is called. The output:
16988
18988
Please correct me in case I am wrong.
Now referring to my original question. Is it possible to pass data to millions of functions in this way? How can this be acheived via the command line argument? What are the procedures adopted?

Now what you are wanting to do is a little different than your original question. I think what you want is to use virtual methods

class Animal
{
public:
   virtual void Speak() = 0;
};

class Cat : public Animal
{
public:
   virtual void Speak() { cout << "Meow\n"; }
};

class Dog : public Animal
{
public:
   virtual void Speak() { cout << "Wolf\n"; }
};

void foo(Animal& a)
{
   a.Speak();
}


int main()
{

   Cat c;
   Dog d;
   foo(c);
   foo(d);
}

>>can this be acheived via the command line argument?

This is just one of several possibilities

int main(int argc, char*argv[])
{
   Cat c;
   Dog d;
   if( argc == 2)
   {
      if( strcmp(argv[1],"Cat") == 0)
          foo(c);
      else if( strcmp(argv[1],"Dog") == 0)
          foo(d);
   }
}

Though Animal is an ABC a reference to it can be initialized (just as a pointer). When foo(c) is called Animal& a=c assignment takes place (because a base class reference can refer to a derived class object) and then subsequently c.speak() is called. Similarly for foo(d). (However I have found the keyword virtual is not used in overridden functions in derived classes of abstract classes(?). The output:
Meow
Wolf
Thanx for the command line argument. But here again the problem persists. Is it possible to call millions of functions by specifying strings in command line arguments? I think no. Please advise in case I am wrong in the above points.
Regards
Supriyo

>>it possible to call millions of functions by specifying strings in command line arguments? I think no

And why not? Just extend the example I posted to include several million methods in the classes. Of course the methods have to be known at compile time and your compiler may put a limit on the size of the executable.

If you're trying to use millions of command line parameters, I'm pretty sure you will run into a command line length limit. What that limit is will depend on the system the executable will be run on. You'll probably want to load the data into a file and have your program parse that line by line.

>What that limit is will depend on the system the executable will be run on.

the limit .. well in linux its stored in ARG_MAX and is usually 4096 bytes , but in other BSD s it might be a little higher .
Besides its a tedious job to pass on soo many arguments.
Out of curiosity , wat exactly are you trying to achieve ?

>What that limit is will depend on the system the executable will be run on.

the limit .. well in linux its stored in ARG_MAX and is usually 4096 bytes , but in other BSD s it might be a little higher .
Besides its a tedious job to pass on soo many arguments.
Out of curiosity , wat exactly are you trying to achieve ?

I am trying call a million function from main().

If you're trying to use millions of command line parameters, I'm pretty sure you will run into a command line length limit. What that limit is will depend on the system the executable will be run on. You'll probably want to load the data into a file and have your program parse that line by line.

Thanx. But in that case the data file will be very large and to parse it line by line in a program will be a very very complex operation. Could you give me some code? or any suitable solution?

Parsing the data file is very very simple approach. And it would be a lot easier than typing all those millions of command-line arguments every time you want to run that program :) Make the text file as simple or as complex as you want -- its all up to you how you do it and what your program requirements are.

First you have to figure out what you want the program to do before you can figure out what the command-line arguments are or how to design the text file that contains those arguments. Just saying "i want a million arguments" is a waste of time.

int main(int argc, char** argv)
{
    int iteration = 0;
    while(iteration < 1000000)
    {
        foo();
        iteration++;
    }
    return 0;
}

Foo gets called a million times. :)

I am trying call a million function from main().

honestly .. its sounds ridiculous if your not expressing yourself articulately .
please supply some more info .. on wat are those functions ?
and there are also many complexities involved . Managing to calling them is just one milestone .
for example :
ANSI C supports only 32 exit handlers, i dont have the slightest inkling on how will you manage their exit/ return ?

if your functions involve any kind of system programming like IPC or calling API s , then you gotto think about refactoring the code .. since API s will keep switching
the execution between kernel mode and user mode which is an overhead when it comes to a million functions.... and many things like that..

honestly .. its sounds ridiculous if your not expressing yourself articulately .
please supply some more info .. on wat are those functions ?
and there are also many complexities involved . Managing to calling them is just one milestone .
for example :
ANSI C supports only 32 exit handlers, i dont have the slightest inkling on how will you manage their exit/ return ?

if your functions involve any kind of system programming like IPC or calling API s , then you gotto think about refactoring the code .. since API s will keep switching
the execution between kernel mode and user mode which is an overhead when it comes to a million functions.... and many things like that..

Sorry Sir!

Currently I am working on a project in file management system. It has over 3000 files and nearly a million functions. One of the utility is file parsing to detect correct file name aliases. For this roughly 200 functions need to be called. I now need a text file and parse it to detect which functions need to be called and then call them. Can you give me an idea as to how to design this text file?

Regards,
Supriyo

First make a structure that contains a function pointer and a text string

struct functions
{
    void (*fn)(); // function pointer
    char* function_name;
};

now make an array of those structures for each of the functions you want to call from the command line arguments. Then for each command-line argument the program can loop through that array and find the desired array element that contains the function pointer to be called. You might be able to also implement this as a <map>

200 functions is a little lengthy so I'd probably put that array in a *.cpp file all by itself so that it would be easier to maintain.

One of the utility is file parsing to detect correct file name aliases. For this roughly 200 functions need to be called

these functions which ur wanting to call are they incorporated in some header files or members of some classes .... details ?

it would be prudent http://www.catb.org/~esr/faqs/smart-questions.html
to read it

well it will be prudent to have those functions under one class . so to check the file aliases u can pass the filename to object and allow it to pass thru the rest of the functions .

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.