Tell me more ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

If I wanted perform an arbitrary set of processes, how would I accomplish that? Is there a language agnostic way of describing this?

If you found the previous statement confusing, here is where I will try to explain.

  1. A variable of a specific data type gives access to one place holder. int a;
  2. An array allows for a single entry point for a defined set of place holders. int a[5];
  3. And a linked list allows for an arbitrary number of place holders limited to the amount of space available. LinkedList a = new LinkedList;

If a function or sub procedure is like a variable in that a function is a single entry point for one set of procedures, what am I looking for that would allow for an arbitrary set of procedures to be performed from a single entry point.

My best guess at this point I would need a linked list of function pointers to cycle through?

Please treat the following as psuedo code:

int f(int x){   
    return x*x};

int g(int x){
    return x+x};

int (*procedures)(int x)[2];   //<<== no idea if this is even acceptable

procedures[0] = f;
procedures[1] = g;

If it's handy to describe this in terms of a technology, C++ should suffice.

share|improve this question
7  
It is not clear to me what you are trying to accomplish. – marco-fiset May 15 '12 at 15:15
1  
You mean like an array of function pointers? – Philip May 15 '12 at 15:38
Yes, but with dynamic allocation. I am just looking for sort of algorithm on how to set this kind of data structure. – hydroparadise May 15 '12 at 15:46
A list of function pointers then. – suszterpatt May 15 '12 at 16:02
When you say list, do you mean a linked list? Is this the right approach? – hydroparadise May 15 '12 at 16:07
show 1 more comment

2 Answers

up vote 1 down vote accepted

Just for completeness sake,

Yes, what you're describing is a linked list that contains function pointers. That's certainly one thing you could do. If this is a "right approach" is another story. You would have to describe the task you're trying to accomplish for use to judge what the best solution for it would be.

But

I do not believe there is a (convenient/safe) way to dynamically allocate memory for functions and define them. Even with a list of function pointers, you still need them to point to something. If you're ok with only pointing to a collection of predefined functions like multiplying X by X or adding X by X, sure, this works fine but you might as well use a fixed-size array. If you want a truly arbitrary set of processes, then you'd need to define your own functions during runtime. Which dives into self-modifying code. It's a fun and exciting field to study, but veers off into dark eldritch tomes of horror and insanity where mere mortals dare not to tread least they lose themselves into the void.

That's a way of saying it doesn't follow standard practices and most wouldn't consider it the "right approach". For some applications it's a novel solution, and it a fun experiment, but I typically wouldn't advise it myself.

share|improve this answer

In C++ a vector<function<int(int)>> seems fine for a ordered list of zero or more functions that take an integer and return an integer. Other languages use different structures, but the concept of "just make a collection of function objects" remains in most of them.

For functions of different signatures, it depends on the language and your goals.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.